Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 9 ■ JavaFX 9 User InterFaCe DesIgn: the Front enD For Java 9 game DesIgn

JavaFX 9 UI Compositing: ImageView and TextFlow


Next, let’s take a look at the primary JavaFX classes that can be used to create the basic compositing pipeline
for the game splash screen and text information screens that will be next to (and underneath) the UI Button
bank that you created during the previous chapter. The game instructions, high scores, and legal and credits
screens will essentially be text (held inside your TextFlow object) composited over background imagery
(held in an ImageView object). The splash screen will be associated with the Start Game Button and will
display on launch of the game application; it will become invisible when the Start Game Button is pressed.
This is because the StackPane UI construct is on a higher-level z-order than the root Group and gameBoard
Group Node objects, which are above it on the Scene Graph. This means that anything that is opaque in the
StackPane will overlay (block from view) the i3D gameBoard Group that is directly underneath the Scene
Graph root, as shown in Figure 8-3. Let’s take a look at the Image class first.


JavaFX Image Class: Referencing Digital Imagery in Your Design


The Image class is a public class that directly extends the java.lang.Object master class, meaning that the
Image class was also “scratch-coded” to provide image loading (referencing) and scaling (resizing). You
can lock the aspect ratio for scaling and specify the scaling (algorithm) quality as well. All URLs that are
supported by the java.net.URL class are supported. This means you can load images from the Internet
(www.domainname.com/imagename.png), from the OS file system (file:imagename.png), or from your JAR file
using a forward slash character (/imagename.png).
The JavaFX Image class is part of the javafx.scene.image package. The class hierarchy for the JavaFX
Image class originates with the java.lang.Object master class and uses the following Java class hierarchy:


java.lang.Object



javafx.scene.image.Image



The Image class provides six different (overloaded) Image() constructor methods. These take anything
from a simple URL to a set of parameter values specifying the URL, width, height, aspectRatioLock,
smoothing, and preload options. These should be specified in this order within your constructor method.
You’ll see this soon when you code an Image() constructor using the most complicated of all of these
constructor methods, which uses the following format:


Image(String url, double requestedWidth, double requestedHeight,
boolean preserveRatio, boolean smooth, boolean backgroundLoading)


The simplest constructor for an Image object specifies only the URL and would use the following
format :


Image(String url)


If you wanted to load the image and also have the constructor method scale the image to a different
width and height (usually this would be smaller, for better quality) using the highest-quality resampling
(smooth pixel scaling) while also locking (preserving) the aspect ratio, you would utilize the following format
for the Image object constructor:


Image(String url, double scaleWidth, double scaleHeight, boolean preserveAspect, boolean
smooth)

Free download pdf