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


We’ve already renamed our Button object to be gameButton (was btn), so we now have six lines of
object instantiation code and five lines of object configuration code, as shown in Figure 8-11, using the
following Java 9 code:


private void createBoardGameNodes() {
root = new Group();
scene = new Scene(root, 300, 250);
scene.setFill(Color.TRANSPARENT);
gameButton = new Button();
gameButton.setText("Start Game");
uiLayout = new StackPane();
uiLayout.setBackground(Background.EMPTY);
uiContainer = new VBox();
uiContainer.setAlignment(Pos.TOP_RIGHT);
uiPadding = new Insets(0,0,10,10);
uiContainer.setpadding(uiPadding);
}


It is important to note that since your root Group object is used in the constructor method call for the
scene Scene object, this line of code will need to come first so that the root Group object is created before it
is utilized.
Next, let’s take the handy programmer’s shortcut and cut and paste your two gameButton instantiation
and configuration lines of code underneath the uiContainer.setPadding(uiPadding); method call and then
copy and paste that code four times underneath itself, as shown highlighted at the bottom of Figure 8-12,
to create all ten of your user interface button elements using the modified gameButton (btn) bootstrap UI
element created in Chapter 6.


Figure 8-11. Create a uiPadding Insets object and wire it to the uiContainer VBox object using
.setPadding(uiPadding);

Free download pdf