28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4 ■ An Introduction to <strong>Java</strong>FX 8: Exploring the Capabilities of the <strong>Java</strong> 8 Multimedia Engine<br />

The Scene class is used to create a Scene object, using the Scene() constructor class, which takes between one<br />

and five parameters, depending on which of the six (overloaded) constructor methods you choose to use. These<br />

include the following constructor methods, along with their six different (and thus overloaded) parameter list data<br />

field configurations:<br />

Scene(Parent root)<br />

Scene(Parent root, double width, double height)<br />

Scene(Parent root, double width, double height, boolean depthBuffer)<br />

Scene(Parent root, double width, double height, boolean depthBuffer, SceneAntialiasing aAlias)<br />

Scene(Parent root, double width, double height, Paint fill)<br />

Scene(Parent root, Paint fill)<br />

The constructor currently used in your bootstrap <strong>Java</strong> and <strong>Java</strong>FX code is the second one, called as follows:<br />

Scene scene = new Scene(root, 300, 250);<br />

If you wanted to add a black background to the scene, you would select the fifth overloaded constructor method,<br />

using a Color.BLACK constant from the Color class (this is a Paint object, because Color is a Paint subclass) as your<br />

fill data (in this case, a fill Color). You would do this by using the following Scene() object constructor method call:<br />

Scene scene = new Scene(root, 300, 250, Color.BLACK);<br />

Note that the root object is a Parent subclass, called the StackPane class, created using the StackPane()<br />

constructor method (two lines above the Scene() constructor method call) by using the following line of <strong>Java</strong> code:<br />

StackPane root = new StackPane(); // StackPane subclassed from Parent, so Parent root satisfied<br />

As you can see, any class can be used in the constructor that is a subclass of the object (class) type that is declared<br />

(required) for that constructor parameter position (data). You are able to use Color and StackPane objects in your<br />

parameter list because they have superclass origins from the Paint and Parent classes, respectively.<br />

In case you are wondering, the Boolean depthBuffer parameter is used for 3D scene components. Because<br />

these scene components are 3D and have depth (a z component, in addition to 2D x and y components), you will<br />

need to include this parameter, and set it to a value of true, if you are creating 3D scenes or combining 2D and 3D<br />

scene components. Finally, the SceneAntialiasing object (and class) that is passed in the parameter list for the fourth<br />

constructor method provides real-time smoothing for 3D scene components.<br />

<strong>Java</strong>FX Scene Graph: Organizing Scenes, Using Parent Nodes<br />

A scene graph, which is not unique to <strong>Java</strong>FX and which can be seen in quite a few new media content creation<br />

software packages, is a data structure that resembles an upside-down tree, with the root node at the top and branch<br />

nodes and leaf nodes coming off the root node. The first time I saw a scene graph approach to design was when I was<br />

3D modeling using a software package on the Amiga called Real 3D from Realsoft Oy. This approach has been copied<br />

by many 3D, digital video, and digital imaging software packages since then and now is a part of how <strong>Java</strong>FX organizes<br />

content and scenes. For this reason, many of you may be familiar (and comfortable) with this design paradigm.<br />

<strong>Java</strong>FX Scene Graph data structure allows you not only to architect, organize, and design your <strong>Java</strong>FX scene and<br />

its content, but also to apply opacity, states, event handlers, transformations, and special effects to entire logical<br />

branches of the Scene Graph hierarchy if you set up the Scene Graph correctly. Figure 4-3 shows the basic Scene<br />

Graph tree, with the root node at the top and branch and leaf nodes below it.<br />

www.it-ebooks.info<br />

79

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!