Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 10 ■ User InterfaCe DesIgn InteraCtIvIty: event hanDlIng anD ImagIng effeCts


Java and JavaFX packages, classes, interfaces, and methods is the different types of input hardware events
that can be handled for pro Java games. These can be generated using the arrow keys on your iTV remote
or smartphone DPAD, to your keyboard, to your mouse or trackball, and to the touchscreen on your
smartphone and tablet or iTV set. There is also custom input hardware, including game controllers on game
consoles and now on iTV sets, gyroscopes and accelerometers in smartphones and tablets, and freeform
hand gesture and motion controllers such as the Leap Motion, VR Glove, and Razer Hydra Portal.


Types of Controllers: What Types of Events Should We Handle?


One of the key things to look at is what is the most logical approach to supporting gameplay-related events,
such as arrow keys, mouse clicks, touchscreen events, game controller buttons (A, B, C, and D), and more
advanced controllers, such as gyroscopes and accelerometers available on Android, Kindle, Tizen, HTML5
OS, and iOS consumer electronics devices. This decision will be driven by the hardware devices that a game
is targeted to run on; if a game needs to run everywhere, then code for handling different event types, and
even different programming approaches to event handling, will ultimately be required. We will be taking a
closer look at what input events are currently supported in Java and JavaFX during this section of the chapter
to give you an overview for your game development.
It is also interesting to note that Java and JavaFX apps can already be run on two popular embedded
platforms, Android and iOS, and I would put money on native support on open source platforms (Opera,
Tizen, Chrome, Ubuntu, and Firefox) and proprietary platforms that currently support Java 8 or 9 technology
(Windows, Samsung Bada, RIM Blackberry, LG WebOS, OpenSolaris) at some point in the near future. The
future of Java 9 is bright, thanks to JavaFX, the momentum of the Java platform over several decades, and
new advanced i3D hardware platform support!


Java and JavaFX Event Packages: java.util and javafx.event


As you have seen in your event handling structure’s new EventHandler declaration, the
javafx.event package’s EventHandler public interface, which extends the java.util package’s EventListener
interface, is the way that Event objects are created and handled, either using an anonymous inner class
(Java 7) structure, which we are using as it is compatible with Android, or using a lambda expression (Java 8).
You have become familiar now with how to code this type of event handling structure, and I will continue
during this book to code methods using the Java anonymous inner class approach. That said, you can mouse
over the wavy yellow underline highlight under any Java 7 code and have NetBeans 9 convert it to use a more
streamlined Java 8 lambda expression. In this way, you can create games that are compatible with Java 7
(64-bit Android 5 and 6), Java 8 (64-bit Android 7 and 8), and Java 9 (PC OSs and future versions of Android)
game code delivery pipelines. In this section, we will look at ActionEvent and InputEvent EventObject
subclass categories so that you have an understanding of what the major events are in JavaFX. These come
from the java.util.EventObject superclass, and we will take a look at how they would be applied to handling
actions, keystrokes, mouse events, touch events, and similar advanced input event types.


JavaFX ActionEvent Class: Created from the java.util.EventObject Superclass


The ActionEvent class (and objects) that you’ve used thus far during the book for your user interface Button
control event handling is a subclass of the javafx.event package’s Event superclass, which is itself a subclass
of the java.util package’s EventObject superclass, which is a subclass of the java.lang.Object master class.

Free download pdf