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

Notice that we are taking everything out of the .start() method that is not required to be “hosted”
there. As the primaryStage Stage object is created by the .start() method parameter passed in, we’ll leave all
primaryStage object references inside of this method, as well as all of the event-processing structures, which
need to be put into place on application startup. Everything else will be going in createBoardGameNodes()
and another addNodesToSceneGraph() method we’ll be creating later in the chapter to hold the
.getChildren.add() or .getChildren().addAll() method call.
So, in the .start() method, we will call createBoardGameNodes() first to create all of your
SceneGraph Node objects (that is, all the subclasses of Node, Parent, or Group) and then call the
addNodesToSceneGraph() method to add all of these to the SceneGraph using the .getChildren().add()
method chain or the .getChildren().addAll() method call chain. This organizational method allows us to add
new nodes to your SceneGraph as we build your Java 9 game.
Next, let’s create a second addNodesToSceneGraph() method that we can use to organize, reconfigure,
and expand the SceneGraph Node building part of the JavaFX game application development work process.


Add Scene Graph Nodes: addNodesToSceneGraph()


Next, you need to create a method that will add the SceneGraph Node objects that we have created, and the
ones we are about to instantiate using the VBox constructor, to the Scene Graph root object, which in this
case is now a Group object. This new higher-level SceneGraph root Group object will hold your StackPane
UI Panel for your high-level game functions, as well as another Group object that we will be creating to
hold the 3D game branch of the SceneGraph. In a sense, we are already using JavaFX 9 to create a hybrid
application, as the game UI (StackPane) branch will be 2D and the game itself (Group) will be 3D. We will
use the .getChildren().add() method chain or the .getChildren().addAll() method chain to add the “children”
Node (subclasses of Node, Parent, or Group) objects to the “parent” Group object named root, which is now
the “root” of the JavaFX SceneGraph.


Figure 8-8. Select all non-Stage and non-event-handling code in the start() method and cut and paste it in
new method

Free download pdf