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 9 ■ Controlling Your Action Figure: Implementing <strong>Java</strong> Event Handlers and Using Lambda Expressions<br />

Game Surface Design: Adding Resolution Flexibility<br />

The first thing that I want to do to the InvinciBagel.java code, which should already be open in a tab in NetBeans (if it<br />

is not, use the right-click and Open work process), is to add WIDTH and HEIGHT constants for the game application.<br />

The reason for doing this is that you may want to provide custom versions for netbooks or tablets (1024 by 600), or<br />

iTV set or eReaders (1280 by 720) or HDTV usage (1920 by 1080) or even for new 4K iTV sets (4096 by 2160). Having<br />

a height and width variable allows you to not have to change your Scene() constructor method call later, and to do<br />

certain screen boundary calculations using these variables rather than “hard coded” numeric values throughout your<br />

code. As you can see at the top of Figure 9-1, I have created a constant declaration for these two variables using a<br />

single line of <strong>Java</strong> code, which is known as a compound statement, as you learned in the previous chapter. The <strong>Java</strong><br />

code for this declaration can be seen at the top of the class in Figure 9-1, and should look like the following:<br />

static final double WIDTH = 640, HEIGHT = 400;<br />

Figure 9-1. Add private static final double WIDTH and HEIGHT constants; install these, and Color.WHITE, in Scene()<br />

The next thing that we will want to do is the upgrade our Stage() constructor method call, to use one of the other<br />

overloaded constructor methods that allows us to also specify the background color value. Let’s use the Color class<br />

constant of WHITE, and our new width and height display screen size constants, and create this new constructor<br />

method call, using the following line of <strong>Java</strong> code, which is also shown (laden with errors) at the bottom of Figure 9-1:<br />

scene = new Scene(root, WIDTH, HEIGHT, Color.WHITE);<br />

As you can see in Figure 9-1, you will have a wavy red error underline under your Color class reference, until you<br />

use the Alt-Enter work process to bring up the helper dialog (as shown), and select the option that specifies “Add<br />

import for javafx.scene.paint.Color” in order to have NetBeans write the <strong>Java</strong> import statement for you. Once you do<br />

this, your code will be error free, and we will be ready to write some code that will put this background color in place.<br />

To do this, we’ll need to hide the ImageView Node objects that hold our full screen (splashScreenbackplate)<br />

and overlay (splashScreenTextArea) image assets. We will do this by setting the visible attribute (or characteristic, or<br />

parameter, if you prefer) to a value of false, which will allow the white background color that we set to show through.<br />

188

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

Saved successfully!

Ooh no, something went wrong!