The PathTransition class is the most powerful movement transition and it has two key methods:
Method | Description |
---|---|
setPath(Shape) | Sets the path to follow |
setOrientation(param) | Determines how the the Node rotates as it follows its path The parameter is one of two types: - OrientationType.NONE (default) The Node will not change its orientation as it slides along its path. - OrientationType.ORTHOGONAL_TO_TANGENT The Node will rotate as it slides along its path to keep it orthogonal to the tangent line of its path. This is the same way that a rollercoaster rotates to follow the shape of its track. |
The code below moves a Node along a Circle.
// Create a Node. Could be any kind.
Rectangle rect = new Rectangle(50, 200, 100, 50);
// Create a Circle to use as our path Shape
Circle circle = new Circle(300, 200, 100, Color.LIGHTBLUE);
// Create the Transition
PathTransition pt = new PathTransition();
pt.setDuration(Duration.seconds(2));
pt.setPath(circle);
pt.setNode(rect);
Notes - The path could be any Shape. It happens to be a Circle here. - The circle can be visible or hidden based on whether you add it to the root node's child list. - Remember to call pt.play() after showing the stage.
Here's the same code but the Node rotates to stay orthogonal to its path.
// Create a Node. Could be any kind.
Rectangle rect = new Rectangle(50, 200, 100, 50);
// Create a Circle to use as our path Shape
Circle circle = new Circle(300, 200, 100, Color.LIGHTBLUE);
// Create the Transition
PathTransition pt = new PathTransition();
pt.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setDuration(Duration.seconds(2));
pt.setPath(circle);
pt.setNode(rect);
Notes - Try changing the path to a Rectangle or a Polygon.
Last modified: March 28, 2023
Back to Other Transitions