Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 10 ■ User InterfaCe DesIgn InteraCtIvIty: event hanDlIng anD ImagIng effeCts

This class also has one known direct subclass, the MediaMarkerEvent class. The class hierarchy structure
therefore looks like the following:


java.lang.Object



java.util.EventObject
javafx.event.Event
javafx.event.ActionEvent



The ActionEvent class is contained in the javafx.event package along with the EventHandler public
interface. An ActionEvent object, as you might have guessed, is an Event object that represents some type of
action. This type of Event object can be used to represent a wide variety of things. As you have seen, it is used
when a Button has been fired and is also used, for instance, when a KeyFrame has finished playing and in
other similar internal software usage. The ActionEvent was introduced to JavaFX in version 2.0 and was not
available in the JavaFX 1.x versions (1.0 to 1.3). It remains in JavaFX 7 for Java 7 (both now discontinued), in
JavaFX 8 for Java 8, and now in JavaFX 9 for Java 9.
There are two data fields (attributes) of an ActionEvent object. The first is a static
EventType ACTION characteristic, which is the only valid EventType for the
ActionEvent. There is also, however, a supertype for an ActionEvent object that takes the form of static
EventType ANY that provides developers with a common supertype that is able to represent
all action event types. Thus, if you want your Java code to process any ActionEvent object, use this data
field, and if you want your Java code to process specific ActionEvent objects, use the ACTION data field.
There are also two constructor methods supported by this ActionEvent class. The default empty
parameter list ActionEvent() constructor method creates a new ActionEvent object with the default event
type of ACTION. There is also an ActionEvent(Object source, EventTarget target) constructor method,
which will create the new ActionEvent with the specified event Object source and EventTarget target.
There are also two methods supported by this ActionEvent class. The first is an ActionEvent
copyFor(Object newSource, EventTarget newTarget) method, which is used to create and return a copy of
the event using a specified event source and target. The second is an EventType<? extends ActionEvent>
getEventType() method, which will get the event type for the event object that it has been called off of.
All the other event-related classes that we’ll be using for the i3D component of the gameplay are
contained in the javafx.scene.input package. I’m going to focus on the javafx.scene.input package for
the rest of this section, as you have already learned how to code your new EventHandler
{ ... } structure for Java 7. If you instruct NetBeans 9 to turn this into a Lambda Expression, it will take the
(ActionEvent) -> { ... } code structure format for Java 8.
Now it’s time to learn how to use other types of events, called input events, in your Java game
development work process. Let’s take a look at the javafx.scene.input package and its 25 input event-related
classes.


JavaFX Input Event Classes: The javafx.scene.input Package


Even though the java.util and javafx.event packages contain the core EventObject, Event, and
EventHandler classes that “handle” your events, at the foundational level of making sure that the events get
processed (handled), there is another JavaFX package called javafx.scene.input that contains the classes
that you’ll be interested in using to process (handle) your player’s input for the different types of games that
you might be creating. These are called input events, and they are different events than the ActionEvents and
pulse events, which you learned about already.
It’s interesting to note that a number of the input event types that are supported in the javafx.scene.
input package are more suited to consumer electronics (the industry term is embedded) devices such as
smartphones and tablets. This tells me that JavaFX is being positioned (designed) for use on open source
platforms, such as Android OS, Firefox OS, Tizen OS, Bada OS, Opera OS, Ubuntu OS, or Chrome OS. JavaFX 9
has “specialized” events, such as GestureEvent, SwipeEvent, TouchEvent, and ZoomEvent, that support

Free download pdf