4 サブシーン
この章では、JavaFXでのSubScene APIの使用について説明します。
SubSceneノードは、シーン・グラフ内の内容のコンテナです。これは、シーンを分離するための特別なノードです。カメラが異なるシーンの一部をレンダリングするために使用できます。レイアウト内で3DオブジェクトのY-upと2D UIレイアウトのY-downが必要な場合に、SubSceneノードを使用できます。
使用できるサブシーンのユースケースには、次のようなものがあります。
-
UIコントロールのオーバーレイ(静止カメラが必要)
-
背景のアンダーレイ(静止か、または頻度の低い更新)
-
ヘッドアップ表示
-
3DオブジェクトのY-upと2D UIのY-down
サブシーンの作成
例4-1に、アプリケーションでSubSceneノードの新しいインスタンスを作成するための2つのコンストラクタを示します。
例4-1 SubSceneのコンストラクタ
// // 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)
サブシーンの作成後に変更するには、サブシーンの高さ、ルート・ノード、幅、背景の塗りつぶし、サブシーンをレンダリングするために使用するカメラのタイプ、またはサブシーンをアンチエイリアス処理するかどうかを指定または取得するために使用できるメソッドを使用します。
サブシーンの使用例
例4-2に示すCreateSubScene()
メソッドは、前述の2番目のSubSceneのコンストラクタを使用する方法を示しています。このメソッドは、付録Aに記載されているMSAAApp.javaサンプル・アプリケーションの一部です。
例4-2 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; }
Ensemble 8サンプル・セットの「Graphics 3D」セクションで利用できる3Dキューブおよび木琴のサンプルでも、SubScene
APIの使用が示されています。Ensemble 8サンプルは、http://www.oracle.com/technetwork/java/javase/downloads/
の「JavaFX Demos and Samples」セクションからダウンロードできます。