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

Since a Button object can’t be positioned individually, I had to use the VBox class along with the Insets
class to contain and position the Button controls professionally. We will be going over the classes that you
will be using to create this high-level design during this chapter so that you have an overview of each class
that you are going to be adding to your JavaFXGame in order to create this top-level UI design for your
JavaFXGame.java Application subclass.
The way we optimize your Scene Graph use for the five different screens needing to match the five
different buttons is to use one ImageView as a backplate to contain the BoardGame splash screen artwork
on game startup. When a user clicks your UI buttons, you can use Java code to have the ImageView reference
different images using one single ImageView Scene Graph Node object. Your TextFlow object will overlay
your text assets on the ImageView.
Finally, there may be a SceneGraph Node that will contain the data structure for a High Score Table.
This will be created via a Score Engine that we’ll be creating later when we cover game score approaches and
techniques. For now, we’ll leave score and gameplay code unimplemented. Let’s look at some new JavaFX UI
design classes next.


JavaFX Design: Using VBox, Pos, Insets, and Group


Before we dive into coding, let’s take an in-depth look at some of the new JavaFX classes we are going to
utilize to complete these top-level game application UI and SceneGraph designs. These include the Pos
class (positioning), the Insets class (padding), the VBox class (a vertical UI layout container), and the Group
class (Scene Graph Node grouping). In the next chapter, we will cover the Image (image asset holder),
ImageView (image backplate display), and TextFlow (text data display) classes. We will look at these in order
from the simplest (Pos) to the most complex (Group), and then you will code fairly extensive changes to your
bootstrap JavaFX project code, which will add these new classes (and objects) to your JavaFX Scene Graph
hierarchy, as well as reorganizing it to better suit your game.


JavaFX Pos Class: Generalized Positioning Using Constants


The Pos class is an Enum class, which is short for enumeration. It contains a list of constants that
are actually translated into integer values for use in your code. The constant values make it easier for
programmers to use these values in their code. In this case, it would be positioning constant prefixes like
TOP, CENTER, or BASELINE, for instance.
The Java class extension hierarchy for the Pos class starts at the java.lang.Object master class and
progresses through the java.lang.Enum class, ending with the javafx.geometry.Pos class. You are
referencing Pos in line 56 in the code in Figure 8-10. Pos is in the javafx.geometry package and uses the
following subclass hierarchy structure:


java.lang.Object



java.lang.Enum
javafx.geometry.Pos



As you’ll see in the next section, you will have to use the Insets class and object to obtain the pixel-
accurate positioning that you desire. Since this is an Enum class, there is not too much to learn in this
section, other than what the constants are that the Pos class offers to you to use for generalized and relative
positioning in your Java games.
Therefore, the Pos class is great for general positioning, using top, bottom, left, and right, as well as
baseline (this is for positioning relative to fonts, primarily). Each of these also has a CENTER option for
centering, so by using the dozen constants provided for in this helper class, you can implement any kind of
generalized positioning you will need.

Free download pdf