Java Reference
In-Depth Information
If you consult the JavaFX API documentation, you will notice that a Text instance, contained in package
javafx.scene.text , extends a Shape that extends a Node . As a consequence, a Text instance is a Node as well, and all
the properties on Node apply on Text as well. Moreover, Text instances can be used in the scene graph the same way
other nodes are used.
As you can detect from the example, a Text instance contains a number of properties that can be modified. Most
of the properties are self-explanatory, but again, it is always useful to consult the JavaFX API documentation when
manipulating objects.
Because all graphical elements in JavaFX directly or indirectly extend the Node class, and because the Node class
already contains many useful properties, the amount of properties on a specific graphical element such as Text can be
rather high.
In our example, we set a limited number of properties that we briefly explain next.
The method
textRef.setLayoutY(100)
applies a vertical translation of 100 pixels to the Text content.
The fill method is used to specify the color of the text.
While you're looking at the javafx.scene.text package in the API documentation, take a look at the font
function of the Font class, which is used to define the font family, weight, and size of the Text .
The textOrigin property specifies how the text is aligned with its area.
Referring again to the JavaFX API documentation, notice that the VPos enum (in the javafx.geometry package)
has fields that serve as constants, for example, BASELINE, BOTTOM, and TOP. These control the origin of the text with
respect to vertical locations on the displayed Text :
The TOP origin, as we're using it in the previous code snippet, places the top of the text
(including ascenders) at the layoutY position, relative to the coordinate space in which the
Text is located.
The BOTTOM origin would place the bottom of the text, including descenders (located in a
lowercase g, for example) at the layoutY position.
The BASELINE origin would place the baseline of the text (excluding descenders) at the
layoutY position. This is the default value for the textOrigin property of a Text instance.
The wrappingWidth property enables you to specify at what number of pixels the text will wrap.
The textAlignment property enables you to control how the text will be justified. In our example,
TextAlignment.JUSTIFY aligns the text on both the left and right sides, expanding the space between words to
achieve that.
The text that we're displaying is sufficiently long to wrap and be drawn on the Earth, so we need to define a
rectangular region outside of which that text cannot be seen.
We recommend you modify some of the values, recompile the example, and run it again. this will help you
understanding how the different properties work. alternatively, by using ScenicView you can inspect and modify the
different properties at runtime.
Tip
Working with Graphical Nodes as a Group
One powerful graphical feature of JavaFX is the ability to create scene graphs, which consist of a tree of graphical
nodes. You can then assign values to properties of a Group located in the hierarchy, and the nodes contained in the
Group will be affected. In our current example from Listing 1-1, we're using a Group to contain a Text node and to clip
 
 
Search WWH ::




Custom Search