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


We will be creating animation code later in the book after we learn the basics about Java 9, NetBeans 9,
JavaFX 9, and SceneGraph (Chapter 8 ). The code examples in this chapter are just examples to show you how
these JavaFX animation implementations would be accomplished. Next, let’s take a look at the JavaFX Stage
classes, where I will actually show you some code to make your JavaFX environment transparent so your games
can float over the OS desktop, an effect in Windows called a “Windowless ActiveX Control,” which allows you to
create virtual i3D objects.


JavaFX Screen and Window Control: Using javafx.stage Classes


The javafx.stage package contains classes that can be considered to be “top level” where the display that
your JavaFX application uses. In your use case, this is pro Java 9 games. This stage is at the “top” of your
resulting gameplay, because it shows your game’s scenes to the end user of your application. Inside of your
Stage object you have Scene objects, and inside of these are SceneGraph Node objects, which contain the
elements that comprise a Java 9 game or a Java 9 IoT application. Thus, a JavaFX Stage object is the highest-
level object you’ll be using in your Java 9 game.
The classes that are in this package, on the other hand, could be considered to provide low-
level services, from an operating system’s perspective. These would include Stage, Screen, Window,
WindowEvent, PopupWindow, Popup, DirectoryChooser, and FileChooser, as well as the FileChooser.
ExtensionFilter nested class. These classes will be utilized to interface with device display hardware,
operating system windowing management, file management, and directory (folder) management
functionality. This is because a Stage class (object) asks the OS for these features, rather than actually
implementing them using Java or JavaFX APIs, so the OS is actually spawning these services at the request of
your Java 9 game or Java 9 IoT application’s request for the OS to provide these OS front-end utilities.
The Screen class is what you will want to use if you want to get a description of the display hardware
that is being used by the hardware device that a JavaFX application is running on. This class supports
multiscreen (second screen is the common industry term) scenarios by providing a .getScreens() method
that can access ObservableList objects (a list object that allows listeners to track changes when they occur),
which will contain a list array containing all of the currently available screens. There is a “primary” screen
that is accessed using the .getPrimary() method call. You can get the physical resolution for the current
screen hardware by using a .getDpi() method call. There are also .getBounds() and .getVisualBounds()
method calls for usable resolution.
The Window superclass, and its Stage and PopupWindow subclasses, can be used by the JavaFX
end user to interact with your application. This is done using the Stage object named primaryStage that is
passed into your .start() method (see Figure 5-2) or using a PopupWindow (dialog, tooltip, context menu,
notification, etc.) subclass, such as a Popup or PopupControl object.
You can use the Stage class to create secondary stages within your JavaFX application programming
logic. A primary Stage object is always constructed by the JavaFX platform, using the public void
start(Stage primaryStage) method call (as you have seen already in Chapter 6 in your bootstrap JavaFX
9 application created by NetBeans 9). All JavaFX Stage objects must be constructed using, and modified
inside of, the primary JavaFX Application Thread, which I talked about earlier, when we looked at pulse
event processing. Since a Stage equates to a window on the operating system platform it is running on,
certain attributes, or properties, are read-only and need to be controlled at your OS level. These are boolean
properties (variables), and they include alwaysOnTop, fullScreen, iconified, and maximized.
All Stage objects have a StageStyle attribute and a Modality attribute, which can be set using constants.
The stageStyle constants include the StageStyle.DECORATED, StageStyle.UNDECORATED, StageStyle.
TRANSPARENT, StageStyle.UNIFIED, and StageStyle.UTILITY constants. The Modality constants include
the Modality.NONE, Modality.APPLICATION_MODAL, and Modality.WINDOW_MODAL constants. After
we finish discussing the javafx.stage package, in the next section, I will show you how to do something really
impressive using this StageStyle attribute and the TRANSPARENT constant that will set your JavaFX-based
Java 9 games and IoT applications far apart from everyone else’s in the marketplace.

Free download pdf