The following are the main properties for the scene class:
- fill: The fill property specifies the background of the scene. The default fill of the
setFill(Paint value)method sets the value. The
Paintis the base class for a color or gradients used to fill shapes and backgrounds when rendering the scene graph.
- root: The root property specifies the application’s root
Nodefor the scene graph. The root can be a
Groupor a resizable node (layout
setRoot(method sets the root value. Note that a
nullcannot be specified for the root property.
- size: The scene’s size may be initialized by the application during construction. If no size is specified, the scene will automatically compute its initial size based on the preferred size of its content.
- depthBuffer and antiAliasing flags: These are conditional features.
The root, fill, size properties can also be specified while constructing the scene as constructor arguments. An application may also specify depth buffer support or scene anti-aliasing support at the creation of a scene. Note that the scene objects must be constructed and modified on the JavaFX
The scene also defines a number of functions to be called when an event occurs on this scene. The following are some of the events:
ScrollEvent etc… Some related functions are:
onScroll, etc.. For example, a
KeyEvent indicates that a keystroke occurred in a
Node. The scene’s
setOnKeyTyped(EventHandler value) method sets the value of the property
The examples in this article show the usage of some properties including, fill, root, size and an event function.
2. Scene Example 1
This example shows the basic scene without any properties set. The default fill and size properties are set by the JavaFX system. The default fill of the scene
Color.WHITE is seen here. The
Group is specified as the root
Node of the scene graph.
The following code snippet shows the construction:
Group root = new Group(); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show();
The code when executed displays a window as shown below:
3. Scene Example 2
This example shows a scene with some properties (fill, size, cursor), and
onMouseClicked function. The following code snippet shows the scene’s construction:
scene = new Scene(hbox, 600, 300); scene.setFill(Color.DARKCYAN); scene.setCursor(Cursor.DEFAULT); scene.setOnMouseClicked(new MouseClickListener());
From the above code, note that the scene’s constructor specifies the size and the root properties. The root property type is a
setOnMouseClicked() property is set with an event handler class
MouseClickListener. The listener class implements
EventHandler<MouseEvent> interface. In the GUI, when a mouse-click occurs in the scene area the scene’s background color (fill property) and the cursor’s shape are changed (
Cursor defines the mouse cursor for this scene).
The following is the GUI for this example:
4. Download Java Source Code
This was an example of
You can download the full source code of this example here: SceneExamples.zip