28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 ■ The Foundation of Game Design: The <strong>Java</strong>FX Scene Graph and the InvinciBagel Game Infrastructure<br />

You will be using the constructor that offers unequal insets values, which would look very unprofessional if you<br />

were framing a picture! The <strong>Java</strong> class hierarchy for the Insets class starts with the java.lang.Object master class and<br />

uses this class to create the javafx.geometry.Insets class. As Figure 6-4 illustrates (l. 5), Insets is contained in the<br />

<strong>Java</strong>FX geometry package, just like the Pos class, and uses the following class hierarchy structure:<br />

java.lang.Object<br />

> javafx.geometry.Insets<br />

The Insets class furnishes a set of four double offset values specifying the four sides (top, right, bottom, left)<br />

of a rectangle, which should be specified in that order within the constructor method. You will be using the Insets<br />

class (object) to fine-tune the position of the Button control bank, which you will be creating using the HBox layout<br />

container. Think of these Insets objects as a way to draw a box inside another box, which shows the spacing that you<br />

want the objects inside the rectangle to “respect” around its edges. The simple constructor for an Insets object would<br />

use the following format:<br />

Insets(double topRightBottomLeft)<br />

This constructor uses a single value for all the spacing sides (topRightBottomLeft), and an overloaded<br />

constructor allows you to specify each of these values separately, like this:<br />

Insets(double top, double right, double bottom, double left)<br />

These values need to be specified in this order. An easy way to remember this is by using an analog clock. A clock<br />

has “12” at the top, “3” at the right, “6” at the bottom, and “9” at the left. So, starting at high noon (for you western<br />

genre lovers out there), always work clockwise, the way the hands move around a clockface, and you will have a great<br />

way to remember how to specify the Insets values in the “uneven values” constructor method. You will soon be using<br />

the Insets class to position the Button control bank, which are initially “stuck” in the bottom-left corner of the splash<br />

screen design, away from the left-hand side and bottom of the screen, using two of these four insets positioning<br />

parameters.<br />

The <strong>Java</strong>FX HBox Class: Using a Layout Container in a Design<br />

Because Button objects cannot be positioned easily, I will be placing the four Button objects in a layout container from<br />

the javafx.scene.layout package called HBox, which stands for Horizontal Box. This public class arranges things in<br />

a row, and because you want the buttons to be aligned at the bottom of the splash screen, you use the Parent node for<br />

four Button control nodes, which will become children (leaf nodes) of this HBox branch node. This will create a bank<br />

of UI buttons that can be positioned (moved around) together as a single unit of the splash screen design.<br />

An HBox class is a public class that directly extends the javafx.layout.Pane superclass, which in turn extends a<br />

javafx.layout.Region superclass. The javafx.layout.Region superclass extends a javafx.scene.parent superclass, which<br />

in turn extends a javafx.scene.Node superclass, which extends the java.lang.Object masterclass. As Figure 6-4<br />

shows (l. 11), HBox is contained in the javafx.scene.layout package, just like the StackPane class, and it uses the<br />

following class hierarchy structure:<br />

java.lang.Object<br />

> javafx.scene.Node<br />

> javafx.scene.Parent<br />

> javafx.scene.layout.Region<br />

> javafx.scene.layout.Pane<br />

> javafx.scene.layout.HBox<br />

www.it-ebooks.info<br />

131

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!