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 />

Adding Nodes to the Scene Graph: .addStackPaneNodes()<br />

Finally, you have to create a method that will add the Node objects that you have created to the Scene Graph root,<br />

which, in this case, is a StackPane object. You will use the .getChildren().add() method chain to add the children Node<br />

objects to the parent StackPane root Scene Graph node. This is done via three simple lines of <strong>Java</strong> code, like this:<br />

root.getChildren().add(splashScreenBackplate);<br />

root.getChildren().add(splashScreenTextArea);<br />

root.getChildren().add(buttonContainer);<br />

As you can see in Figure 6-8, the <strong>Java</strong> code is error free, and the root object sees its declaration at the top of the<br />

class. Clicking the root object in the code creates this highlighting, which traces the use of the object through the code.<br />

This is a pretty cool NetBeans 8.0 trick that you should use whenever you want to track an object in code.<br />

Figure 6-8. Coding the addNodesToStackPane() method, using the .getChildren() method chained to the .add()<br />

method<br />

The important thing to note here is the order in which the Node objects were added to the StackPane root<br />

Scene Graph object. This affects the compositing layer order for the image compositing, as well as for the UI element<br />

compositing, on top of these digital image elements. The first node added to the StackPane will be on the bottom of<br />

the layer stack; this needs to be the splashScreenBackplate ImageView Node object, as you can see in the figure.<br />

The next node to add will be the splashScreenTextArea ImageView Node object, as the transparent images with<br />

the panel overlays need to go right on top of Pat Harrington’s InvinciBagel splash screen 2D artwork. After this, you<br />

can place the UI design, which, in this case can be done in one fell swoop, using the buttonContainer HBox Node<br />

object, which contains all the Button objects. Note that you do not have to add buttons to this StackPane root Scene<br />

Graph object, because you have already used the .getChildren().addAll() method chain to add your Button UI<br />

controls to the Scene Graph hierarchy below an HBox (Parent object) Node branch object. Now, you are ready to test!<br />

138

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

Saved successfully!

Ooh no, something went wrong!