28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

If an HBox has a border or a padding value specified, the contents of your HBox layout container will respect that<br />

border or padding specification. A padding value is specified using the Insets class, which you will be using for exactly<br />

this fine-tuned UI control bank application.<br />

You will use the HBox class (object), along with the Pos class constant and the Insets class (object), to group the<br />

UI Button objects together and, later, to fine-tune their position as a Button control bank. This HBox layout container<br />

will thus become the Parent node (or a branch node) for the Button UI controls (or leaf nodes).<br />

Think of an HBox object as a way to array children objects horizontally, in a row. These could be your image<br />

assets, which would use the basic HBox constructor (with zero spacing), or UI controls, such as buttons, arranged next<br />

to each other but spaced apart, using one of the overloaded constructors. The simplest constructor for an HBox object<br />

creation would use the following empty constructor method call format:<br />

HBox()<br />

The overloaded constructor that you will be employing for your HBox object creation will use a spacing value to<br />

put some space between the child Button objects inside the HBox, with the following constructor method call format:<br />

HBox(double spacing)<br />

There are also two other overloaded constructor method call formats. These will allow you to specify the children<br />

Node objects (in this case, Button objects) inside the constructor method call itself, as follows:<br />

HBox(double spacing, Nodes... children) - or, with zero spacing value in between Node objects:<br />

HBox(Nodes... children)<br />

You are going to be using the “long form,” and .getChildren().addAll() method chain, in your code, but you<br />

could also declare the HBox, and its Button Node objects, by using the following constructor:<br />

HBox buttonContainer = new HBox(12, gameButton, helpButton, scoreButton, legalButton);<br />

The HBox layout container will control resizing of child elements, based on different screen sizes, aspect ratios,<br />

and physical resolutions if the child objects are set to be resizable. If the HBox area will accommodate the child<br />

objects’ preferred widths, they will be set to that value. In addition, a fillHeight attribute (boolean variable) is set to<br />

true, as the default value, specifying whether a child object should fill (scale up to) the HBox height value.<br />

Alignment of an HBox is controlled by the alignment attribute (property or variable), which defaults to the<br />

TOP_LEFT constant from the Pos class (Pos.TOP_LEFT). If an HBox is sized above its specified width, the child<br />

objects use their preferred width values, and the extra space goes unused. It is important to note that the HBox layout<br />

engine will lay out the managed child elements, regardless of their visibility attribute (property or variable) setting.<br />

Now that I have discussed the <strong>Java</strong>FX geometry and layout classes, which you will be using to create the UI<br />

(a bank of Button objects) design, let’s take a look at the digital image–related classes, from the javafx.scene.image<br />

package, which will allow you to implement the digital image–compositing pipeline that you will put in place behind<br />

these four <strong>Java</strong>FX Button UI control element objects held inside an HBox UI layout container object.<br />

The <strong>Java</strong>FX Image Class: Referencing Digital Images in a Design<br />

The Image class is a public class that directly extends the java.lang.Object masterclass, meaning that the Image class<br />

was also coded from scratch to provide image loading (referencing) and scaling (resizing). You can lock the aspect<br />

ratio for scaling and specify the scaling algorithm (quality) as well. All URLs that are supported by the java.net.URL<br />

class are supported. This means that you can load images from the Internet (www.servername.com/image.png); from<br />

the OS (file:image.png); or from the JAR file, using a forward slash (/image.png).<br />

132<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!