28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

<strong>Java</strong> APIs: Using Packages to Organize by Function<br />

At the highest level of a programming platform, such as Google’s 32-bit Android 4, which uses <strong>Java</strong> SE 6; 64-bit<br />

Android 5, which uses <strong>Java</strong> SE 7; and the current Oracle <strong>Java</strong> SE platform, which was recently released as <strong>Java</strong> SE<br />

8, there is a collection of packages that contain classes, interfaces, methods, and constants and that together form<br />

the API. This collection of <strong>Java</strong> code (in this case, the <strong>Java</strong> 8 API) can be used by application (in this case, game)<br />

developers to create professional-level software across many OSs, platforms, and consumer electronics devices, such<br />

as computers, laptops, netbooks, notebooks, tablets, iTV sets, game consoles, smartwatches, and smartphones.<br />

To install a given version of an API level, you install the SDK (Software <strong>Development</strong> Kit). The <strong>Java</strong> SDK has a<br />

special name, the JDK (<strong>Java</strong> <strong>Development</strong> Kit). Those of you who are familiar with Android (<strong>Java</strong> SE on top of Linux)<br />

OS development know that a different API level is released every time a few new features are added. This is because<br />

these new hardware features need to be supported, not because Google’s executives feel like releasing a new SDK<br />

every few months. Android has 24 different API levels, whereas <strong>Java</strong> SE has only eight, and only three of <strong>Java</strong>’s API<br />

levels (<strong>Java</strong> 6, <strong>Java</strong> 7, <strong>Java</strong> 8) are in use currently.<br />

<strong>Java</strong> SE 6 is used with Eclipse ADT (Android Developer Tools) IDE to develop for 32-bit Android (versions 1.5<br />

through 4.5); <strong>Java</strong> SE 7 is used with IntelliJ IDEA to develop for 64-bit Android (version 5.0 and later); and <strong>Java</strong> 8 is<br />

used with the NetBeans IDE to develop for <strong>Java</strong>FX and <strong>Java</strong> 8 across the Windows, Mac OS X, Linux, and Oracle Solaris<br />

OSs. I have three different workstations that are optimized for each of these <strong>Java</strong> API platforms and IDE software<br />

packages so that I can develop applications for Android 4 (<strong>Java</strong> 6), Android 5 (<strong>Java</strong> 7), and <strong>Java</strong>FX (<strong>Java</strong> 8) at the same<br />

time. Fortunately, you can get a powerful Windows 8.1 hexacore or octacore 64-bit AMD workstation on PriceWatch.<br />

com for a few hundred dollars!<br />

Besides API level (the SDK you installed and are using), the highest-level construct in the <strong>Java</strong> programming<br />

language is the package. A <strong>Java</strong> package uses the package keyword to declare the application’s package at the top of<br />

your <strong>Java</strong> code. This must be the first line of code declared, other than comments (see Figure 3-1; see also Chapter 2).<br />

The New Project series of dialogs in NetBeans that you used in Chapter 2 will create your package for you and will<br />

import other packages that you will need to use, based on what you want to do in your application. In this case, these<br />

are <strong>Java</strong>FX packages, so you can use the <strong>Java</strong>FX new media engine.<br />

As you may have ascertained from the name, a <strong>Java</strong> package collects all the <strong>Java</strong> programming constructs. These<br />

include classes, interfaces, and methods that relate to your application, so the invinciBagel package will contain all your<br />

code, as well as the code that you imported to work with your code, to create, compile, and run the InvinciBagel game.<br />

A <strong>Java</strong> package is useful for organizing and containing all your own application code, certainly, but it is even<br />

more useful for organizing and containing the SDK’s (API’s) <strong>Java</strong> code, which you will use, along with your own <strong>Java</strong><br />

programming logic, to create your <strong>Java</strong> 8 applications. You can use any of the classes that are part of the API that you<br />

are targeting by using the <strong>Java</strong> import keyword, which, in conjunction with the package and class that you want to<br />

use, constitutes an import statement.<br />

The import statement begins with the import keyword, followed by the fully qualified class name, which is the<br />

package name, any subpackage name, and the class name as a complete naming reference path (the full proper<br />

name for the class). A semicolon terminates an import statement. As you have already seen in Figure 3-1, the import<br />

statement used to import the <strong>Java</strong>FX EventHandler class from the javafx.event package should look just like this:<br />

import javafx.event.EventHandler;<br />

The import statement tells the <strong>Java</strong> compiler that you will be using methods (or constants) from the class that is<br />

referenced, using the import keyword, as well as which package the class is stored in. If you use a class, method, or<br />

interface in your own <strong>Java</strong> class, such as the InvinciBagel class (see Figure 3-2), and you have not declared the class<br />

for use, using the import statement, the <strong>Java</strong> compiler will throw an error until you add the required import statement<br />

at the top of the class (after the <strong>Java</strong> package declaration statement, and before the <strong>Java</strong> class declaration statement).<br />

47

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

Saved successfully!

Ooh no, something went wrong!