Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples

Between these three JavaFX modules (base, graphics, and media) you have everything you need for
games in Java 9, as long as you make your own user interface element control graphics, which is standard
practice in games development. We’ll be taking a look at what packages, classes, and methods are contained
within these core modules over the course of the book. When I cover a given package and classes, I will let
you know what module it is a part of.


The Purpose of Java 9 Modules: Secure, Strong Encapsulation


One of the biggest complaints over the years has been that Java is not as secure as other platforms, and that
has prevented widespread use as a digital content distribution avenue. Like all the distribution formats
(Kindle, Android, HTML5, etc.), secure DRM is also needed and on the way next year, but internal security of
the API itself is also an issue that needs to be addressed in Java in particular, and as you’ll see in this section,
Java 9 Modules Strong Encapsulation rules will go a long way toward doing this by preventing access to the
Java internal API as well as private encapsulation.
Applications previous to Java 9 will have to be modularized to “lock them down” using the
module-info.java definition file discussed previously. Only explicitly exported packages will be visible;
the internal API used to create an application will not be visible (accessible) anymore. Attempting to
access any type in a package not explicitly exported will throw an error. Advanced programmers will not
be able to use reflection to call a .setAccessible() method to force access. For testing, there is currently a
command-line switch that will allow access to nonexported packages. This will be called --add-exports
and should be used only when absolutely necessary.
There is also increased security within exported packages because now only types declared using a
public access control modifier will be accessible. Attempting to access any type in an explicitly exported
package not declared using public access control throws an error. Programmers will not be able to use
reflection to call a .setAccessible() method to force access. For testing, there is currently a command-line
switch that will allow access to nonpublic types. As you might have guessed, this will be called --add-
exports-private and should be used only when absolutely necessary.
Strong encapsulation allows you to selectively support or expose only portions (modules) of the API. In
this case, we are using the JavaFX API, and I am going to show you how to create a robust i3D game in this
book using only the two core JavaFX packages (base and graphics) and the MediaPlayer (media) package,
along with highly optimized new media assets. We will not need to include the “heavy” WebKit (web), Swing
(swing), FXML (fxml), or UI (controls) packages, which will reduce the distribution file data footprint and
increase the security of this Pro Java 9 Game. Each module you access (require) will list its publicly exported
packages, and your public classes and methods will be part of the API.


Creating a Pro Java 9 Game Module: Using the Exports Keyword


Let’s continue looking at how to set up the Java 9 Game we will be creating using the JavaFX API by looking
at how we use the exports keyword to add your BoardGame package to the rest of the Java (JavaFX) APIs
we are going to be using to create the game. The JavaFX launcher will construct an instance of your
application subclass, which will be called BoardGame or something similar. You can see this in Figure 5-4
for the InvinciBagel i2D game created in Beginning Java 8 Games Development. You can export the package
that contains this Application subclass to the javafx.graphics module (which contains the majority of the
packages and classes that will comprise this game) using an exports keyword and package name and a to
keyword and module name. Here’s the Java code:


module BoardGame.app {
requires javafx.media;
exports boardgame.pkg to javafx.graphics;
}

Free download pdf