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 Box: Creating Boxes, Posts, and Planes for 3D Games


Next, let’s cover the public Box Shape3D subclass, which can be used to create square, rectangular, and
planar 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.Box



A Box class defines a three-dimensional box, often called a cube primitive, with a specified size. A Box
object is a 3D geometric primitive with three double properties( depth, width, and height) in addition to
the three inherited cullFace, drawMode, and material Shape3D properties. Upon instantiation, it is initially
centered at the origin.
The Box class has two overloaded constructor methods. One creates a default 2,2,2 cube and looks like
the following Java code:


box = new Box();


A second constructor method allows you to specify dimensions for the cube and looks like the following:

box = new Box(10, 200 , 10); // Creates a Post (or Tall Rectangle) Primitive
box = new Box(10, 0.1, 10); // Creates a Plane (or a Flat Surface) Primitive


As you might have guessed, there are nine methods, three for each property, available in the Box class.
This is the class we will use to create the majority of our gameboard infrastructure, so we could be using
these quite often.
The DoubleProperty depthProperty() method is used to define the depth, or the Z dimension, for the
Box. A double getDepth() method can be used to get (poll) the value of the depth property from the Box
object. The void setDepth(double value) method can be used to set or specify a new value for the depth
property for a Box object.
The DoubleProperty heightProperty() method is used to define the height, or the Y dimension, for the
Box. A double getHeight() method can be used to get (poll) the value of the height property from the Box
object. The void setHeight(double value) method can be used to set or specify a new value for the height
property for a Box object.
The DoubleProperty widthProperty() method is used to define the width, or the X dimension for your
Box. A double getWidth() method can be used to get (poll) the value of the width property from your Box
object. The void setWidth(double value) method can be used to set or specify a new value for the width
property for your Box object.
Next, let’s take a look at what it takes to actually implement different primitives in your JavaFXGame code!


Using Primitives: Adding Primitives to Your JavaFXGame Class


Let’s add the other two primitive objects, Box and Cylinder, to your JavaFXGame class, so we can learn about
Face Culling and Draw Modes. We’ll save Material for its own Chapter 13 , as shaders and texture maps
deserve their own chapter and focused discussion. Declare a Box object named box at the top of your class
and use Alt+Enter to have NetBeans 9 help you write the import statement. As you can see in Figure 12-1,
it is important that you add the correct class to your Java 9 game because there is also a javax.swing.Box

Free download pdf