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


The second overloaded Group(Node...) constructor method will construct a Group consisting of a
Java List of child Node objects, constructed as a comma-delimited list inside of the constructor method
parameter area. This can be accomplished by using the following constructor method format:


Group(Node... children)


Now that you have taken an overview of the various classes you are using during this chapter, let’s get
back to organizing the code for the JavaFXGame class so that it conforms to what we’re doing with the game
SceneGraph.


Scene Graph Code: Optimize the JavaFXGame Class


I know you are eager to work on the JavaFXGame class code, so let’s clean up, organize, and optimize the
existing Java 9 code to implement the majority of this top-level user interface and SceneGraph design shown
in Figure 8-3 so that you make some progress toward creating your top-level Java 9 game framework during
this chapter. The first thing you’re going to do is to put all the object declaration and naming statements
at the top of the JavaFXGame class, after the import block and the Java class declaration. These object
declarations will come before all of your methods. Many of you programmers are used to declaring global
variables at the top of your code, and an empty object declaration can be declared for use at the top of your
Java code in much the same fashion. This approach is more organized, and all the methods that are inside of
this class will be able to “see” (access or reference) these objects, without using any Java modifier keywords.
This is because the object declarations are at the top of the JavaFXGame class and not inside any of the
methods contained in the class, so all of the declarations done in this way are “visible” to all of the methods
declared “underneath” them. As you can see in Figure 8-4, I am adding a new Group object, which I am
naming root because it will become the new SceneGraph root. Notice the wavy red underline error under
Group because there is no import statement telling Java 9 that you want to use the Group class. Use the
Alt+Enter keystroke combination to bring up the NetBeans helper pop-up and select the Add import for
javafx.scene.Group option, as shown in Figure 8-4.


Figure 8-4. Declare the scene Scene object and the root Group object at the top of the JavaFXGame class before
.start()

Free download pdf