Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 7 ■ IntroduCtIon to JavaFX 9: overvIew oF the JavaFX new MedIa engIne


The JavaFX Timeline Class: An Animation Subclass for JavaFX Properties


Timeline Management


The JavaFX Timeline class is a subclass of the JavaFX Animation superclass, so its inheritance hierarchy
would look like the following, starting with the Java masterclass java.lang.Object and progressing on
downward to the Timeline class:



java.lang.Object
javafx.animation.Animation
javafx.animation.Timeline



Timeline objects can be used to define a special kind of Animation object that is comprised, or made up
of, JavaFX values (properties) of object type WritableValue. All JavaFX properties are of type WritableValue,
so this class can be used to animate anything in JavaFX, which means what you can do with it is limited only
to your imagination.
Timeline animations are defined using KeyFrame objects, created via the KeyFrame class mentioned
earlier. This KeyFrame class, not surprisingly, allows you to create and manage the KeyFrame objects that
live inside the Timeline object. Those familiar with animation know keyframes set the different interpolated
data values for different points in the animation of an object or data value to create smooth movements.
KeyFrame objects will always be processed by Timeline objects according to a time variable (accessed
using KeyFrame.time) and by properties to be animated, which are defined using the KeyFrame object’s
values and accessed using the KeyFrame.values variable.
It is important to note that you need to set up your KeyFrame objects before you start running the
Timeline object, as you cannot change a KeyFrame object within a running Timeline object. This is because
it has been put into system memory once it has been started. If you wanted to change a KeyFrame object in
a running Timeline object in any way, first stop the Timeline object, then make the change to the KeyFrame,
and then restart the Timeline object. This will reload the Timeline object and its revised KeyFrame objects
into memory along with their new values.
An Interpolator class, which you will be using during the book, interpolates these KeyFrame objects in
your Timeline object based on the Timeline direction. Interpolation is a process of creating in-between, or
“tween,” frames based on the beginning and ending values. In case you’re wondering how the direction is
inferred, it is kept in the rate property and the read-only currentRate property of the Animation superclass,
which is part of the extended Timeline subclass.
Inverting the value of the rate property (i.e., making it negative) will reverse (toggle) the playback
direction, and the same principle would hold when reading the currentRate property (the negative value
signifies the reverse, or backward, direction). Finally, a KeyValue class (object) is used to hold the data
values inside of each KeyFrame object. A KeyFrame object stores multiple (as many as needed) KeyValue
objects, using one KeyValue object per data value.


The JavaFX Transition Class: Animation Subclass for Transitions and


Special Effects Application


The JavaFX Transition class is a subclass of the JavaFX Animation superclass, so its inheritance hierarchy
would look like the following, starting with a Java master class called java.lang.Object and progressing
downward to a Transition class:



java.lang.Object
javafx.animation.Animation
javafx.animation.Transition


Free download pdf