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 />

Scene Graph Nodes: .createSplashScreenNodes( )<br />

The first thing you will want to do with your createSplashScreenNodes() method is code the empty method structure<br />

and add the Node object creation code that already exists in the bootstrap code that was generated for you by<br />

NetBeans in Chapter 2. This includes the Node objects for the Button node, the StackPane root node, and the Scene<br />

object named scene. You will keep the primaryStage code in the .start() method because that object is created using<br />

the .start(Stage primaryStage) constructor method call. The Button object has already been renamed gameButton<br />

(it was btn), so you have three lines of object instantiation code and a line of configuration code, as follows:<br />

root = new StackPane();<br />

scene = new Scene(root, 640, 400);<br />

gameButton = new Button();<br />

gameButton.setText("PLAY GAME");<br />

It is important to note that because the root StackPane object is used in the constructor method call for the scene<br />

Scene object, this line of code needs to come first (your root object has to be created before it is used!). The next thing<br />

you need to create is the HBox layout container object that will hold your four Button UI controls. You will also set the<br />

alignment attribute for the HBox; add an Insets object to contain the padding values; and then add this padding to<br />

the4 HBox object, using these four lines of <strong>Java</strong> code:<br />

buttonContainer = new HBox(12);<br />

buttonContainer.setAlignment(Pos.BOTTOM_LEFT);<br />

buttonContainerPadding = new Insets(0, 0, 10, 16);<br />

buttonContainer.setpadding(buttonContainerPadding);<br />

Next, let’s take a handy programmers’ shortcut and copy and paste the two gameButton (instantiation and<br />

configuration) lines of code below the HBox code (because the buttons are inside the HBox, this is just for visual<br />

organization, not to make the code work) and then copy and paste them three more times, on separate lines. This will<br />

allow you to change the game to help, score, and legal, respectively, by creating the following four button <strong>Java</strong> codes:<br />

gameButton = new Button();<br />

gameButton.setText("PLAY GAME");<br />

helpButton = new Button();<br />

helpButton.setText("INSTRUCTIONS");<br />

scoreButton = new Button();<br />

scoreButton.setText("HIGH SCORES");<br />

legalButton = new Button();<br />

legalButton.setText("LEGAL & CREDITS");<br />

Now that you have created the HBox Button UI control layout container and buttons, you still need to write one<br />

more line of code to fill the HBox with Button objects, using the .getChildren().addAll() method chain, like this:<br />

buttonContainer.getChildren().addAll(gameButton, helpButton, scoreButton, legalButton);<br />

Next, let’s add your image-compositing Node objects (Image and ImageView) so that you can add the artwork for<br />

your InvinciBagel splash screen as well as the panel overlays that will decorate your instructions, legal disclaimers and<br />

production credits and a background and screen title for your (eventual) game high scores table. I use two ImageView<br />

objects to contain these two layers; let’s set up the bottommost backplate image layer first by using the following <strong>Java</strong><br />

code to instantiate the Image object and then the ImageView object and wire them together:<br />

splashScreen = new Image("/invincibagelsplash.png", 640, 400, true, false, true);<br />

splashScreenBackplate = new ImageView();<br />

splashScreenBackplate.setImage(splashScreen); // this <strong>Java</strong> statement connects the two objects<br />

136<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!