Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


and OpenSolaris OSs. I have three different workstations that are optimized for each of the Java API
platforms and IDE software packages so that I can develop applications for 32-bit Android devices (Java 6),
Android 5 through 6 (Java 7), HTML5 and Android 7 through 8 (Java 8), and JavaFX 9 (Java 9) at the same
time. Fortunately, you can get a powerful Windows 10 or Ubuntu LTS 18 HexaCore (or OctaCore) 64-bit pro
Java 9 game development workstation at http://www.PriceWatch.com for a few hundred dollars.
Besides the API level (the SDK you installed and are using), the highest-level construct in the Java
programming language is a package. Java packages use the package keyword to declare your own application
package at the top of your Java code. This needs to be the first line of code declared other than comments,
as you’ll see in Chapter 6 (and shown in Figure 5-1 earlier in this chapter). You can have only one package
declaration and can declare only one package, and it must be the first Java statement! The New Project series
of dialogs in NetBeans 9 that you will use in Chapter 6 will create your package for you and will import other
packages that you will need to use based on what you want to do in your application. In our case, these will
be JavaFX 9 packages, so we can utilize the JavaFX new media engine. Java 9 further groups packages into
modules, which are added to an application outside of (externally to) your main Java program logic.
As you may have ascertained from the name, a Java package bundles together all of the Java
programming constructs that you will be learning about, or reviewing, during this chapter. These include
classes, interfaces, and methods that relate to your application, so the gameboard package will contain all
of your code, as well as all of the code that you imported to work with your code, that is needed to create,
compile, and run your board game. We will take a look at the concept of importing and the Java import
keyword next, as it relates closely to the package concept.
A Java package is useful for organizing and containing all of your own application code, but it is even
more useful for organizing and containing the SDK’s (API’s) Java code that you will utilize along with your
own Java programming logic to create your pro Java games or IoT applications. As of Java 9, Java packages
will now be organized by functional modules, which we will be covering at the end of this chapter, as
modules do not affect your Java game programming logic; they simply organize things at a high level to
allow you to optimize your distribution so that you can obtain the smallest download size for your Java game
distribution to your target game-playing end users.
You can use any of the classes that are part of the API that you are developing “under,” or with, by using
the Java import keyword, which, in conjunction with the package and the classes that you want to use, is
called an import statement. This import statement begins with the import keyword, the package and
class reference path (full proper name) is next, and then the statement needs to be terminated using a
semicolon. As you saw in Figure 5-1, the import statement used to import the JavaFX EventHandler class
from the javafx.event package should look like this:


import javafx.event.EventHandler;


An import statement informs the Java compiler that it will need to bring a specified external package
inside of your package (import it into your package), because you will be using methods (and constants)
from the class that is referenced using the import keyword, as well as what package it’s stored in. If you use a
class, method, or interface in your own Java 9 class, such as the BoardGame class you will be creating during
Chapter 6 , and you have not declared that class for use by using an import statement, the Java 9 compiler
will throw an error. This is because it cannot locate or reference the class that is going to be used in your
package so that it can import that functionality.


Java Classes: Java Structure to Modularize the Game


The next largest Java programming structure underneath the package level is the Java class level, as you have
seen in the import statement, which references both the package that contains the class and a class itself.
Just like a package organizes all of the related classes, a class organizes all of its related methods, variables,
and constants, and sometimes other nested classes as well, which we will cover in the next section of this
chapter.

Free download pdf