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 Transition class is a public abstract class, and as such, it can only be utilized (subclassed or
extended) to create transition subclasses. In fact, there are ten of these subclasses that have already been
created for you to use, to create your own transition special effects. These include your SequentialTransition,
FadeTransition, FillTransition, PathTransition, PauseTransition, RotateTransition, ScaleTransition,
TranslateTransition, ParallelTransition, and StrokeTransition classes. The Java 9 class inheritance hierarchy
for these subclasses would look similar to the following:



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



As a subclass of Animation, the Transition class contains all the functionality of Animation. Chances
are you will end up using the ten custom transition classes directly, since they provide the different types
of transitions you are likely to want to use (fades, fills, path based, stroke based, rotate, scale, movement or
translate, etc.). We’ll be learning how to use some of these as the book progresses, so I’m going to move on to
the AnimationTimer class.


The JavaFX AnimationTimer Class: Frame Processing, Nanoseconds,


and Pulse


The JavaFX AnimationTimer class is not a subclass of the JavaFX Animation superclass, so its inheritance
hierarchy would look like the following; it starts with the Java master class called java.lang.Object and ends
with AnimationTimer:



java.lang.Object
javafx.animation.AnimationTimer



This means the AnimationTimer class was scratch-coded specifically to provide AnimationTimer
functionality to JavaFX and that it is not related to the Animation (or Timeline or Transition) class or
subclasses in any way. For this reason, the name of this class might be somewhat misleading if you are
mentally grouping it in with the Animation, Interpolator, KeyFrame, and KeyValue classes that occupy the
javafx.animation package with it; it has no relation to these classes whatsoever! This class allows you to
implement your own animation (or game engine) timer and scratch-code everything yourself! I showed how
to do this for i2D games in Beginning Java 8 Games Development.
This AnimationTimer class has also been declared to be a public abstract class, just like the Transition
class. Since it’s an abstract class, it can be utilized (subclassed or extended) only to create AnimationTimer
subclasses. Unlike the Transition class, it has no subclasses that have been created for you; you have to
create your own AnimationTimer subclasses from scratch.
The AnimationTimer class is deceptively simple, in that it has only one method that you must
“override,” or replace, which is contained in the public abstract class: the .handle() method. This method
contains the programming logic that you want to have executed on every frame of the JavaFX engine’s stage
and scene processing cycle, which is optimized to play at 60 FPS (60 frames per second), which just happens
to be perfect for games. JavaFX uses a pulse system, which is based on the new Java nanosecond unit of
time (versions previous to Java 7 used milliseconds).

Free download pdf