28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 ■ The Foundation of Game Design: The <strong>Java</strong>FX Scene Graph and the InvinciBagel Game Infrastructure<br />

<strong>Java</strong>FX ImageView Class: Displaying Digital Images in a Design<br />

The ImageView class is a public class that directly extends the javafx.scene.Node superclass, which is an extension<br />

of the java.lang.Object (see Chapter 4). The ImageView object is therefore a type of Node object in the <strong>Java</strong>FX Scene<br />

Graph that is used for painting a view, using the data contained in an Image object. The class has methods that allow<br />

image resampling (resizing), and, as with the Image class, you can lock the aspect ratio for scaling as well as specify<br />

the resampling algorithm (smoothing quality).<br />

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

to create the javafx.scene.Node class, which is then employed to create an ImageView Node subclass. As Figure 6-4<br />

illustrates (l. 10), like the Image class, ImageView is contained in the <strong>Java</strong>FX image package. The ImageView class uses<br />

the following <strong>Java</strong> class inheritance hierarchy structure:<br />

java.lang.Object<br />

> javafx.scene.Node<br />

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

The ImageView class provides three different (overloaded) ImageView() constructor methods. These range from<br />

an empty constructor (which is the one you are going to use later on, in your code); to one that takes an Image object<br />

as its parameter; to one that takes a URL String object as the parameter and creates the Image object automatically.<br />

To create an ImageView object, the simple (empty) ImageView() constructor method uses the following format:<br />

ImageView()<br />

You will be employing this constructor method so that I can show you how to use the .setImage() method call to<br />

load an Image object into an ImageView object. If you want to avoid using the .setImage() method call, you can use<br />

the overloaded constructor method, which has the following format:<br />

ImageView(Image image)<br />

So, to set up an ImageView “explicitly” and wire it to the Image object looks like this:<br />

splashScreenBackplate = new ImageView();<br />

splashScreenBackplate.setImage(splashScreen);<br />

// This uses the empty constructor method approach<br />

You can condense this into one line of code, using an overloaded constructor method, structured as follows:<br />

splashScreenBackplate = new ImageView(splashScreen); // using the overloaded constructor method<br />

If you want to bypass the process of creating and loading an Image object, there is a constructor method for that<br />

as well, which uses the following format:<br />

ImageView(String url)<br />

To load an image in the background (asynchronously), using its native (default) resolution and native aspect<br />

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

splashScreen = new Image("/invincibagelsplash.png", 640, 400, true, false, true);<br />

splashScreenBackplate = new ImageView();<br />

splashScreenBackplate.setImage(splashScreen); // uses the empty constructor method approach<br />

134<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!