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

as well as for the UI element compositing, on top of your 3D elements. The first Node added to the root
Group will be on the bottom of the scene compositing (rendering) stack. Therefore, this needs to be the
gameBoard Group Node object, which will hold the i3D game so that Node is added to the Scene Graph
root first and is at the bottom, if you are looking down, or at the back, if you are looking forward, of the Scene
compositing and rendering stack. You can see this in Figure 8-13.
The next Node to add will be your uiLayout StackPane Node object because your 2D user interface
(floating) panel will need to overlay right on top of your 3D GameBoard. After these top-level Node objects
are placed into your Scene Graph hierarchy, we can add the uiContainer VBox Node object, which will
contain all of the Button Control leaf Node objects, to the StackPane Node object. Note that we are using
the .getChildren().addAll() method chain to add Button Control objects to the VBox because we can more
easily add them using a Java List object or comma-delimited list in the parameter area of the .addAll()
method call (chain) called off of the .getChildren() method.
In Chapter 9 we’ll also add an ImageView object named boardGameBackPlate and a TextFlow object
named infoOverlay. I will also need to instantiate five Image objects to hold digital image assets in memory
during Chapter 9 so that the image objects we declared in this chapter can be implemented. As you know,
we named these splashScreen, helpLayer, legalLayer, creditLayer, and scoreLayer using a compound Java
statement, as we did for the Button objects.


Interactivity: Creating the BoardGame Button UI Control


The next thing you need to do is to copy the gameButton.setOnAction() event-handling Java code structure
in your .start() method and then paste it four more times underneath itself to create your helpButton,
legalButton, creditButton, and scoreButton Button Control object event-handling structures. For testing
purposes, at this stage, you will want to change the System.out.println statements to each print a unique
message to your Output console window, so you can make sure that each of the five Button UI elements is
unique to itself and is handling its Button events properly. It is important to always make sure that your Java
9 code constructs work at each stage (that is, after each change or enhancement) before proceeding to add
even more Java code and thus more application complexity. This takes a little bit longer during development
than writing all your code at once but saves time in debugging.
In case you are wondering what the wavy yellow underline warning (or suggestion) is in Figure 8-14,
along with the pop-up message I generated by putting my mouse over this yellow highlighting found under
your event handler ActionEvent processing construct EventHandler() { public void
handle(){...} });, it is because this expression can be turned into a lambda expression using less code. Be
aware that doing this will ensure your code works only under Java 8 and Java 9. If you want to use your code
in Android, which uses Java 6 and Java 7, you may just want to leave these slightly longer Java code structures
in place, as they do exactly the same thing.

Free download pdf