- JavaFX - CSS
- JavaFX - Layout Panes
- JavaFX - Charts
- JavaFX - UI Controls
- JavaFX - Event Handling
- JavaFX - 3D Shapes
- JavaFX - Images
- JavaFX - Colors
- JavaFX - Animations
- JavaFX - Transformations
- JavaFX - Effects
- JavaFX - Text
- JavaFX - 2D Shapes
- JavaFX - Application
- JavaFX - Architecture
- JavaFX - Environment
- JavaFX - Overview
- JavaFX - Home
JavaFX Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JavaFX - Animations
In general, animating an object imppes creating illusion of its motion by rapid display. In JavaFX, a node can be animated by changing its property over time. JavaFX provides a package named javafx.animation. This package contains classes that are used to animate the nodes. Animation is the base class of all these classes.
Using JavaFX, you can apply animations (transitions) such as Fade Transition, Fill Transition, Rotate Transition, Scale Transition, Stroke Transition, Translate Transition, Path Transition, Sequential Transition, Pause Transition, Parallel Transition, etc.
All these transitions are represented by inspanidual classes in the package javafx.animation.
To apply a particular animation to a node, you have to follow the steps given below −
Create a require node using respective class.
Instantiate the respective transition (animation) class that is to be appped
Set the properties of the transition and
Finally play the transition using the play() method of the Animation class.
In this chapter we are going to discuss examples of basic transitions(Rotation, Scapng, Translation).
Rotate Transition
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.apppcation.Apppcation; import static javafx.apppcation.Apppcation.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; pubpc class RotateTransitionExample extends Apppcation { @Override pubpc 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.milps(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(); } pubpc static void main(String args[]){ launch(args); } }
Compile and execute the saved java file from the command prompt using the following commands.
javac RotateTransitionExample.java java RotateTransitionExample
On executing, the above program generates a JavaFX window as shown below.
Scale Transition
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.apppcation.Apppcation; import static javafx.apppcation.Apppcation.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; pubpc class ScaleTransitionExample extends Apppcation { @Override pubpc 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.milps(1000)); //Setting the node for the transition scaleTransition.setNode(circle); //Setting the dimensions for scapng 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(); } pubpc static void main(String args[]){ launch(args); } }
Compile and execute the saved java file from the command prompt using the following commands.
javac ScaleTransitionExample.java java ScaleTransitionExample
On executing, the above program generates a JavaFX window as shown below.
Translate Transition
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.apppcation.Apppcation; 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; pubpc class TranslateTransitionExample extends Apppcation { @Override pubpc 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.milps(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(); } pubpc static void main(String args[]){ launch(args); } }
Compile and execute the saved java file from the command prompt using the following commands.
javac TranslateTransitionExample.java java TranslateTransitionExample
On executing, the above program generates a JavaFX window as shown below.
In addition to these, JavaFX provides classes to apply more transitions on nodes. The following are the other kinds of transitions supported by JavaFX.
Transitions that effects the attributes of the nodes
Transition that involve more than one basic transitions
Transition that translate the object along the specified path