Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 9 ■ JavaFX 9 User InterFaCe DesIgn: the Front enD For Java 9 game DesIgn


At least initially, for learning purposes, I am going to do this the long way, and I will always “explicitly”
load Image objects using the Image() constructor method so that we can specify all of the different attributes
and so that you can see all of the different image assets that you’re using in your Java 9 programming logic.
I wanted to show you the shortcut code here because you might want to use this shortcut approach later
if you start using ImageViews as 2D sprites. You can use this shortcut approach with your sprites because
you will not be scaling them and because they are so highly optimized that the background loading option,
which saves long loading times, won’t be necessary.


JavaFX TextFlow Class: Use Text Objects (Content) in a Design


The TextFlow class is a public class that allows the developer to create a text paragraph. A text paragraph is a
container for multiple lines of text, each of which is delimited using a “new line” character, denoted using an
“escape n” sequence in your Java code.
The TextFlow class would therefore use the following Java class inheritance hierarchy:


java.lang.Object



javafx.scene.Node
javafx.scene.Parent
javafx.scene.layout.Region
javafx.scene.layout.Pane
javafx.scene.text.TextFlow



This TextFlow object is the type of Node object in the JavaFX Scene Graph that can be used for
rendering text paragraphs using the data contained in a Text object, in much the same way an ImageView
can render data contained in an Image object. The TextFlow can handle more than one Text object at a
time, however, allowing you to style different Text objects differently using method calls like .setFill() and
.setFont(). TextFlow is a specialized text layout class designed to render what’s commonly referred to as rich
text format (RTF). Some call this desktop publishing, and it involves using different fonts, styles, or color to
enhance the presentation of text-based content. It’s interesting to note that javafx.scene.text is kept in the
javafx.graphics module and not in the javafx.controls module. This is significant, because if you wanted to
optimize out (not use) the JavaFX 9 UI Control classes (100 classes or more), you could still create your own
UI elements using Image, ImageView, Text, and 3D geometry objects using only the javafx.base and javafx.
graphics modules, which give you everything you need to create pro Java 9 i3D games.
A TextFlow object can be used to lay out a number of Text nodes within a single TextFlow object.
A TextFlow object uses the text and the font and style settings for each of the Text Node objects inside of it,
plus its own maximum width and text alignment style properties, to determine the location for rendering
each child Text object.
A single Text node can span several lines because of the wrapping capability of the TextFlow object, and
a visual location of a Text node can differ from the logical location because of bidirectional (bidi) reordering.
The Java Bidi object provides information on the bidirectional reordering of the text used to create it. This
is required, for example, to properly display Arabic or Hebrew text, which is read from right to left (RTL)
instead of left to right (LTR).
Any other Node object type, other than a Text Node object, of course, will be treated as an embedded
“rich content” object within the TextFlow object’s layout. It will be inserted in the content using its preferred
width, height, and baseline offset values to space and align it relative to the other Text objects within the
parent TextFlow object.
When a Text Node object is inside of a TextFlow object, some its properties will be ignored. For example,
the X and Y properties of a Text Node object will be ignored since the location of the child Text Node is
determined by the Parent TextFlow object. Likewise, the wrapping width in the Text node will be ignored
since the maximum width used for wrapping will inherit the TextFlow object’s maximum width property.

Free download pdf