Definition of JavaFX Animation
In JavaFX, by altering the property by time, animation can be done to a particular node. For this, a package javafx.animation is provided by JavaFX. It consists of classes that help in animating the nodes. For all these classes, the animation is considered as the base class. There are different animations like Fill Transition, Scale Transition, Fade Transition, Stroke Transition, Rotate Transition, Sequential Transition, etc in this. All of them are denoted by separate classes in the javafx.animation package. This animation is also known as the transition. In this article, we will see more in animation.
Constructors
Below are the two constructors of animation in JavaFX.
- Animation()
- Animation(double targetFramerate)
Methods
Following are the methods of animation in JavaFX.
- autoReverseProperty(): This property defines whether on alternating cycles, animation reverses the direction or not.
- currentRateProperty(): This is a ready only variable that denotes the current direction or current speed at which the animation has to be played.
- currentTimeProperty(): This property defines the play head position of animation.
- cycleCountProperty(): This property defines the count of cycles of animation.
- cycleDurationProperty(): This is a ready only variable that denotes the cycle duration of animation has to be played.
- delayProperty(): This property delays the animation.
- getCuePoints(): Animation’s important positions are marked using the cue points.
- getCurrentRate(): currentRate property’s value will be retrieved.
- getCurrentTime(): currentTime property’s value will be retrieved.
- getCycleCount(): cycleCount property’s value will be retrieved.
- getCycleDuration(): cycleDuration property’s value will be retrieved.
- getDelay(): delay property’s value will be retrieved.
- getOnFinished(): onFinished property’s value will be retrieved.
- getRate(): rate property’s value will be retrieved.
- getStatus(): status property’s value will be retrieved.
- getTotalDuration(): totalDuration property’s value will be retrieved.
- isAutoReverse(): autoReverse property’s value will be retrieved.
- jumpTo(Durationtime): This method helps in jumping to animation’s specific position.
- jumpTo(StringcuePoint): This method helps in jumping to animation’s predefined position.
- pause(): This method pauses the animation.
- play(): This method plays the animation from the present position in a direction denoted by rate.
- playFrom(Durationtime): This method plays the animation from the position which is mentioned.
- playFrom(StringcuePoint): This method plays the animation from the position which is predefined.
- playFromStart(): This method plays the animation from the position which is initial and moves in a forward direction.
- setAutoReverse(boolean v): autoReverse property’s value will be set as v.
- setCycleCount(int v): CycleCount property’s value will be set as v.
- setCycleDuration(Durationv): CycleDurationproperty’s value will be set as v.
- setDelay(Durationv): Delay property’s value will be set as v.
- setOnFinished(EventHandler<ActionEvent> v): OnFinished property’s value will be set as v.
- setRate(double v): Rate property’s value will be set as v.
- setStatus(Statusv): Statusproperty’s value will be set as v.
- statusProperty(): Animation starts.
- stop(): Animation stops and the play head is reset to the initial position.
- onFinishedProperty(): This property explains the action that has to be executed at the animation conclusion.
- rateProperty(): This property defined the direction or speed of playing the animation.
- totalDurationProperty(): This is a ready only variable that denotes the cycle duration of animation has to be played including the repeats.
How to Create Animation in JavaFX?
Let us see how animation is done to a particular node.
- First, a node that is required is created using a corresponding class.
- Once this is done, configure the properties.
Rectangle rt = new Rectangle(136,112,112,112);
r.setFill(Color.YELLOW);
- Instantiate the corresponding transition class or animation class which has to be applied
RotateTransition r = new RotateTransition();
- Set the transition properties such as cycle count, duration.
r.setDuration(Duration.millis(2000));
r.setAxis(Rotate.Y_Axis);
r.setCycleCount(600);
- Fix the target node where the transition has to be applied.
r.setnode(rt);
- Play the transition with the help of play() method
r.play();
Examples of JavaFX Animation
Now, we will see a sample program on JavaFX animation.
Example #1
Code:
JavaFX program to demonstrate animation.
Code:
import javafx.animation.RotateTransition;
import javafx.application.Application;
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;
//main class
public class AnimationSamplePorogram extends Application {
@Override
public void start(Stage st) {
//create a hexagon
Polygon hx = new Polygon();
//Add coordinates to the created hexagon
hx.getPoints().addAll(new Double[]{
220.0, 60.0,
420.0, 60.0,
430.0, 160.0,
430.0, 260.0,
230.0, 260.0,
180.0, 160.0,
});
//fill color for the created hexagon
hx.setFill(Color.YELLOW);
//rotate transitioncreation
RotateTransition rt = new RotateTransition();
//Set the duration for the rotate transition created
rt.setDuration(Duration.millis(2000));
//Set the node for the rotate transition created
rt.setNode(hx);
//Set the angle of the rotate transition created
rt.setByAngle(360);
//Set the cycle count for rotate transition created
rt.setCycleCount(60);
//Set false as the auto reverse value
rt.setAutoReverse(false);
//start playing the animation
rt.play();
//Create a Group object
Group g = new Group(hx);
//Create a scene object
Scene sc = new Scene(g, 600, 300);
//Set title of the Stage st
st.setTitle("Animation example ");
//Add scene to the stage st
st.setScene(sc);
//Display the contents of the stage st
st.show();
}
//main method
public static void main(String args[]){
//launches the application
launch(args);
}
}
Output:
First, import all the necessary packages. Then, create a hexagon and add coordinates to the created hexagon. After this, fill color for the created hexagon. Once this is done, create a rotate transition and set the duration for the rotate transition created. Moreover, set the node, angle, cycle count also for the rotate transition created. Then, set false as the auto-reverse value and start playing the animation using the play() method. Next is to create a Group object and a scene object. Also, set the title for the created stage and add a scene to it. After this, display the contents of the stage st using the method show().
On executing the code, it can be seen that a hexagon of yellow color is rotating 360 degrees. This rotation will be for around 2000 seconds. The screenshots provided are taken at two times for showing that the hexagon is rotating.
Conclusion
In JavaFX, a package known as JavaFX. animation is used which consists of classes that help in animating the nodes. There are different animations like Fill Transition, Scale Transition, that are provided by this package. In this article, a detailed aspect such as constructors, methods, working, and examples of the animation package is explained in detail.
Recommended Articles
This is a guide to JavaFX Animation. Here we discuss Definitions, methods, How to create animation in JavaFX? and example with code implementation. You may also have a look at the following articles to learn more –