Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


are optional. These are in this example to show it’s parallel to the Car class, which I declared earlier without
using an interface. There is not too much difference, other than using the implements keyword, except that
implementing an interface tells the Java compiler to check and make sure that all of the necessary methods
that make the Car class work properly are included by the developer.


What’s New in Java 9: Modularity and Project Jigsaw


You might be wondering why I am covering Java 9 and its new modules last, and there are a couple of
reasons for this, which I will explain first before we get into what is new in Java 9. None of what is new
in Java 9 affects your game code, which is great because you can write the same basic Java game code in
Java 6, Java 7, and Java 8; this means your game can go places that are not yet using Java 9 and probably
won’t be for a while. Since 32-bit Android uses Java 6 and 64-bit Android uses Java 7 (Android 5 and 6)
and Java 8 (Android 7, 8 and later), this means you can write game logic in Java that spans a decade worth
of platforms. Since Java 9 is a couple of years late in its release originally planned for the fourth quarter
of 2015, I had to develop the code for this book, which releases the same time as Java 9, using Java 8.
Fortunately, Project Jigsaw (Java 9’s primary feature) affects the modularity of the programming language
and not the code inside of the modules, which stays the same as Java 8 and JavaFX 8. So for the purposes of
what this book is about, writing Pro Java Game Logic, there is no significant change between Java 8 and Java



  1. Whether or not a game is modularized using the Java 9 features does not affect performance (gameplay),
    only its distribution, so I’m covering this modularity feature last during this chapter, as it is the least
    important Java aspect regarding game performance.
    I did want to include this coverage of modules in Java because as of Java 9 modules are now a core
    feature, even though they only affect the packaging of a Pro Java Game and not how games are actually
    coded and optimized for memory and processor usage.


The Definition of a Java 9 Module: A Collection of Packages


The defining feature of Java 9 is JEP 200 (Modular JDK), which stands for JDK Enhancement Proposal



  1. This is an “umbrella” JEP over JEP 201 (Modular Source Code), JEP 220 (Modular RunTime), JEP 260
    (Encapsulate APIs), and JEP 261 (Module System), which encapsulate what needs to be accomplished to
    achieve a Modular JDK (JEP 200).
    Currently, Java 8 and JavaFX 8.0 are like having two different programming languages in one. The first
    thing that is therefore going to be modularized for Java 9 JDK is JavaFX 8 (now renamed JavaFX 9), and since
    that is what you are going to be using to create games, we’ll get into that in detail in this section. If you are
    an enterprise (business apps) Java 9 developer, this Java 9 module system will allow you to exclude all of the
    “heavy” JavaFX API libraries, packages, classes, and so on. However, the knife also cuts the other way as well,
    so if you are only going to be developing an i2D or i3D Java game, you will only need to declare and include
    the javafx.graphics module, and your distribution package (module) for your game will not need to include
    the plethora of other Java APIs that a game does not need, as it just focuses on graphics and event processing
    (multimedia visuals on the screen and how they interact with the player).
    A Java module contains a collection of Java packages, so Java modules add another hierarchical level
    above the Java package-class-method-variable hierarchy that exists currently. A Java package can belong
    to only one module, and you cannot split Java packages between modules. This makes your organization
    of packages even more important both for your own game and for JavaFX, which has been organized into
    packages and modules for you in Java 9. We will be learning about that later during this section of the
    chapter.
    Whereas Java packages allow you to organize by function, modules allow you to organize by features.
    This allows data (and code) footprint optimization. For instance, we will not be using JavaFX Swing,
    Standard UI Controls, FXML, or WebKit for our Pro Java 9 Game; thus, we will not need to include these code
    modules in our distribution.

Free download pdf