Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 8 ■ JavaFX 9 SCene Graph hierarChy: a Foundation For Java 9 Game deSiGn

pixels on two sides. This pushes the Button bank away from the corner of your user’s display screen.
The JavaFX Insets class is contained in the javafx.scene.geometry package, just like the Pos class, and uses
the following Java 9 class hierarchy structure:


java.lang.Object



javafx.scene.geometry.Insets



The Insets class provides a set of four double offset values specifying the top, right, bottom, and left
sides of a rectangle and should be specified in that order within a constructor method, as you saw when you
wrote your code. You will be using this Insets class (object) to “fine-tune position” your Button control bank,
which you will be creating using the VBox layout container (which you will be learning about in the next
section). Think of these Insets objects as a way to draw a box inside of another box, which shows the spacing
that you want the objects inside of the rectangle to “respect” around its edges. This is often called padding,
especially in Android Studio and HTML5 programming.
The simplest Insets() constructor for use in creating your Insets object would use the following format:


Insets(double topRightBottomLeft)


This constructor uses a single value for all the spacing sides (topRightBottomLeft), and an overloaded
constructor allows you to specify each of these values separately, which looks like the following:


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


These values need to be specified in this order. A great way to remember this is to think of an analog
clock. The clock has 12 at the top, 3 at the right, 6 at the bottom, and 9 at the left. So, simply remember to
specify clockwise starting at high noon (for you Western film genre lovers out there), and you will have
a great way to remember how to specify the Insets values when using the “uneven values” constructor
method.
You are using the Insets class to position your Button control bank, which would initially be “stuck” in
your bottom-left corner of the BoardGame user interface design. The Insets object will allow you to push
the Button controls away from the right side of your screen and away from the top of your VBox, using two of
these four Insets parameters.


JavaFX VBox Class: Using a Layout Container for Your Design


Since Button objects cannot be positioned easily, I will be placing the five Button objects into a layout
container from the javafx.scene.layout package called VBox, which stands for vertical box. This public
class arranges things into a column, and since you want the buttons aligned at the side of your BoardGame,
it is the Parent Node that you will use for five Button control Nodes, which will become children leaf nodes
of this VBox branch node. This will create a “bank” of UI Button controls that can be positioned (moved
around) together as a single unit of the UI and splash screen design.
A VBox class is a public class that directly extends the javafx.scene.layout.Pane superclass, which in
turn extends a javafx.scene.layout.Region superclass, which extends the javafx.scene.parent superclass,
which extends a javafx.scene.Node superclass, which extends the java.lang.Object master class. As you
can see in line 55, in Figure 8-10, you will use VBox as a Button control positioning user interface layout
container. This VBox class is contained in the javafx.scene.layout package, just like the StackPane class, and
it uses the following Java class hierarchy structure:


java.lang.Object



javafx.scene.Node
javafx.scene.Parent


Free download pdf