Implementation
Let’s look at the program in which we’ll implement the JavaFX path transition. Here, we will be creating a rectangle with the help of the constructor and size parameters. The PathTransition object is then constructed, and setters are used to set properties. Then it shows a rectangle moving along the cubic curve path. The dots between the two rectangle images in the output is just used to show the movement of the rectangle to the next position. The path duration is set to 1 second and the cycle is set to 10.
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.animation.PathTransition.OrientationType;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import javafx.stage.Stage;
public class AnimationUI extends Application {
@Override
public void start(Stage stage) throws Exception
{
//setting up the rectangle
Rectangle newrect = new Rectangle(120,120,100,100);
newrect.setStroke(Color.GREEN);
newrect.setFill(Color.YELLOW);
newrect.setStrokeWidth(20);
//Setting up the path
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();
//Setting duration for the PathTransition
pathTransition.setDuration(Duration.millis(1000));
pathTransition.setNode(newrect);
pathTransition.setPath(newpath);
//setting orientation for the path transition
pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
//setting up the cycle count
pathTransition.setCycleCount(10);
pathTransition.setAutoReverse(true);
//Playing path transition
pathTransition.play();
//Configuring group and scene
Group root = new Group();
root.getChildren().addAll(newrect);
Scene scene = new Scene(root,700,450);
stage.setScene(scene);
stage.setTitle("Path Transition Example");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

You can also try this code with Online Java Compiler
Run Code
Execution
To execute the above program. You can follow the given steps:
-
Open the terminal in the same folder where you have saved your source code.
-
Enter the below code and hit enter.
javac --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java
After you hit enter a new file will automatically be generated namely filename.class
-
Now, enter the below command and hit enter to execute the above program.
java --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java
- You have successfully executed the JavaFX program.
Output

Frequently Asked Questions
What is the JavaFX path?
The path element is used to draw a straight line from the current position to a point in the specified coordinates.
What is an object in java?
A Java object is an instance of a Java class. Every object has a unique identity, behavior, and state. Fields store an object's state, whereas methods exhibit the object's action.
Which method is used to develop a JavaFX application?
To create a JavaFX application start() method is used. The primary entry point for all JavaFX applications is the start() function.
Conclusion
In this article, we have extensively discussed the JavaFX path transition Class present inside JavaFX Framework. We have seen different constructors to initialize the path transition. Also, we have seen various methods available inside the class along with the implementation.
If you think this blog has helped you enhance your knowledge about JavaFx path transition and if you would like to learn more, check out our articles JavaFX, JavaFX Rotate Transition, JavaFX Scale Transition, and JavaFX Fill Transition.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!