”;
As its name suggests, scaling refers to increasing or decreasing the size of an object. In computer graphics, by applying the scale transition on an object (or image), you can either increase or decrease its size, for a specified duration.
This transition can also be applied on various JavaFX nodes, on 2D shapes, 3D shapes, text, and any other element on an application.
Scale Transition in JavaFX
The Scale transition is performed in JavaFX using the ScaleTransition class which belongs to the javafx.animation package. This class contains various properties that assist in playing this animation on a JavaFX application −
-
byX − Specifies the incremented stop X scale value, from the start, of this ScaleTransition.
-
byY − Specifies the incremented stop Y scale value, from the start, of this ScaleTransition.
-
byZ − Specifies the incremented stop Z scale value, from the start, of this ScaleTransition.
-
duration − The duration of this ScaleTransition
-
fromX − Specifies the start X scale value of this ScaleTransition.
-
fromY − Specifies the start Y scale value of this ScaleTransition.
-
fromZ − Specifies the start Z scale value of this ScaleTransition.
-
node − The target node of this ScaleTransition.
-
toX − Specifies the stop X scale value of this ScaleTransition.
-
toY − The stop Y scale value of this ScaleTransition.
-
toZ − The stop Z scale value of this ScaleTransition.
Example
Following is the program which demonstrates Scale Transition in JavaFX. Save this code in a file with the name ScaleTransitionExample.java.
import javafx.animation.ScaleTransition; 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.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class ScaleTransitionExample extends Application { @Override public void start(Stage stage) { //Drawing a Circle Circle circle = new Circle(); //Setting the position of the circle circle.setCenterX(300.0f); circle.setCenterY(135.0f); //Setting the radius of the circle circle.setRadius(50.0f); //Setting the color of the circle circle.setFill(Color.BROWN); //Setting the stroke width of the circle circle.setStrokeWidth(20); //Creating scale Transition ScaleTransition scaleTransition = new ScaleTransition(); //Setting the duration for the transition scaleTransition.setDuration(Duration.millis(1000)); //Setting the node for the transition scaleTransition.setNode(circle); //Setting the dimensions for scaling scaleTransition.setByY(1.5); scaleTransition.setByX(1.5); //Setting the cycle count for the translation scaleTransition.setCycleCount(50); //Setting auto reverse value to true scaleTransition.setAutoReverse(false); //Playing the animation scaleTransition.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("Scale 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 ScaleTransitionExample.java java --module-path %PATH_TO_FX% --add-modules javafx.controls ScaleTransitionExample
Output
On executing, the above program generates a JavaFX window as shown below.
”;