×

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape along the path. The path transition required the user to specify the path for the transition. This is done using the class Path in the javafx.scene.shape package. We have to specify the x and y coordinates for the given node at the regular intervals to create the transition. All methods needed for this purpose are present in the javafx.animation.PathTransition class.

Constructor to create Path Transition:

1. public PathTransition():

This Path constructor is used to create path transition and properties can be set using setters.

2. public PathTransition( Duration time, shape path ):

This Path constructor is used to create path transition with specified duration and shape and properties can be set using setters.

3. public PathTransition(Duration duration, Shape path, Node node):

This Path constructor is used to create path transition with specified duration and shape with node specified and properties can be set using setters.

Properties and corresponding methods to create Translate Transition:

1. duration

This property is of double type. It is used to represent the duration of the transition and the setDuration() method is used to set it.

2. node

This property is of the object type. It is used to represent the shape on which the animation is to be carried out and setNode() method is used to set it.

3. orientation

This property is of the object type. It is used to represent the orientation for the given node to specify the animation along the path and the setOrientation() method is used to set it.

4. path

This property is of the object type. It is used to show the path for the animation of the shape. The setPath() method is used to set it.

JavaFX Animation - Path Transition

Example:

import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;


public class AnimationUI extends Application {  


    @Override  
    public void start(Stage stage) throws Exception 
    { 
    
    Rectangle newrect = new Rectangle(120,120,100,100);     
    newrect.setStroke(Color.BLUE);  
    newrect.setFill(Color.RED);  
    newrect.setStrokeWidth(20);  
          
    Path newpath = new Path();  
    newpath.getElements().add (new MoveTo (120f, 20f));  
    newpath.getElements().add (new CubicCurveTo (200f, 200f, 450f, 320f, 560f, 40f));  
          
        PathTransition pathTransition = new PathTransition();  
         
        pathTransition.setDuration(Duration.millis(1000));  
          
        pathTransition.setNode(newrect);  
          
        pathTransition.setPath(newpath);  
          
        pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);  
          
        pathTransition.setCycleCount(10);  
          
        pathTransition.setAutoReverse(true);  
      
        pathTransition.play();  
          
     
        Group root = new Group();  
        root.getChildren().addAll(newrect);  
         Scene scene = new Scene(root,700,450);  
         stage.setScene(scene);  
        stage.setTitle("JavaFx Path Transition example");  
         stage.show();  
     }
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Translate Transition in JavaFX, we have to import all the required libraries such as javafx.animation.PathTransition, javafx.animation.PathTransition.OrientationType, javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.paint.Color, import javafx.scene.shape.CubicCurveTo, javafx.scene.shape.MoveTo, javafx.scene.shape.Path, javafx.scene.shape.Rectangle, javafx.stage.Stage, javafx.util.Duration.

Then we have created one class named AnimationUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a rectangle moving along the path, a Group object is created which is then passed to the Scene class object.

The rectangle is created with the help of the constructor and size parameters. Then the object of PathTransition is created and setters are used to set properties. Then it displays a rectangle that moves along the cubic curve path.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX Path Transition example”. Also, it displays a rectangle field with red color moving along the cubic curve path.

JavaFX Animation - Path Transition JavaFX Animation - Path Transition JavaFX Animation - Path Transition

Related Topics

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

4 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX HBox

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All...

3 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Fill Transition

In the JavaFX application, in order to generate the fill transition, we have to apply fill transition on any particular shape. It has the ability to fill the given shape...

4 minutes read.