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

The Popup class can be used to create custom popup notifications, or even custom game components,
from scratch. Alternately, you can use the PopupControl class and its ContextMenu and Tooltip subclasses
to provide predefined (that is, precoded for your own implementation) JavaFX graphical user interface (GUI)
controls.
The DirectoryChooser and FileChooser classes provide support for passing through the standard
OS file selection and directory navigation dialogs into your JavaFX applications. The FileChooser.
ExtensionFilter nested class provides a utility for filtering the files that will come up in the FileChooser
dialog based on file type (file extension).
Next, let’s take your current JavaFXGame Application’s Stage object to the next level and show you how
to make your Java 9 (JavaFX 9) game a windowless (floating) application! This is one of the many impressive
features of JavaFX 9 that you can leverage in your Pro Java 9 Games Development pipeline.


Using the JavaFX Stage Object: Creating a Floating Windowless


Application


Let’s make the primaryStage Stage object, created by our .start(Stage primaryStage) method constructor,
for our JavaFXGame application transparent so that the HelloWorld Button (UI control) floats right on top of
your OS desktop (or in this case, on top of NetBeans 9). This is something that JavaFX can do that you don’t
see utilized very often, and it will allow you to create i3D games that will appear to “float” right on top of
your user’s OS desktop. For i3D virtual objects, at least on the Windows 7, 8, and 10 OS, this would be called
a “windowless ActiveX control.” Removing the window “chrome” or decorations should also be supported
in other advanced OSs such as Linux and Mac as well, and there is a program call to determine whether this
“remove everything except my content using alpha channel (transparency)” capability is in place, so you
can implement a fallback plan to a solid color or background image. This cool little trick (I thought I’d show
you something cool and powerful early on in the book) is accomplished in part by using the StageStyle.
TRANSPARENT constant, which you have just learned about, in conjunction with the .initStyle() method
from the Stage class. StageStyle is a “helper” class filled with stage (or OS window, ultimately) decoration
constants, one of which is TRANSPARENT. The fallback we’ll use is UNDECORATED (a normal OS window).


Adding a StageStyle Constant: Using the .initStyle(StageStyle style) Method


Call


As you can see in Figure 7-4, I have added a new line 26 in the Java 9 code (highlighted in light blue) and
typed in the primaryStage Stage object name; then I hit the period key to insert a Java dot chain to the
method that I want to use. At this point, NetBeans 9 will open a pop-up method selector helper dialog (more
of a chooser UI, actually); look for the .initStyle(StageStyle style) method, as shown in Figure 7-4. Clicking
the method will select it in blue, and double-clicking it will insert it into your code. We will do the same thing
for the parameter for the method next using the same work process of allowing (or enticing) NetBeans 9 into
doing the Java coding work for you.

Free download pdf