JavaFX

JavaFX Scene Example

This is an example of JavaFX Scene class. The Scene class is the base container for all content in a scene graph. This class is defined in the javafx.scene package.

1. Overview

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 Scene is Color.WHITE. The setFill(Paint value) method sets the value. The Paint is 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 Node for the scene graph. The root can be a Group or a resizable node (layout Region or Control). The setRoot(Parent value) method sets the root value. Note that a null cannot 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 Application thread.

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: MouseEvent, DragEvent, KeyEvent, Rotate, ScrollEvent etc… Some related functions are: onMouseClicked, onKeyTyped, onMouseDragged, onRotate, 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 onKeyTyped.

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:

Figure 1 : Scene Example 1
Figure 1 : Scene Example 1

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 HBox layout.

The 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:

Figure 2: Scene Example 2
Figure 2: Scene Example 2

4. Download Java Source Code

This was an example of javafx.scene.Scene

Download
You can download the full source code of this example here: SceneExamples.zip

Prasad Saya

Prasad Saya is a software engineer with over ten years’ experience in application development, maintenance, testing and consulting on various platforms. He is a certified Java and Java EE developer. At present his interest is in developing Java applications. He also has experience working with databases and ERP applications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button