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


If you wanted to load an image using its “native” or “physical” (default) resolution and native aspect
ratio and have it load the image in the background (asynchronously), you would use the following format
for the Image() constructor:


Image(String url, boolean backgroundLoading)


There are also two Image() constructor methods that use the java.io.InputStream class. This class
provides a lower-level Java input stream of input data to the Image() constructor method. Generally,
you’ll use a URL to reference your digital image files. These two Image object constructor formats take the
following formats. The simple format is as follows:


Image(InputStream is)


The complex InputStream constructor method allows you to specify the width, height, aspect ratio lock,
and image scaling interpolation smoothing algorithm (on/true or off/false). The second format will look like
the following:


Image(InputStream is, double newWidth, double newHeight, boolean preserveAspect, boolean
smooth)


The Image class (object) is thus used to prepare a digital image asset for use, that is, to read its data
from a URL, resize it if necessary (using whatever smoothing and aspect ratio lock you like), and even load
it asynchronously while other things are going on within the application. It is important to note that this
Image class (or object) does not display your image asset; it just loads it, scales it if needed, and places it into
system memory, to be used in your app.
To display an Image object, you’ll need to utilize a second class (object), called an ImageView, which we
are going to cover in the next section of this chapter. This ImageView object is implemented as a leaf Node in
your Scene Graph and references and then “paints” your Image object data onto the layout container, which
contains this ImageView Node. In our case, this is the uiLayout StackPane Parent (or branch) Node above
the leaf ImageView Node.
From a digital image compositing perspective, the StackPane class (object) is the layer compositing
engine, or the layer manager if you will, and the ImageView object represents one single digital image layer
in the layer stack. An Image object contains the digital image data that is displayed inside of the ImageView
layer or in more than one ImageView, if that is required, since the Image objects and the ImageView objects
are decoupled and therefore exist independently of each other. I am trying to minimize Scene Graph Node
use, so I’m using one ImageView image plate and one text information compositing plate to create the user
interface screens and then using code to switch them.


JavaFX ImageView Class: Display Digital Images in Your Design


The ImageView class is a public class that directly extends the javafx.scene.Node superclass, which is an
extension of the java.lang.Object master class. The ImageView object is therefore a type of Node object in the
JavaFX Scene Graph that is used for painting a graphic viewport using the data contained in an Image object.
The class has methods that allow image resampling (resizing), and like with the Image class, you can lock
aspect ratio for scaling, as well as specify the resampling algorithm (the smoothing quality, through the use
of pixel interpolation).
As you can see in line 24 of your Java code, shown in Figure 8-6, you will be using an ImageView object
named boardGameBackPlate to display your Image object data. This ImageView class, like your Image
class, is also contained in the javafx.scene.image package. The Java class hierarchy for the ImageView class
starts out with the java.lang.Object master class and uses this class to create a javafx.scene.Node class,

Free download pdf