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



javafx.scene.layout.Region
javafx.scene.layout.Pane
javafx.scene.layout.VBox



If the VBox has a border or padding value specified, the contents inside of your VBox layout container
will “respect” that border and padding specification. A padding value is specified using the Insets class,
which we covered earlier and which you will be using for this fine-tuned user interface Control Button bank
application.
You are using the VBox class (object), along with the Pos class constant and the Insets class (object), in
order to group your UI Button objects together and, later, to fine-tune position them as your Button control
bank. This VBox layout container will thus become a Parent Node (as well as a branch node) for the UI
Button controls (or leaf nodes).
Think of a VBox object as a way to vertically array child objects together using a column. This could
be your image assets, arranged on top of each other, which would use the basic VBox constructor (with zero
pixels spacing) or UI controls, such as Buttons arranged on top of each other, spaced apart, using one of the
overloaded constructors.
The simplest constructor for a VBox object creation would use the following empty constructor
method call:


VBox()


The overloaded constructor that you’ll be using for your VBox object creation will have a spacing value
to put some space in between your child Button objects inside of a VBox. It uses the following constructor
method call format:


VBox(double spacing)


There are also two other overloaded constructor method call formats. These will allow you to specify
your children Node objects (in our case, these are Button objects) inside of the constructor method call
itself, as follows:


VBox(double spacing, Nodes... children)


This constructor would specify zero pixels of spacing value in between the Array of Node objects:

VBox(Nodes... children)


We’re going to be using the “short form” and .getChildren().addAll() method chain in our code to
show you how this is done, but we could also declare our VBox, and its Button Node objects, by using the
following constructor:


VBox uiContainer = new VBox(10, gameButton, helpButton, scoreButton, legalButton,
creditButton);


Your VBox layout container will control the resizing of child elements based on different screen sizes,
aspect ratios, and physical resolutions if the child objects are set to be resizable. If the VBox area will
accommodate the child object preferred widths, they will be set to that value. There is a boolean fillWidth
attribute (property), which is set to true as its default value. This specifies whether a child object should fill
(scale up to) the VBox width value.
The alignment of a VBox is controlled by the alignment attribute (property or variable), which defaults
to the TOP_LEFT constant from the Pos class (Pos.TOP_LEFT). If the VBox fillWidth property is false and

Free download pdf