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

As you can see, I also moved the declaration for your existing scene Scene object to the top of the class,
so, instead of Scene scene = new Scene();, we now have the following Scene object declaration Java code
structure, which can be seen in Figure 8-5:


public class JavaFXGame extends Application {
Scene scene;
public void start(Stage primaryStage) {
scene = new Scene(root, 300, 250);
}
}


Next, we will do the same thing for the StackPane object, which I am going to rename uiLayout, as the
root object is now a Group Node class object. Add a StackPane uiLayout; declaration, as shown in Figure 8-5,
and then change the Java code shown in Figure 8-5 in a red box to use the uiLayout name instead of the root
name, as follows:


uiLayout = new StackPane;
uiLayout.setBackground(Background.EMPTY);
uiLayout.getChildren().add(btn);


I placed the uiLayout StackPane code right before the scene Scene instantiation. We are going to be
moving the object instantiations, with the exception of the Stage object (which needs to be part of the .start()
method), into their own .createBoardGameNodes() method, after we create the block of object declarations
and naming at the top of the JavaFXGame.java class.


Figure 8-5. Organize the .start() method by creating createBoardGameNodes() and
addNodesToSceneGraph() methods

Free download pdf