In this lab, we are going to talk about how to create animations in JavaFX, this will not only allow you to create simple animations but to do updates in certain intervals and more.
The animation class is an abstract class that defines common things for all animations, for example:
To create an animation, we will use one of the following classes.
As the name indicates that the class create an animation that moves something over a path, so let's create a path and animate something over it.
Notice that polyline takes an array of doubles, each two values indicate a point (x, y).
Polyline Output
We will move a circle over the polyline, so we will create a circle shape.
now by adding the polyline and circle to a Pane, we could run our scene.
Path Transition Example
This class will allow us to control the opacity of an object, in this example, we will create a button that fades in when the mouse entered over it and fades out when it exited.
Fade transition Example
The Timeline class allows you to create anything you like, you will create a KeyFrame (which is a frame with a time), and add all the keyframes to a Timeline which extends the Animation class.
The KeyFrame will take the duration of the frame, onFinish handler, and KeyValues.
In the following example, we will create a bouncing ball, we will first create a ball class:
Then we will create a Pane to contain all the balls, and animation to run the move
method.
Timeline Example
Notice that in the KeyFrame finish event, we could write any code we want, the KeyFrame could also contain KeyValues, which will control a certain value.
Programming
Java
IUG