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

which is then used to create the javafx.scene.image.ImageView Node subclass. An ImageView class uses the
following Java class inheritance hierarchy:


java.lang.Object



javafx.scene.Node
javafx.scene.image.ImageView



The ImageView class provides three different (overloaded) ImageView() constructor methods. These
range from the empty ImageView constructor (which is the one you are going to use later in your code) to
one that takes an Image object as its parameter to one that takes a URL String object as the parameter and
creates the Image object automatically. The simplest, empty parameter list ImageView() constructor method
will create an (empty) ImageView object (that is, one with no Image object to display but can hold Image
objects). It will use this following format:


ImageView()


We will be using this constructor method so that I can show you how to use the .setImage() method call
to load your Image object into an ImageView object. If you wanted to avoid using the .setImage() method
call, you could use another overloaded constructor method. That ImageView object constructor would use
this following format:


ImageView(Image image)


So, the way that I’m going to explicitly set up an ImageView and wire it to the Image object will look like this:

boardGameBackPlate = new ImageView(); // This uses empty constructor method approach
boardGameBackPlate.setImage(splashScreen);


This could be condensed into one line of code using an overloaded constructor method, structured
like this:


boardGameBackPlate = new ImageView(splashScreen); // using the overloaded constructor method


If you also want to bypass the process of creating and loading an Image object, there is another
constructor method for that as well, which uses the following format:


ImageView(String url)


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), the Image() constructor would use
the following format:


backPlate = new Image("/backplate8.png", 1280, 640, true, false, true);
boardGameBackplate = new ImageView();
boardGameBackplate.setImage(backPlate); // use empty ImageView constructor method approach


If you didn’t want to specify the image dimensions, background image loading, or smooth scaling and
you wanted to lock the aspect ratio for any scaling, you could condense the previous three lines of Java code
into the one following constructor:


boardGameBackPlate = new ImageView("/backplate8.png"); // uses third constructor method

Free download pdf