Tutorials     About     RSS
Tech and Media Labs

JavaFX 3D

Jakob Jenkov
Last update: 2020-11-04

The JavaFX 3D graphics support is quite good. You can create predefined 3D shapes such as boxes, spheres etc. as well as create your own advanced 3D shapes. You can add texture to these 3D shapes, add lights to the scene, add a camera, rotate and translate the shapes and camera etc. This JavaFX 3D tutorial explains the basics of how the JavaFX 3D features work.

Basic JavaFX 3D Example

Here is a basic JavaFX 3D example. First the example checks if the platform the JavaFX application is running on supports 3D graphics from within JavaFX. Second, a JavaFX Box object is created. Third, a PerspectiveCamera is created and set on the JavaFX Stage.

import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class Gfx3DExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {

        boolean is3DSupported = Platform.isSupported(ConditionalFeature.SCENE3D);
        if(!is3DSupported) {
           System.out.println("Sorry, 3D is not supported in JavaFX on this platform.");
           return;
        }

        Box box = new Box(100,100,100);
        box.setCullFace(CullFace.NONE);
        box.setTranslateX(250);
        box.setTranslateY(100);
        box.setTranslateZ(400);

        boolean fixedEyeAtCameraZero = false;
        PerspectiveCamera camera = new PerspectiveCamera(fixedEyeAtCameraZero);
        camera.setTranslateX(150);
        camera.setTranslateY(-100);
        camera.setTranslateZ(250);

        Group root = new Group(box);
        root.setRotationAxis(Rotate.X_AXIS);
        root.setRotate(30);

        Scene scene = new Scene(root, 500, 300, true);
        scene.setCamera(camera);
        primaryStage.setScene(scene);
        primaryStage.setTitle("3D Example");

        primaryStage.show();
    }
}

Jakob Jenkov

Featured Videos

















Sponsored Ads

Maildroppa - Smart Email Marketing Solution
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next