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


Next, let’s use the Background Image capability of the StackPane. Currently we’re using the EMPTY
constant from the Background class, so let’s replace that with a BackgroundImage object, which your
Background class will also support. Let’s take a look at how to wire this up next so we can optimize your
SceneGraph further by using an unused feature (a StackPane background) rather than by adding another
ImageView object, which most will be inclined to do.


Using StackPane Background: Leverage All Compositing Layers


The JavaFX StackPane class supports a .setBackground(Background background) method call, which,
in turn, supports a BackgroundImage object, which can be loaded with an Image object or used with the
EMPTY constant. This means you can reference an image asset in the background of a StackPane UI layout
container object, so let’s take a look at how to use this to your advantage so that you have five compositing
layers (Stage background, Scene background, ImageView, TextFlow, StackPane) you can utilize, using only
the nodes you have added to the Scene Graph. All these are currently set to EMPTY or TRANSPARENT
or contain a PNG32 with alpha. Add the backPlate and alphaLogo object names to your existing Image
declaration compound Java statement at the top of the class, using the following code:


Image splashScreen, helpLayer, legalLayer, creditLayer, scoreLayer, backPlate, alphaLogo;


Next, declare a BackgroundImage object named uiBackgroundImage at the top of your class and use
Alt+Enter to have NetBeans 9 write your import statement for you. Next, add a Background object named
uiBackground (notice that the Background class was already imported during Chapter 6 , so you can utilize
the Background.EMPTY constant) at the top of your JavaFXGame class, as shown in the following Java code,
as well as highlighted in Figure 9-12:


BackgroundImage uiBackgroundImage; // Object Declaration at the top of the JavaFXGame class
Background uiBackground; // Object Declaration at the top of the JavaFXGame class

Free download pdf