Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 10 ■ User InterfaCe DesIgn InteraCtIvIty: event hanDlIng anD ImagIng effeCts

This code configures the different objects in the compositing stack (Stage ➤ Root ➤ StackPane
➤ ImageView ➤ TextFlow) to display the default white background for the OS, which is the Scene
(and Stage) object, by installing transparency in a StackPane object using Background.EMPTY. The
boardGameBackPlate ImageView contains a transparent Instructions script font drop-shadowed PNG32
image, which lets the white background color through. The TextFlow and two Text objects also support
transparency and add the game instructions, so the Information screen is a nice, readable white color with
the text preset to Color.GREEN. If you click the Start Game Button (which we’ll be coding next, to reset itself
to the default settings), you can switch between the SplashScreen and the new help text, albeit with some
mistakes on the SplashScreen because the gameButton event handler needs to reset characteristics, which
we’ll do next to restore the white text, text location, splash screen image, and welcome image, since this
Button changes the object characteristics.
Next, let’s copy and paste these Java statements into the gameButton event handling structure, and
then we will configure your method call parameter areas using the correct Text, Background, and Image
objects and pixel location values. Clear your TextFlow object and then load your TextFlow object with your
playText and moreText Text objects using the .addAll() method. Next, set the TextFlow container X,Y pixel
location (its position on the screen) using a 240 Integer value for your .setTranslateX() method call and a
420 Integer value for your .setTranslateY() method call. Load your uiLayout StackPane object’s background
with the uiBackground Background object by using the .setBackground() method call and then load the
boardGameBackPlate ImageView with the splashScreen Image object by using the .setImage() method
call. This is all accomplished using the following Java code structure inside the .handle() method, as shown
highlighted in the middle of Figure 10-3:


gameButton.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent event) {
infoOverlay.getChildren().clear();
infoOverlay.getChildren().addAll(playText, moreText);
infoOverlay.setTranslateX( 240 );
infoOverlay.setTranslateY( 420 );
uiLayout.setBackground(uiBackground);
boardGameBackPlate.setImage(splashScreen);
}
} );

Free download pdf