Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 12 ■ 3D MoDel Design anD priMitives: Using JavaFX 9 shape3D Classes

The .drawModeProperty() method defines the ObjectProperty for a Shape3D object,
whereas the .getDrawMode() method allows you to poll the Shape3D object for its current DrawMode
constant setting. There is also the .setDrawMode(DrawMode value) method, which allows you to change
the DrawMode constant setting for the Shape3D object.
The .materialProperty() method defines the ObjectProperty for a Shape3D object, whereas
your .getMaterial() method allows you to poll the Shape3D object for its current Material object setting.
There is also the .setMaterial(Material value) method, which allows you to change the Material object
setting for the Shape3D object.
Next, let’s take a look at the Shape3D subclasses individually as we’ll be leveraging them in the
JavaFXGame.


JavaFX Sphere: Creating Sphere Primitives for Your 3D Games


Since we already created a Sphere object named sphere in the previous chapter to test the
PerspectiveCamera and PointLight 3D Scene setup Java code, let’s cover that Shape3D subclass here first.
This class is kept in the javafx.scene.shape package and is a subclass of Shape3D, as you know, so it has the
following Java class hierarchy:


java.lang.Object



javafx.scene.Node
javafx.scene.shape.Shape3D
javafx.scene.shape.Sphere



The Sphere class defines a three-dimensional sphere with the specified size. A Sphere is a 3D geometry
primitive created algorithmically using the input by the programmer of a radius dimension (size). This
Sphere is always initially centered at the 3D origin 0,0,0. The Sphere object therefore has one radius
DoubleProperty that defines the radius of the Sphere as well as three cullFace, drawMode, and material
properties inherited from javafx.scene.shape.Shape3D.
The Sphere class contains three overloaded constructor methods, including one with no parameters,
which creates an instance of a Sphere with a radius of 1.0. This would look like the following Java 9 Sphere
instantiation:


sphere = new Sphere();


The second constructor method, which is the one we used in Chapter 11 , allows you to specify a radius
using a double numeric value. This would look like the following Sphere instantiation Java code:


sphere = new Sphere(100);


The third constructor allows you to specify a radius and a mesh density via a number of divisions
parameter, which looks like the following Java statement that creates a 100 unit radius Sphere with 24
divisions:


sphere = new Sphere(100, 24)


The Sphere class has some of its own unique methods in addition to the ones it inherits from the
Shape3D class, including the .getDivisions() method, which polls the Sphere object to see how many
divisions it is using; the .radiusProperty() method, which defines the radius of the Sphere object; the
.getRadius() method, which gets the value of the current radius; and the .setRadius(double value) method,
which sets the value for the radius to a different value.

Free download pdf