28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4 ■ An Introduction to <strong>Java</strong>FX 8: Exploring the Capabilities of the <strong>Java</strong> 8 Multimedia Engine<br />

Chances are you will end up using the ten custom transition classes directly, as they provide the different types<br />

of transitions you are likely to want to use (fades, fills, path based, stroke based, rotate, scale, movement, and so on).<br />

I am going to move on to the AnimationTimer class next, as we will be using this class for our game engine during<br />

the book.<br />

The <strong>Java</strong>FX AnimationTimer Class: Frame Processing, Nanoseconds, and Pulse<br />

The <strong>Java</strong>FX AnimationTimer class is not a subclass of the <strong>Java</strong>FX Animation superclass, so its inheritance hierarchy<br />

starts with the <strong>Java</strong> 8 masterclass, java.lang.Object, and looks like this:<br />

> java.lang.Object<br />

> javafx.animation.AnimationTimer<br />

What this means is that the AnimationTimer class is scratch coded specifically to offer AnimationTimer<br />

functionality to <strong>Java</strong>FX and that it is not related to the Animation (or Timeline or Transition) class or subclasses in any<br />

way. For this reason, the name of this class may be somewhat misleading if you are mentally grouping the class in with<br />

the Animation, Interpolator, KeyFrame, and KeyValue classes that occupy the javafx.animation package with it, as is<br />

has no relation to these classes whatsoever!<br />

Like the Transition class, the AnimationTimer class has been declared a public abstract class. Because it is<br />

an abstract class, it can only be used (subclassed or extended) to create AnimationTimer subclasses. Unlike the<br />

Transition class, it has no subclasses that have been created for you; you have to create your own AnimationTimer<br />

subclasses from scratch, which we will be doing later on in the book to create our GamePlayLoop.java class.<br />

The AnimationTimer class is deceptively simple, in that it has only one method that you must override or replace,<br />

contained in the public abstract class: the .handle() method. This method provides the programming logic that you<br />

want to have executed on every frame of the <strong>Java</strong>FX engine’s stage and scene processing cycle, which is optimized to<br />

play at 60FPS (this is perfect for games). <strong>Java</strong>FX uses a pulse system, which is based on the new <strong>Java</strong> 8 nanosecond<br />

unit of time (previous versions of <strong>Java</strong> used milliseconds).<br />

<strong>Java</strong>FX Pulse Synchronization: Asynchronous Processing for Scene<br />

Graph Elements<br />

A <strong>Java</strong>FX pulse is a type of synchronization (timing) event, one that synchronizes the states of the elements that are<br />

contained within any given Scene Graph structure that you create for your <strong>Java</strong>FX applications (games). The pulse<br />

system in <strong>Java</strong>FX is administered by the Glass Windowing Toolkit. Pulse uses high-resolution (nanosecond) timers,<br />

which are also available to <strong>Java</strong> programmers using the System.nanoTime() method, as of the <strong>Java</strong> 8 API.<br />

The pulse management system in <strong>Java</strong>FX is capped or throttled to 60FPS. This is so that all the <strong>Java</strong>FX threads<br />

have the “processing headroom” to do what they need to do. A <strong>Java</strong>FX application will automatically spawn up to<br />

three threads, based on what you are doing in your application logic. A basic business application will probably only<br />

use the primary <strong>Java</strong>FX thread, but a 3D game will also spawn the Prism rendering thread, and if that game uses<br />

audio or video, or both, which it usually will, it will spawn a media playback thread as well.<br />

You will be using audio, 2D, 3D, and possibly video in the course of your game development journey, so your<br />

<strong>Java</strong>FX game application will certainly be multithreaded! As you will see, <strong>Java</strong>FX has been designed to be able to<br />

create games that take advantage of multithreading and nanosecond timing capabilities and 3D rendering hardware<br />

(Prism) support.<br />

Whenever something is changed in the Scene Graph, such as a UI control positioning, a CSS style definition, or<br />

an animation playing, a pulse event is scheduled and is eventually fired to synchronize the states of elements on the<br />

Scene Graph. The trick in <strong>Java</strong>FX game design is to optimize pulse events so that they are focusing on the game play<br />

logic (animation, collision detection, and so on); thus, you will minimize the other changes (UI control location, CSS<br />

style changes, and so on) the pulse engine looks at. You will do this by using the Scene Graph as a fixed design system,<br />

meaning that you will use the Scene Graph to design your game structure but will not manipulate nodes in real time<br />

on the Scene Graph, using dynamic programming logic, as the pulse system will perform the updates.<br />

87

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!