Jubbat Jubbat - 3 years ago 133
Java Question

Setting up a camera in JavaFX for objects in a panel

I am building an application with JavaFX with my IDE and Java FX Scene Builder.

The "Scene" object is defined in the class that extends Application. Inside one of the nested panels of this class, I load another .fxml file.

This other .fxml file, which is paired with a Java class, contains some shape that I want to display.

If I add the camera to the Scene object, the camera applies to all the panels, menus and the rest of the elements in the GUI. I only want the camera to apply to the inner panel that contains the shape, but when I look up documentation the camera is always added to a Scene.

This is code in the Application class and my first attempt at adding a camera, which, as I explained before, gives strange results as the camera applies to everything inside the application window:

public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();

// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
addCameraAndLights(scene, null);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}

Answer Source

Use a SubScene to contain your 3D shape.

The SubScene class is the container for content in a scene graph. SubScene provides separation of different parts of a scene, each of which can be rendered with a different camera, depth buffer, or scene anti-aliasing. A SubScene is embedded into the main scene or another sub-scene.

You can set a camera on the sub scene separate from any camera applied on the root scene.

An example of SubScene use is provided in the answer to:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download