Session 7 |
Scene graph
A JavaFX scene graph is a starting point in constructing a JavaFX application. The scene graph is a hierarchical tree of nodes that represents all of the visual elements of the application's user interface. A single element in the scene graph is called a node. Each node is classified as either a branch node (meaning that it can have children), or a leaf node (meaning that it cannot have children). The first node in the tree is always called the root node, and it never has a parent. Study the general inheritance diagram:
The JavaFX API
defines a number of classes that can act as the root, a branch or a leaf node. However, three of them are the most significant:
Node
: the abstract base class for all scene graph nodes.Parent
: the abstract base class for all branch nodes. (This class directly extendsNode
class).Scene
: the base container class for all content in the scene graph.
These base classes define important functionality that is subsequently inherited by subclasses (including the paint order, visibility, composition of transformations, support for CSS styling, and so on). Some branch node classes that inherit directly from the Parent class are Group
, Region
, and WebView
. The leaf node classes are defined throughout a number of additional packages, such as javafx.scene.shape and javafx.scene.text.
Let's try to compose the scene graph for our BlackSquare application.
The scene graph is very laconic in this case. The Group type object acts as the root node. The Rectangle type object is a leaf node because it does not (and cannot) have any children. The scene graph cannot be even more reduced because the class Rectangle itself cannot be the root node (this class is not a subclass of Parent
class).
Used resources:
Session 7 |