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


For an example of generalized position, refer to your web site design experience where you can design
a web page so that it will scale to fit different window sizes and shapes. This is quite different from pixel-
accurate positioning where you start at location 0,0 on a fixed screen size and shape and place elements
precisely where you want them!
Game design more often than not uses pixel-precise positioning, but in this chapter I am going to show
you how to position a bank of UI buttons in a general location (such as the top right or bottom left of the
user’s screen) so that you are exposed to as many of the JavaFX API utility classes (this one is in the javafx.
geometry package) as possible.
You will be using the TOP_RIGHT constant, as shown on line 56 in Figure 8-10, to position your Button
control bank in the top-right corner of your BoardGame user interface design, out of the way of the primary
central 3D view.
The Pos class provides a set of constants, which I will summarize in Table 8-1, for providing
“generalized” horizontal and vertical positioning and alignment.


The Pos class provides generalized positioning; it can be used in conjunction with the Insets class to
provide a more pixel-precise positioning. Let’s take a look at the Insets class next, as it is also in the javafx.
geometry package.


JavaFX Insets Class: Providing Padding Values for Your UI


The Insets class is a public class that directly extends the java.lang.Object master class, meaning that the
Insets class was “scratch-coded” to provide insets, or offsets, inside of a rectangular area. Imagine a picture
frame where you leave a “matte,” or attractive border, between the frame on the outside and the picture on
the inside. This is what the Insets class does with two constructor methods; one provides equal or even
insets, and one provides unequal or uneven insets.
We will be using the constructor that provides unequal inset values, which would look very
unprofessional if we were framing a picture! The Java class hierarchy for the Insets class starts with the java.
lang.Object master class and uses this class to create the javafx.geometry.Insets class. As you will see later
in this chapter in code line 58 in Figure 8-11, the Insets class is set to provide zero pixels on two sides and ten


Table 8-1. The Pos Class Enum Constants That Can Be Used for Positioning and Alignment in JavaFX


Pos Class Constant General Positioning Result


BASELINE_CENTER Positions an object on the baseline vertically and at the center horizontally


BASELINE_LEFT Positions an object on the baseline vertically and on the left horizontally


BASELINE_RIGHT Positions an object on the baseline vertically and on the right horizontally


BOTTOM_CENTER Positions an object on the bottom vertically and at the center horizontally


BOTTOM_LEFT Positions an object on the bottom vertically and on the left horizontally


BOTTOM_RIGHT Positions an object on the bottom vertically and on the right horizontally


CENTER Positions an object at the center vertically and at the center horizontally


CENTER_LEFT Positions an object at the center vertically and on the left horizontally


CENTER_RIGHT Positions an object at the center vertically and on the right horizontally


TOP_CENTER Positions an object at the top vertically and at the center horizontally


TOP_LEFT Positions an object at the top vertically and on the left horizontally


TOP_RIGHT Positions an object at the top vertically and on the right horizontally

Free download pdf