Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


It’s important to note that if you use FXML, which allows nonprogrammers to design the UI layouts, this
will require the javafx.fxml module, which needs the ability to access your private variables and methods.
This will require the exports private keyword string in your module-info.java declaration file, which would
then look like the following:


module BeginnerBoardGame.app {
requires javafx.media;
requires javafx.fxml;
exports private beginnerboardgame.pkg to javafx.fxml;
}


We are not using FXML in this Pro Java 9 Games Development book because we will be doing everything
with Java and external new media content development applications, just like a professional Java 9 game
developer would. This will keep your application more secure and more compact because the library for
using FXML is massive in size and scope; the same goes for the library for using CSS, HTML5, and JavaScript
(javafx.web) as well as the libraries for using generic user interface control or widget collections
(javafx.swing and javafx.controls).


Resource Encapsulation: Further Module Security Measures


Java 9 modularity not only covers your Java code but also your application resources, which in the case of a
Java 9 game include audio, video, image, vector (SVG), and animation assets. As you probably know, security
can be breached through these file formats as easily as it can through text file formats. The only issue
preventing Java from taking off in web, e-mail, apps, e-books, or iTV sets is the security issue, and it looks like
Oracle is determined to fix this very soon. Another issue with Java has been the deployment of a huge Java
runtime and the many different versions of this runtime. The new module feature will allow developers to
optimize their distribution to only include needed Java components.
Resources are encapsulated using the java.lang.Class class, and resources can be retrieved using only
the Class.getResource() method. As of Java 9, there is no ClassLoader.getResource() method call access,
as there was in previous Java versions.
You cannot use URLs to access resources in a class anymore either, so /path/name/voiceover.mp3 will
no longer work. This again makes the Java 9 distribution more secure. Some of the other modules still allow
URL access, such as javafx.web and javafx.media; however, we are going to use captive (that is, internal
to your JAR) media assets, and we are not going to open up our game to the Internet by not requiring the
javafx.web (WebKit API) module. The package containing the resource must be accessible (exported) for
the resources to be “visible” and not hidden from public view. This is done via your exports boardgame.
pkg to javafx.graphics; line of code. Since javafx.media is also dependent on javafx.graphics, exports
boardgame.pkg to javafxmedia; should also work since your chain of module requirements goes from
javafx.media ➤ javafx.graphics ➤ javafx.base, as you can see in Table 5-6.
If you wanted to externalize your new media and design assets (which I never do, for my Android
and Java 9 development), there are several JavaFX APIs that will still take a URL object or a URL specified
using a String URL value. These include CSS (Cascading Style Sheets in javafx.web); FXML (JavaFX Markup
Language in javafx.fxml); image, audio, and video assets (javafx.media and javafx.graphics); and HTML and
JavaScript (WebKit WebEngine javafx.web).


Summary


In this fifth chapter we reviewed some of the more important concepts and structures found in the Java
programming language. Certainly I cannot cover everything about Java in one chapter, so I stuck with key
concepts, constructs, and keywords that you will be using to create a game during this book. Most Java books

Free download pdf