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 6 ■ The Foundation of Game Design: The <strong>Java</strong>FX Scene Graph and the InvinciBagel Game Infrastructure<br />

The <strong>Java</strong> class hierarchy for the Image class starts with the java.lang.Object master class and uses this class<br />

to create the javafx.scene.image.Image class. As Figure 6-4 shows (l. 9), Image is contained in the <strong>Java</strong>FX image<br />

package, just like the ImageView class, and uses the following class hierarchy structure:<br />

java.lang.Object<br />

> javafx.scene.image.Image<br />

The Image class supplies six different (overloaded) Image() constructor methods. These take anything from<br />

a simple URL to a set of parameter values specifying the URL, width, height, aspectRatioLock, smoothing, and<br />

preload options. These should be specified in that order within the constructor method, as you will soon see, when<br />

you write an Image() constructor using the most complicated of all the constructor methods, which has the following<br />

format:<br />

Image(String url, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean<br />

smooth, boolean backgroundLoading)<br />

The simple constructor for an Image object specifies only the URL and uses the following format:<br />

Image(String url)<br />

If you want to load an image and also have the constructor method scale the image to a different width and<br />

height (usually, this is smaller, to save memory), while locking (preserving) the aspect ratio, using the highest-quality<br />

resampling (smooth-pixel scaling), that Image object constructor uses the following format:<br />

Image(String url, double scaleWidth, double scaleHeight, boolean preserveAspect, boolean smooth)<br />

If you want to load an image in the background (asynchronously), using its “native,” or physical, resolution and<br />

native aspect ratio, the Image() constructor uses the following format:<br />

Image(String url, boolean backgroundLoading)<br />

Two Image() constructor methods also use the java.io.InputStream class, which furnishes a real-time stream<br />

(like streaming video or audio, only customized for a <strong>Java</strong> application) of input data to the Image() constructor<br />

method. These two Image object constructor formats take the following formats (simple and complex):<br />

Image(InputStream is) // This is the simple format. The complex format would thus be as follows:<br />

Image(InputStream is, double newWidth, double newHeight, boolean preserveAspect, boolean smooth)<br />

Therefore, the Image class (object) is used to prepare a digital image asset for use, that is, to read its data<br />

from a URL; resize them, if necessary (using whatever smoothing and aspect ratio lock you like); and load them<br />

asynchronously, while other things are going on in your application. It is important to note that the Image class<br />

(or object) does not display an image asset: the Image class just loads it; scales it, if needed; and places it in system<br />

memory to be used in your application.<br />

To display an Image object, you will need to use a second class (object), called an ImageView class. The<br />

ImageView object can be used as a node on your Scene Graph and references and then “paints” the Image object data<br />

onto the layout container, which holds the ImageView node (in this case, a StackPane Scene Graph root and Parent<br />

node to the leaf ImageView node). I will be covering the ImageView class in the next section.<br />

From a digital image–compositing perspective, the StackPane class (object) is the image-compositing engine,<br />

or layer manager, if you will, and each ImageView object represents an individual layer in the layer stack. An Image<br />

object contains the digital image data in the ImageView layer or in more than one ImageView, if needed, as the Image<br />

objects and the ImageView objects are decoupled and exist independently of each other.<br />

www.it-ebooks.info<br />

133

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

Saved successfully!

Ooh no, something went wrong!