Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples


These attributes and behaviors will serve to define a car to the outside world, just like your pro Java 9
game application objects will do for your Java 9 and JavaFX 9 game applications.
Objects can be as complicated as you want them to be, and Java objects can also nest, or contain, other
Java objects within their object structure or their object hierarchy. An object hierarchy is like a tree structure,
with a main trunk, branches, and then subbranches as you move up (or down) the tree structure, very
similar to a JavaFX or a 3D software Scene Graph, which you saw in Chapter 3 (on the right of Figure 3-4).
A good example of a hierarchy that you use every day would be the multilevel directory or folder
structure, which is on your computer’s hard disk drive.
Directories or folders on your hard drive will contain other directories or folders, which can, in turn,
contain yet other directories and folders, allowing complex hierarchies of organization to be created.
You’ll notice that, in real life, objects can be made up of other objects. For example, a car engine object
is made up of hundreds of discrete objects that function together to make the engine object work as a whole.
This same construction of more complicated objects out of simpler objects can be done in OOP
languages, where complex hierarchies of Java objects can contain other Java objects. Many of these Java
objects may have been created using preexisting or previously developed Java code, which is one of the
objectives of modular programming.
As an exercise, you should practice identifying different complex objects in the room around you and
then break their definition or description down into states (variable states or constant characteristics) as well
as behaviors (things that the objects can or will do) and object and subobject hierarchies.
This is a great exercise to perform because this is how you will eventually need to start thinking to
become more successful in your professional object-oriented game programming endeavors using the
JavaFX engine, inside of the larger Java programming language framework.


Coding the Object: Turning Your Object Design into Java Code


To illustrate this further, let’s construct a basic class for our Car object example. To create a Car class, you
use the Java keyword class, followed by your custom name for the new class that you are writing, and then
curly brackets that will contain your Java code class definition. The first things that you usually put inside


Figure 5-3. The anatomy of a car GamePiece object, with methods encapsulating variables or constants inside
a class

Free download pdf