Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 7 ■ IntroduCtIon to JavaFX 9: overvIew oF the JavaFX new MedIa engIne


first take a look at how the Scene class, and its Scene() constructor method, is used to create Scene objects
for JavaFX applications. This section will provide a great reinforcement for what you learned in Chapter 5
regarding overloading constructor methods, since there needs to be several different ways to create a Scene
object.
This Scene class is used to create the Scene object, using a Scene() constructor method. This takes
between one and five parameters, depending on which of the six overloaded constructor methods you
choose to utilize. These include the following constructor methods featuring six different overloaded
parameter list data field configurations:


Scene(Parent root)
Scene(Parent root, double width, double height)
Scene(Parent root, double width, double height, boolean depthBuffer)
Scene (Parent root, double width, double height, boolean depthBuffer, SceneAntialiasing
aAlias)
Scene(Parent root, double width, double height, Paint fill)
Scene(Parent root, Paint fill)


The constructor currently used in your current bootstrap Java and JavaFX code, shown in Figure 6-7
and in your Java code seen on line number 28, is the second constructor, and thus far, it has been structured
(called) as follows:


Scene scene = new Scene(root, 300, 250);


If you wanted to add a Black background color to the scene, you would use the fifth overloaded
constructor method using a Color.BLACK constant from the Color class (this is a Paint object because
Color is a Paint subclass) as your fill data, in this case a fillColor. This would be done by using the following
Scene() object constructor method call:


Scene scene = new Scene(root, 300, 250, Color.BLACK);


Notice that the root object is a Parent subclass, called the StackPane class, and is created using
the StackPane() constructor method, two lines above the Scene() constructor method call, by using the
following line of Java code:


StackPane root = new StackPane(); // StackPane subclassed from Parent; so Parent root node
type


As you can see, any class can be used in the constructor that is a subclass of the object (class) type that is
declared (required) for that constructor parameter position (data). This is why we are able to use Color and
StackPane objects in our parameter list, because they have the superclass origins from the Paint and Parent
classes, respectively.
In case you are wondering what the boolean depthBuffer parameter is, it is used for i3D scene
components. Since these scene components are 3D and have depth (a “Z” component, in addition to a 2D
“X” and “Y” components), you will need to include this parameter and set it to a value of true, if you are
creating 3D scenes, or combining 2D and 3D scene components. Finally, if you are wondering what the
SceneAntialiasing object (and class) that is passed in the parameter list for the fourth constructor method
is, it provides real-time smoothing for 3D scene components. So, for a 3D Scene object, which is what we
will be needing, the constructor method call would look like the following:


Scene 3Dscene = new Scene(root, 300, 250, true, true);

Free download pdf