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


To create this second method, we will follow the same work process that we used to create the first of
your custom methods. Add a line of code immediately after the createBoardGameNodes(); line of code and
then type in addNodesToSceneGraph(); as the second line of code.
After NetBeans 9 highlights this with a wavy red error underline, use the Alt+Enter keystroke
combination and select the Create method "addNodesToSceneGraph" to javafxgame.JavaFXGame
option, as shown highlighted in Figure 8-9. I have also highlighted in red the one statement currently in the
createBoardGameNodes() method body, which will be relocated into this new addNodesToSceneGraph()
method body. This will replace the stock throw new UnsupportedOperationException() Java statement,
which NetBeans puts into all newly created bootstrap methods that it creates using this particular work
process where you can get NetBeans to write your new method code for you.


Cut the uiLayout.getrChildren().add(gameButton); statement at the end of createBoardGameNodes()
and paste it over the placeholder throw new UnsupportedOperationException() line of code, replacing that
code. We will be adding more nodes to the SceneGraph using this method once we instantiate those new
nodes in the next section.


Adding New UI Scene Graph Nodes to createBoardGameNodes()


Let’s add those new UI design and positioning JavaFX class objects (VBox, Pos, Insets) that we learned about
earlier in the chapter to the JavaFXGame class and the createBoardGameNodes() method we created that
contains our JavaFX 9 SceneGraph Node object creation (and configuration) Java 9 statements.
Create a new VBox named uiContainer by using the following Java object instantiation code, which
uses the Java new keyword in conjunction with the VBox() constructor method:


uiContainer = new VBox(); // Create a Vertical Box UI element container named "uiContainer"


Figure 8-9. After creating addNodesToSceneGraph() method, copy the uiLayout.getChildren() method chain
to new method

Free download pdf