Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 15 ■ 3D Gameplay UI CreatIon: UsInG the sphere prImItIve to Create a UI noDe


The overloaded constructor method for the Scene that allows you to turn on both depth buffering and
anti-aliasing as the default behavior for the 3D Scene object looks like the following Java code:


Scene(Parent root, double width, double height, boolean depthBuffer, SceneAntialiasing
constant)


Thus, we need to add depthBuffer=true and SceneAntialiasing.BALANCED to the Scene()
constructor that we’re using in the createBoardGameNodes() method, which as you can see in
Figure 15-12 (in a red rectangle) I added to the end of the scene = new Scene(root, 1280, 640); Java 9
Scene object instantiation statement. This switches your constructor method call to utilize a different
overloaded constructor method to create your 3D Scene.
Let’s add a 3D UI element, called a spinner, that a player can use to randomly spin the game board to
pick a topic.


Creating a 3D User Interface Element: A 3D Spinner Randomizer


Now let’s reuse our Sphere primitive code and beach ball texture map to create a 3D user interface (UI)
element that the player can click to spin the board to pick a random subject (topic) category. Declare the
Sphere and name it spinner at the top of the class. Then instantiate it with a radius of 60 and configure it
with Shader25 and the X, Y location of -200, -500, which puts it at the top-left corner of the screen. Use the Y
rotation axis and set a rotate value of 25 degrees to try to place the word SPIN facing the user. Your Java code,
shown in Figure 15-12, will look like this:


Sphere spinner;
...
spinner = new Sphere( 60 );
spinner.setMaterial(Shader25);
spinner.setTranslateX(-200);
spinner.setTranslateY(-500);
spinner.setRotationAxis(Rotate.Y_AXIS);
spinner.setRotate( 25 );
scene = new Scene(root, 1280, 640, true, SceneAntialiasing.BALANCED);

Free download pdf