Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 8 ■ JavaFX 9 SCene Graph hierarChy: a Foundation For Java 9 Game deSiGn


Next, let’s make sure our nodes are added to the SceneGraph correctly. Out of the root (top) of the
Scene Graph, which is a Group object, we will have another gameBoard Group object to hold the i3D game
elements and assets, as well as the uiLayout StackPane object. These are added to the root Group using the
following statements:


root.getChildren().add(gameBoard); // Add new i3D Game Group Node to root Group Node
root.getChildren().add(uiLayout); // Add uiLayout StackPane Node to root Group Node


Next, we add the uiContainer VBox layout container branch node to the uiLayout StackPane branch
node and add five Button UI element leaf nodes to the uiContainer VBox. This is done using two lines of Java
9 code, like this:


uiLayout.getChildren().add(uiContainer); // Add VBox Vertical Layout Node to StackPane
Node
uiContainer.getChildren().addAll(gameButton, // Add All UI Button Nodes to the VBox Node
helpButton,
legalButton, creditButton, scoreButton);


Figure 8-13 shows this SceneGraph construction code. I used color fills on the object hierarchy, which
is visualized to show Node objects (Node subclassed objects, more accurately), which are Scene, root, or
branch nodes. (This is shown in Figure 8-3 if you want to review these JavaFX root, branch, and leaf Node
object hierarchies.)
The important thing to observe here is the order that you have added the Node objects to the Group
root Scene Graph object. The order affects the compositing layer order for the scene render compositing,


Figure 8-13. Add gameBoard Group object and add Node objects to SceneGraph using .getChildren().add
and .addAll()

Free download pdf