”;
In general, the Translate transition simply moves an object from one location to another on an application. This is usually done by specifying the new coordinate points or the distance this object needs to be moved to.
JavaFX allows you to apply the translate transition on various nodes. For instance, in few applications on important days, some animated characters are seen moving around the application screen; like Santa Claus on Christmas. That can be performed using translation transition.
Translate Transition in JavaFX
The Translate transition in JavaFX moves the JavaFX nodes for a specified duration using the TranslateTransition class of javafx.animation package. This class contains various properties which are listed below to improve the quality of the said animation.
-
byX − Specifies the incremented stop X coordinate value, from the start, of this TranslateTransition.
-
byY − Specifies the incremented stop Y coordinate value, from the start, of this TranslateTransition.
-
byZ − Specifies the incremented stop Z coordinate value, from the start, of this TranslateTransition.
-
duration − The duration of this TranslateTransition
-
fromX − Specifies the start X coordinate value of this TranslateTransition.
-
fromY − Specifies the start Y coordinate value of this TranslateTransition.
-
fromZ − Specifies the start Z coordinate value of this TranslateTransition.
-
node − The target node of this TranslateTransition.
-
toX − Specifies the stop X coordinate value of this TranslateTransition.
-
toY − The stop Y coordinate value of this TranslateTransition.
-
toZ − The stop Z coordinate value of this TranslateTransition.
Example
Following is the program which demonstrates Translate Transition in JavaFX. Save this code in a file with the name TranslateTransitionExample.java.
import javafx.animation.TranslateTransition; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class TranslateTransitionExample extends Application { @Override public void start(Stage stage) { //Drawing a Circle Circle circle = new Circle(); //Setting the position of the circle circle.setCenterX(150.0f); circle.setCenterY(135.0f); //Setting the radius of the circle circle.setRadius(100.0f); //Setting the color of the circle circle.setFill(Color.BROWN); //Setting the stroke width of the circle circle.setStrokeWidth(20); //Creating Translate Transition TranslateTransition translateTransition = new TranslateTransition(); //Setting the duration of the transition translateTransition.setDuration(Duration.millis(1000)); //Setting the node for the transition translateTransition.setNode(circle); //Setting the value of the transition along the x axis. translateTransition.setByX(300); //Setting the cycle count for the transition translateTransition.setCycleCount(50); //Setting auto reverse value to false translateTransition.setAutoReverse(false); //Playing the animation translateTransition.play(); //Creating a Group object Group root = new Group(circle); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle("Translate 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 TranslateTransitionExample.java java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateTransitionExample
Output
On executing, the above program generates a JavaFX window as shown below.
”;