Definition of JavaFX AnimationTimer
In JavaFX, Animation is considered as a rapid series of images that produces a movement illusion. It is not the only movement, but the change of a node background as well. There are three tools for creating animation in JavaFX. They are Animation Timer, Transition, and Timeline. In this article, we will discuss an animation timer. It allows the creation of a timer that is called on each frame during the active time. Let us see more on animation timer in the below sections.
Declaration:
Below is the declaration of the animation timer:
public abstract class AnimationTimer extends Object
Constructors:
There is only one constructor in JavaFX animation timer.
- AnimationTimer(): This constructor will help in creating a timer.
Methods:
Let us see the important methods used in JavaFX animation timer.
1. Handle
Declaration:
public abstract void handle(long now)
Explanation:
- The method has to be overridden by class extending. After that, it will be called in all frame during the time when AnimationTimer is active.
Parameters:
The parameter of this method is now which is the current frame’s timestamp that is given in nanoseconds. It will be same for every timer called at a particular frame.
2. Start
Declaration:
public void start()
Explanation:
- This is the method which starts the AnimationTimers. After starting the animation timer, the method handle() of timer will be called in every frame. It can be stopped using stop().
3. Stop
Declaration:
public void stop()
Explanation:
- This is the method that stops the AnimationTimers. It can be restarted using start().
How to Use Animation Timer in JavaFX?
Before moving to the coding part, the underlying technique behind the timer has to be discussed. Then only it will be clear for you.
Animation timer is not considered as a timer that is in and of itself. It is working like a receiver. Each frame, an animation pulse is sent by JavaFX through the system that has marked with a similar timestamp. Classes like transition and timeline extend the class Animation. This helps in pausing the animation. Moreover, it offers support for animation support of bigger arrays. Support includes finding the animation duration, values interpolation, animation desired rate, setting delay, status of animation, properties linked with it, action event setting, cue pint setting, animation movement, etc.
The main advantage of JavaFX is that it has high information binding. However, the comprehensive support results in decreasing the performance of runtime.
As we know, the animation timer won’t time itself. The information source is the pulse of animation that links through the system each frame. This is the point where we bring up start and stop methods of animation timer. That is, clock should be stopped on the timer. Let us see what actually is happening with some sample programs.
Examples of JavaFX AnimationTimer
Given below are the examples mentioned:
Program #1
JavaFX program on animation timer that fades the text.
Code:
//import all the necessary packages
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
//main class
public class AnimationTimerSample extends Application {
//declare opacity and a label
private double op = 1;
private Label lbel;
@Override
public void start(Stage st) {
UIinit(st);
}
//define the UIinit method
private void UIinit(Stage st) {
//create a stackpane
StackPane sp = new StackPane();
//define the label
lbel = new Label("Animation Sample");
//set the font for label
lbel.setFont(Font.font(24));
//add the label to stack pane
sp.getChildren().add(lbel);
//declare an animation timer
AnimationTimer tm = new TimerMethod();
//start the timer
tm.start();
//create a scene
Scene sc = new Scene(sp, 310, 260);
//set the title for stage
st.setTitle("Animation Timer Sample");
//set the scene
st.setScene(sc);
//display the result
st.show();
}
private class TimerMethod extends AnimationTimer {
//define the handle method
@Override
public void handle(long now) {
//call the method
handlee();
}
//method handlee
private void handlee() {
//set the value of opacity
op -= 0.01;
//set the opacity for label
lbel.opacityProperty().set(op);
//checks the value of opacity
if (op <= 0)
{
//if it is less than zero, stop it
stop();
//print message
System.out.println("Animation stops here");
}
}
}
//main method
public static void main(String[] args) {
launch(args); } }
Output:
On executing the code, we can see that a text “Animation Sample” gets displayed and fades with time. Above displayed screenshots are the fading od text.
Program #2
JavaFX program on animation timer that shows dots.
Code:
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
//main class
public class AnimationTimerSample extends Application {
//declare count, node, angle and start
private static final int CNT = 3000;
private final Rectangle[] ND = new Rectangle[CNT];
private final double[] ang = new double[CNT];
private final long[] start = new long[CNT];
private final Random rnd = new Random();
@Override
public void start(final Stage st)
{
for (int i=0; i<CNT; i++)
{
ND[i] = new Rectangle(1, 1, Color.RED);
//angle
ang[i] = 3.0 * Math.PI * rnd.nextDouble();
//start
start[i] = rnd.nextInt(2000000000);
}
final Scene sc = new Scene(new Group(ND), 700, 500, Color.YELLOW);
//set the scene
st.setScene(sc);
//display the result
st.show();
//animation timer
new AnimationTimer() {
@Override
//handle method
public void handle(long now) {
//width
final double w = 0.6 * st.getWidth();
//height
final double h = 0.6 * st.getHeight();
//radius
final double r = Math.sqrt(2) * Math.max(w, h);
//loop
for (int i=0; i<CNT; i++)
{
//node
final Node nd = ND[i];
//angle
final double ag = ang[i];
final long tm = (now - start[i]) % 2000000000;
final double dt = tm * r / 2000000000.0;
nd.setTranslateX(Math.cos(ag) * dt + w);
nd.setTranslateY(Math.sin(ag) * dt + h);
}
}
}.start();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
On executing the code, lot of dots can be seen in an animated way. Above displayed screenshot is the same for this.
Conclusion
Animation timer allows the creation of a timer which is called on each frame during the active time. In this article, a different aspect of animation timer is discussed in detail.
Recommended Articles
This is a guide to JavaFX AnimationTimer. Here we discuss the introduction, how to use animation timer in JavaFX and different examples. You may also have a look at the following articles to learn more –