Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 7 ■ IntroduCtIon to JavaFX 9: overvIew oF the JavaFX new MedIa engIne

JavaFX Scene Graph: Organizing Scenes by Using Parent Nodes


The Scene Graph, which is not at all unique to JavaFX, can now be seen in quite a few genres of new media
content creation software packages, such as 3D, digital audio, sound design, digital video, and special effects,
for instance. A Scene Graph is a visual representation for a content data structure that resembles an upside-
down tree, with the root node at the top and branch nodes and leaf nodes coming off of the root node.
The first time I saw the Scene Graph approach to scene design was when I was 3D modeling, rendering,
and animating using a software package on the Amiga 4000 called Real3D from RealSoft OY in Finland. This
approach has since been copied by a plethora of 3D, digital video, and special effects software packages since
then and is now the way that JavaFX organizes the content in its scenes. For this reason, many of you may
be familiar with, and therefore comfortable with, this design paradigm. Not only does the Scene Graph data
structure allow you to architect, organize, and design your JavaFX scene and its content, but it also allows you
to apply opacity, states, event handlers, transformations, and special effects to entire logical branches of
your Scene Graph hierarchy if you set your Scene Graph up correctly. Figure 7-3 shows a basic Scene Graph
tree, with the root node at the top and branch nodes and leaf nodes underneath that root node.


The root node is the topmost node, which is why it is called the root, even though it is at the top, rather
than at the bottom, like a root would be in the plant life world. A root node has no parent, that is, nothing
above it in the Scene Graph hierarchy. A root node is itself a parent to the branch nodes and leaf nodes that
are underneath it.
The next most powerful (and complex) construct in the Scene Graph tree is called the branch node,
which uses the javafx.scene.Parent class as its superclass and can contain children, which is logical since it
extends a class that is aptly named Parent. A branch node can contain other branch nodes, as well as “leaf ”
nodes, so it can be used to create some very complicated and very powerful Scene Graph hierarchy (or Scene
Graph architecture) constructs.
The last level in the hierarchy is the “leaf ” node, and a leaf node is the end of the branch. As such, a leaf
node can have no children. It is important to notice that leaf nodes can come directly off the root node, as
you can see in Figure 7-3. Branch nodes can be created by using the Parent, Group, or SubScene classes,
shown in Figure 7-2, or using any of their subclasses, such as the WebView, Region, Pane, or StackPane
classes, for instance.
Examples of objects that would be at the very end of the branches, that is, leaf nodes, include JavaFX
classes (instantiated as objects) that can be configured using parameters. Examples would include shapes,
text, or controls. These are design or content components in and of themselves, and therefore not designed


Figure 7-3. JavaFX Scene Graph hierarchy, starting with the root node and progressing to branch nodes and
leaf nodes

Free download pdf