×

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 Rotate Transition

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

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 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 ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

4 minutes read.

JavaFX Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX Reflection

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

4 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Drop Shadow

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

4 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 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 Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 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 Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Bloom

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

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.