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 in Figure 8-6 outlined in red, NetBeans will code five new import statements for you,
as long as you hit Alt+Enter as you type these object declaration and naming statements at the top of the
JavaFXGame class.
As you can see highlighted in yellow, I have renamed the bootstrap btn Button to gameButton and
changed its .setText("Hello World") to .setText("Start Game") to more directly reflect what this Button
UI element will eventually accomplish, as we continue to refine this Java 9 class code throughout the course
of this book.
I also changed uiLayout.getChildren().add(btn); to uiLayout.getChildren().add(gameButton);
to reflect this name change throughout all of the Java 9 code in this class that currently affects this Button
object. All of this is shown highlighted in Figure 8-6 using red boxes, blue line selection, and yellow object
reference selection.
NetBeans 9 will write these five new import statements for you as long as you utilize an Alt+Enter
keystroke combination. Be sure to select the option with the correct javafx package class path. Next, let’s
optimize your .start() method by offloading game object instantiation (with the exception of Stage, which is
part of your .onCreate(Stage primaryStage) method) so that all non-Stage object creation is done using
the .createBoardGameNodes() method.


Scene Graph Design: Optimizing the BoardGame .start() Method


Now we can optimize the .start() method so that it uses less than a dozen lines of code (see Figure 8-16 if you
want to look ahead). The first thing that I want to do is to modularize the Scene Graph Node creation Java
constructs into their own createBoardGameNodes() method, which will be called at the top of the .start()
method, as shown in Figure 8-7. Add a line of code at the top of the method, type createBoardGameNodes();,


Figure 8-6. Declare five new object types at the top of the JavaFXGame class and rename the btn object
gameButton

Free download pdf