Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 7 ■ IntroduCtIon to JavaFX 9: overvIew oF the JavaFX new MedIa engIne

JavaFX Bounds and Dimensions: Using javafx.geometry Classes


Even though the term geometry technically applies to 2D and 3D assets, these are contained in a javafx.
scene.shape package, which we have already covered earlier in the chapter. The javafx.geometry package
can be considered to be more of a “utility” package, containing foundational classes for building 2D or
3D constructs from scratch. As such, the package contains classes such as a Bounds superclass and its
BoundingBox subclass, as well as Insets, Point2D, Point3D, Dimension2D, and Rectangle2D geometry
content creation utility classes. All of these classes in this javafx.geometry package, except for the
BoundingBox class, were extended directly from the java.lang.Object master class, meaning that they were
each developed (coded from scratch) for providing points (also called vertices), rectangles, dimensions,
boundaries, and insets (inside boundaries) for use as geometric utilities for your Java 9 games.
The Point2D and Point3D classes (objects, ultimately) hold X,Y coordinates for a 2D point on a 2D
plane, or X,Y,Z coordinates for a 3D point in 3D space, respectively. These Point objects will ultimately be
utilized to build more complex 2D or 3D structures, made up of a collection of points, such as a 2D path,
or a 3D mesh. The Point2D and the Point3D constructor method calls are not overloaded, and they use the
following standard format, respectively:


Point2D(double X, double Y)
Point3D(double X, double Y, double Z)


The Rectangle2D class (object) can be used to define a rectangular 2D area, often referred to as a
“plane,” and has many uses in graphics programming, as you might well imagine.
A Rectangle2D object has a starting point on the upper-left corner of the rectangle, specified using
an X and Y coordinate location, as well as a dimension (width by height). A constructor method for a
Rectangle2D object has the following standard format and is not overloaded:


Rectangle2D(double minX, double minY, double width, double height)


There is also a Dimension2D class (object) that specifies only the width and height dimension and
does not place the dimension (which would make it a rectangle) on the screen using an X, Y location. Its
constructor method is as follows:


Dimension2D(double width, double height)


The Insets class (object) is like a Dimension2D class, in that it does not provide a location value for
the inset but does provide offsets for a rectangular inset area based on top, bottom, left, and right offset
distances. The Insets method is, in fact, overloaded so that you can specify an equidistant inset, or a
customized inset, using the following:


Insets(double topRightBottomLeft)
Insets(double top, double right, double bottom, double left)


The Bounds class is a public abstract class and will never be an object but instead is a blueprint for
creating Node boundary classes such as its BoundingBox subclass. The Bounds superclass also allows a
negative value, which is used to indicate that a bounding area is empty (think of it as null, or unused).
A BoundingBox class uses the following (overloaded) constructor methods to create a 2D (first constructor)
or a 3D (second constructor) BoundingBox object:


BoundingBox(double minX, double minY, double width, double height)
BoundingBox(double minX, double minY, double minZ, double width, double height, double depth)


Next, let’s take a look at Event and ActionEvent processing in JavaFX, as this adds interactivity to your game.
Free download pdf