About 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.

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.

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

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

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.