Java Reference
In-Depth Information
Taking a look at the code, in the
main()
method's entry point you launch the
JavaFX application by simply passing in the command-line arguments to the
Ap-
plication.launch()
method. Once the application is in a ready state, the frame-
work internals will invoke the
start()
method to begin. When the
start()
meth-
od is invoked, a JavaFX
javafx.stage.Stage
object is available for the deve-
loper to use and manipulate.
You'll notice that some objects are oddly named, such as
Stage
and
Scene
. The
designers of the API have modeled things similar to a theater or a play in which actors
perform in front of an audience. With this same analogy, in order to show a play, there
are basically one-to-many scenes that actors perform in. And, of course, all scenes are
performed on a stage. In JavaFX the
Stage
is equivalent to an application window
similar to Java Swing API
JFrame
or
JDialog
. You may think of a
Scene
object
as a content pane capable of holding zero-to-many
Node
objects. A
Node
is a funda-
mental base class for all scene graph nodes to be rendered. A scene graph is a tree data
structure that maintains an internal model of all nodes or graphical objects that are part
of an application. Commonly used nodes are UI controls and
Shape
objects. Similar
to a tree data structure, a scene graph will contain children nodes by using a container
class
Group
. You'll learn more about the
Group
class later when you look at the
Ob-
servableList
, but for now think of them as Java
List
s or
Collection
s that
are capable of holding
Node
s.
Once the child nodes have been added, you set the
primaryStage
's (
Stage
)
scene and call the
show()
method on the
Stage
object to show the JavaFX window.
One last thing: in this chapter most of the example applications are structured the
same as this example, in which recipe code solutions will reside inside the
start()
method. Most of the recipes in this chapter follow the same pattern. For the sake of
brevity, much of the boilerplate code is not shown. To see the full source listings of all
the recipes, download the source code from the topic's website.
14-2. Drawing Text
Problem
You want to draw custom text within a JavaFX application.
Solution