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 />

<strong>Java</strong>FX Input Control for <strong>Games</strong>: Using javafx.event Classes<br />

Because games are interactive by their very nature, let’s take a look at the javafx.event package next; it provides us<br />

with the Event superclass and its ActionEvent subclass, for handling ACTION events, such as UI element use and<br />

animation KeyFrame processing. Because you are going to be using ActionEvent in your <strong>Java</strong>FX games (applications),<br />

I am describing its class inheritance hierarchy here, as this will show you the origins of the Event class as well:<br />

<strong>Java</strong>.lang.Object<br />

> java.util.EventObject<br />

> javafx.event.Event<br />

> javafx.event.ActionEvent<br />

Your InvinciBagel game application is already using this ActionEvent class (object), with the EventHandler<br />

interface and its .handle() method, which you code yourself to tell your application what to do to “handle” the Event<br />

(ActionEvent) once it has occurred (fired). The .handle() method catches this fired event and then processes it,<br />

according to the <strong>Java</strong> programming logic inside the body of the .handle() method.<br />

A <strong>Java</strong> interface is a class that furnishes empty methods, which are declared for use but which do not yet<br />

contain any <strong>Java</strong> programming constructs. The unimplemented methods will, at the time of their use, have to be<br />

implemented by you, the <strong>Java</strong> programmer. This <strong>Java</strong> interface defines only the methods to be implemented (in this<br />

case, a method for handling the ActionEvent so that the event gets processed in some fashion). It is important to note<br />

that the <strong>Java</strong> interface defines the method that needs to be coded but does not write the method code for you, so it is a<br />

road map of what you must do to complete, or interface with, the programming structure that is in place (in this case,<br />

the <strong>Java</strong> programming structure for handling ActionEvent objects, that is, fired action events.<br />

Now, let’s take a look at multithreading in <strong>Java</strong>FX, which is another important concept for advanced games, to<br />

conclude this exploration of everything 2D- and 3D- (game) related in the <strong>Java</strong>FX API and package hierarchy.<br />

<strong>Java</strong>FX Thread Control for <strong>Games</strong>: javafx.concurrent Package<br />

<strong>Games</strong> require background, or asynchronous, processing. This can be done using additional threads besides<br />

the <strong>Java</strong>FX application thread, Prism rendering thread, and media playback thread, which are all automatically<br />

created for you, based on which classes (objects) you are using in your scene graph. Your application programming<br />

logic can spawn its own Worker threads for processing so that you do not overload the primary <strong>Java</strong>FX application<br />

thread. Let’s take a look at the javafx.concurrent package next, as it provides us with the Service superclass and its<br />

ScheduledService subclass, for creating Worker objects, as well as a Task class, used for one-off task processing.<br />

Because you are going to be using Service and ScheduledService in your <strong>Java</strong>FX games (applications), I am<br />

demonstrating the ScheduledService class inheritance hierarchy here, as this will show you the java.lang.Object<br />

origins of the Service classes as well:<br />

<strong>Java</strong>.lang.Object<br />

> javafx.concurrent.Service<br />

> javafx.concurrent.ScheduledService<br />

Whereas Task (class) objects are used only one time, to accomplish a given task, a service is ongoing, and a<br />

Service object and a ScheduledService (class) object can be reused, that is, they are ready to perform their service<br />

at any time. This is more appropriate for game play processing, as game play continues for long periods of time, and<br />

the assumption here is that the types of game logic processing involved will also need to be calculated as time goes on<br />

during game play, and not just one single time, as with the Task class (object).<br />

The Worker <strong>Java</strong> construct is actually an interface, and the Task, Service, and ScheduledService classes have<br />

been created for you, based on this Worker interface (which is more than can be said for the EventHandler interface,<br />

which you have to implement yourself!).<br />

www.it-ebooks.info<br />

97

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

Saved successfully!

Ooh no, something went wrong!