Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 12 ■ 3D MoDel Design anD priMitives: Using JavaFX 9 shape3D Classes


JavaFX Cylinder: Creating Cylinder or Disk Primitives for Games


Next, let’s cover the public Cylinder Shape3D subclass, which can be used to create cylindrical 3D objects
as it is a concrete (usable) class that also implements the Styleable and EventTarget interfaces. This class is
kept in the javafx.scene.shape package and is a subclass of Shape3D, so it will have the following Java class
hierarchy:


java.lang.Object



javafx.scene.Node
javafx.scene.shape.Shape3D
javafx.scene.shape.Cylinder



The Cylinder class is used to define a three-dimensional cylinder with a specified radius and height.
A Cylinder is a 3D geometric primitive algorithm that takes a radius (double) property and a height (double)
property. It is initially centered at the 0,0,0 origin with the radius using the z-axis direction and the height
using the y-axis direction.
Besides the radius and height properties, it will also inherit the Shape3D cullFace, drawMode, and
material properties. It has three overloaded constructor methods, with one the default (empty), one with a
radius and height, and the third with a radius, height, and divisions.
The first empty constructor method creates a new instance of a Cylinder object with a radius of 1.0 and
height of 2.0. It has the following Java statement format:


cylinder = new Cylinder();


The second constructor method creates a new instance of a Cylinder object with a developer specified
radius and height. It has the following Java statement format:


cylinder = new Cylinder(50, 250);


The third constructor method creates a new instance of a Cylinder object with a developer-specified
radius, height, and resolution (number of divisions to determine smoothness). It has the following Java
statement format:


cylinder = new Cylinder(50, 250, 24);


There are three methods for radius, three methods for height, and one .getDivisions() method used
to poll the divisions property, which must be set using the third constructor method format as there is no
.setDivisions() method call or divisionsProperty() method call.
The double .getHeight() method will poll for (get) the value of the height property for a Cylinder object.
The DoubleProperty heightProperty() method defines the height attribute for, or the Y dimension of, the
Cylinder object. Finally, a void setHeight(double value) method allows developers to set the value of the
height property for a Cylinder object.
The double getRadius() method will poll for (get) the value of the radius property for a Cylinder object.
The DoubleProperty radiusProperty() method defines the radius attribute for, or the Z dimension of, the
Cylinder object. Finally, a void setRadius(double value) method allows developers to set the value of the
radius property for a Cylinder object.
Finally, let’s take a look at a Box primitive class, which allows the creation of a wide range of useful
shapes.

Free download pdf