28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4 ■ An Introduction to <strong>Java</strong>FX 8: Exploring the Capabilities of the <strong>Java</strong> 8 Multimedia Engine<br />

}<br />

@Override<br />

public void stop() {<br />

super.stop();<br />

}<br />

Next, let’s investigate the <strong>Java</strong>FX Stage class (object),which is passed into your InvinciBagel .start() method!<br />

<strong>Java</strong>FX Screen and Window Control: Using javafx.stage Classes<br />

The javafx.stage package contains classes that can be considered top level, in terms of the display for your <strong>Java</strong>FX<br />

application (in this case, a game). This display is at the top of the resulting game play, because it shows your game’s<br />

scenes to the end user of your application. Inside the Stage object are Scene objects, and inside these are Scene Graph<br />

Node objects, which contain the elements that make up an application.<br />

In contrast, the classes that are in this package could be considered fairly low level, from an OS perspective;<br />

these are the Stage, Screen, Window, WindowEvent, PopupWindow, Popup, DirectoryChooser, and FileChooser<br />

classes as well as the FileChooser.ExtensionFilter nested class. These classes can be used to interface with the<br />

device’s display hardware, and the OS software’s windowing management, file management, and directory (folder)<br />

management functionality.<br />

To get a description of the display hardware that is being used by the device that a <strong>Java</strong>FX application is running<br />

on, you will want to use the Screen class. This class supports multiscreen (commonly referred to as second screen)<br />

scenarios, using the .getScreens() method, which can access an ObservableList object that will contain a list (array)<br />

with all the currently available screens. A primary screen is accessed using the .getPrimary() method call. You<br />

can get the physical resolution for the primary screen hardware by using a .getDpi() method call. There are also<br />

.getBounds() and .getVisualBounds() method calls for usable resolution.<br />

The Window superclass, and its Stage and PopupWindow subclasses, can be used by the <strong>Java</strong>FX end user to<br />

interact with your application. As you saw in Chapter 3 (see Figure 3-2), this is done using the Stage object named<br />

primaryStage, which is passed into your .start() method, or using a PopupWindow (dialog, tool tip, context menu,<br />

notification, and so on) subclass, such as a Popup or PopupControl object.<br />

You can use the Stage class to create secondary stages within your <strong>Java</strong>FX application programming logic.<br />

A primary Stage object is always constructed by the <strong>Java</strong>FX platform, using the public void start(Stage primaryStage)<br />

method call, as you have already seen in Chapters 2 and 3 in the bootstrap <strong>Java</strong>FX application created by NetBeans.<br />

All <strong>Java</strong>FX Stage objects must be constructed using, and modified inside the primary <strong>Java</strong>FX application thread,<br />

which I discussed in the previous section. Because a stage equates to a window on the OS platform it is running<br />

on, certain attributes or properties are read-only, as they need to be controlled at the OS level; these are Boolean<br />

properties (variables): alwaysOnTop, fullScreen, iconified, and maximized.<br />

All Stage objects have a StageStyle attribute and a Modality attribute, which can be set using constants. The<br />

stageStyle constants are StageStyle.DECORATED, StageStyle.UNDECORATED, StageStyle.TRANSPARENT, and<br />

StageStyle.UTILITY. The Modality constants are Modality.NONE, Modality.APPLICATION_MODAL, and Modality.<br />

WINDOW_MODAL. In the next section, I will show you how to do something really impressive using the StageStyle<br />

attribute and the TRANSPARENT constant that will make your <strong>Java</strong>FX applications stand out from everyone else’s in<br />

the marketplace.<br />

The Popup class can be used to create custom pop-up notifications, and even custom game components, from<br />

scratch. Alternately, you can use the PopupControl class, and its ContextMenu and Tooltip subclasses, to provide<br />

these predefined (coded) <strong>Java</strong>FX UI controls.<br />

The DirectoryChooser and FileChooser classes give support for passing through the standard OS file selection<br />

and directory navigation dialogs into your <strong>Java</strong>FX applications. The FileChooser.ExtensionFilter nested class offers a<br />

utility for filtering the files that will come up in the FileChooser dialog, based on file type (file extension).<br />

Next, let’s take your current InvinciBagel Stage object to the next level and make it a windowless (floating)<br />

application. This is one of the impressive features of <strong>Java</strong>FX that cannot be matched by Flash or other game engines.<br />

www.it-ebooks.info<br />

89

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

Saved successfully!

Ooh no, something went wrong!