”;
Animations in JavaFX can be applied to change an object”s geometry, properties, position, etc. Rotate transition is used to deal with an object”s position by retaining its shape and properties.
An object is pivoted at its centre point and rotated around it. This type of animation is known as Rotate transition. Various software allow you to apply this rotate transition on an object by rotating it, while it is being displayed.
This type of animation can be applied while entering the object into the application or exiting it.
Rotate Transition in JavaFX
Rotate Transition in JavaFX is applied using the RotateTransition class in the javafx.animation package. This is done by specifying the starting value, the ending value and the duration of the transition. The RotateTransition class has the following properties −
-
axis − Specifies the axis of rotation for this RotateTransition.
-
node − The target node of this RotateTransition.
-
byAngle − Specifies the incremented stop angle value, from the start, of this RotateTransition.
-
fromAngle − Specifies the start angle value for this RotateTransition.
-
toAngle − Specifies the stop angle value for this RotateTransition.
-
duration − The duration of this RotateTransition.
Example
Following is the program which demonstrates Rotate Transition in JavaFX. Save this code in a file with the name RotateTransitionExample.java.
import javafx.animation.RotateTransition; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.stage.Stage; import javafx.util.Duration; public class RotateTransitionExample extends Application { @Override public void start(Stage stage) { //Creating a hexagon Polygon hexagon = new Polygon(); //Adding coordinates to the hexagon hexagon.getPoints().addAll(new Double[]{ 200.0, 50.0, 400.0, 50.0, 450.0, 150.0, 400.0, 250.0, 200.0, 250.0, 150.0, 150.0, }); //Setting the fill color for the hexagon hexagon.setFill(Color.BLUE); //Creating a rotate transition RotateTransition rotateTransition = new RotateTransition(); //Setting the duration for the transition rotateTransition.setDuration(Duration.millis(1000)); //Setting the node for the transition rotateTransition.setNode(hexagon); //Setting the angle of the rotation rotateTransition.setByAngle(360); //Setting the cycle count for the transition rotateTransition.setCycleCount(50); //Setting auto reverse value to false rotateTransition.setAutoReverse(false); //Playing the animation rotateTransition.play(); //Creating a Group object Group root = new Group(hexagon); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle("Rotate transition example "); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } }
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls RotateTransitionExample.java java --module-path %PATH_TO_FX% --add-modules javafx.controls RotateTransitionExample
Output
On executing, the above program generates a JavaFX window as shown below.
”;