Documentation



JavaFX: Working with JavaFX Graphics

4 SubScene

This chapter gives information about the use of SubScene API in JavaFX.

The SubScene node is a container for content in a scene graph. It is a special Node for scene separation. It can be used to render part of the scene with a different camera. You can use a SubScene node if you want to have Y-up for 3D objects and Y-down for 2D UI objects in your layout.

Some of the possible SubScene use cases are:

  • overlay for UI controls (needs a static camera)

  • Underlay for background (static or updated less frequently)

  • ”Heads-up” display

  • Y-up for your 3D objects and Y-down for your 2D UI.

Creating a SubScene

Example 4-1 shows the two constructors for creating a new instance of a SubScene node in your application.

Example 4-1 SubScene Constructors

// 
// Creates a SubScene for a specific root Node with a specific size.
//
public SubScene(Parent root, double width, double height)

// 
// Constructs a SubScene consisting of a root, with a dimension of width and 
// height, specifies whether a depth buffer is created for this scene and 
// specifies whether scene anti-aliasing is requested.

public SubScene(Parent root, double width, double height, boolean depthBuffer, 
         SceneAntialiasing antiAliasing)

Once you have created a SubScene, you can modify it by using the available methods to specify or obtain the height, root node, width, background fill of the SubScene, the type of camera used to render the SubScene, or whether the SubScene is anti-aliased.

Sample Use of SubScene

The CreateSubScene() method, shown in Example 4-2, illustrates how to use the second SubScene constructor listed above. This method is part of the MSAAApp.java sample application which is listed in Appendix A.

Example 4-2 Code Sample Using SubScene

...
SubScene msaa = createSubScene("MSAA = true", cylinder2,
                Color.TRANSPARENT,
                new PerspectiveCamera(), true);
...
...
private static SubScene createSubScene(String title, Node node,
            Paint fillPaint, Camera camera, boolean msaa) {
        Group root = new Group();
 
        PointLight light = new PointLight(Color.WHITE);
        light.setTranslateX(50);
        light.setTranslateY(-300);
        light.setTranslateZ(-400);
        PointLight light2 = new PointLight(Color.color(0.6, 0.3, 0.4));
        light2.setTranslateX(400);
        light2.setTranslateY(0);
        light2.setTranslateZ(-400);
 
        AmbientLight ambientLight = new AmbientLight(Color.color(0.2, 0.2, 0.2));
        node.setRotationAxis(new Point3D(2, 1, 0).normalize());
        node.setTranslateX(180);
        node.setTranslateY(180);
        root.getChildren().addAll(setTitle(title), ambientLight, light, light2, node);
 
        SubScene subScene = new SubScene(root, 500, 400, true, 
                msaa ? SceneAntialiasing.BALANCED : SceneAntialiasing.DISABLED);
        subScene.setFill(fillPaint);
        subScene.setCamera(camera);
 
        return subScene;
    }

The 3D Cubes and Xylophone samples that are available in the Graphics 3D section of the Ensemble 8 samples set also illustrate the use of the SubScene API. You can download the Ensemble 8 samples from the JavaFX Demos and Samples section of http://www.oracle.com/technetwork/java/javase/downloads/.

Close Window

Table of Contents

JavaFX: Working with JavaFX Graphics

Expand | Collapse