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

If you did not want to specify the image dimensions, background image loading, and smooth scaling, or lock the<br />

aspect ratio for any scaling, you could condense the previous three lines of <strong>Java</strong> code into the following constructor:<br />

splashScreenBackplate = new ImageView("/invincibagel.png");<br />

// uses third constructor method<br />

At least initially (for learning purposes), I am going to do this the long way, and I will always explicitly load Image<br />

objects, using the Image() constructor method, so that you can specify all the different attributes and see all the<br />

different image assets that you are using in this <strong>Java</strong> programming logic. I want to show you the shortcut code here,<br />

because you will be using this approach later in the book, once you start using ImageViews as sprites (see Chapter 8).<br />

You can use this shortcut approach with your sprites because you will not be scaling them and because they are so<br />

highly optimized that background loading will not be necessary.<br />

Next, let’s take a quick look at the TableView class, which will hold the high scores table. Although you will<br />

not be implementing this here, I will cover the class, as it is part of the top-level UI design that you are creating and<br />

implementing in this chapter.<br />

The <strong>Java</strong>FX TableView Class: Displaying Data Tables in a Design<br />

The TableView class is a public class that directly extends the javafx.scene.control.Control superclass, which is an<br />

extension of javafx.scene.layout.Region. javafx.scene.layout.Region is an extension of the javafx.scene.Parent, which<br />

is an extension of the javafx.scene.Node Scene Graph superclass (see Chapter 4). A TableView object is therefore<br />

a type of UI control (a table) and a Node object in the <strong>Java</strong>FX Scene Graph that is used for constructing a table, using<br />

S objects, each of which contains data to be displayed in a table. You will be writing data into a TableView object<br />

later in the book, using these S objects, after scores have been achieved that eclipse those currently listed in the table.<br />

The <strong>Java</strong> class hierarchy for the TableView class starts with the java.lang.Object master class and uses this class<br />

to create the javafx.scene.Node class, which is then used to create a Parent class. This is used to create a Region<br />

class, which in turn creates a Control class, which is used to create the TableView class. The TableView class has the<br />

following <strong>Java</strong> class inheritance hierarchy structure:<br />

java.lang.Object<br />

> javafx.scene.Node<br />

> javafx.scene.Parent<br />

> javafx.scene.layout.Region<br />

> javafx.scene.control.Control<br />

> javafx.scene.control.TableView<br />

The TableView class provides two different (overloaded) TableView() constructor methods, an empty constructor<br />

and a constructor that takes an ObservableList object, filled with Table data items as a parameter. A simple (empty)<br />

TableView() constructor method to create an empty TableView object will use the following format:<br />

TableView()<br />

The second constructor type uses an ObservableList class (object) from the javafx.collections package,<br />

which is a type of list that allows a data change event listener to track any changes in the list as they occur. This<br />

TableView object constructor method call uses the following format:<br />

TableView(ObservableList items)<br />

I think that is enough class background information for now, so let’s get into writing the code for your first<br />

.createSplashScreenNodes() method, which will instantiate and set up all the Node objects for your Scene Graph!<br />

www.it-ebooks.info<br />

135

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

Saved successfully!

Ooh no, something went wrong!