Friday, October 16, 2015

Smooth 3D shapes in JavaFX 8

Here is the code:

package vu.co.kspace;

/**
 * Created by IDEA on 28/08/15.
 */
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.shape.Box;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;

public class PreDefinedShapes extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        // Create a Box
        Box box = new Box(100, 100, 100);
        box.setTranslateX(150);
        box.setTranslateY(0);
        box.setTranslateZ(400);

        // Create a Sphere
        Sphere sphere = new Sphere(50);
        sphere.setTranslateX(300);
        sphere.setTranslateY(-5);
        sphere.setTranslateZ(400);

        // Create a cylinder
        Cylinder cylinder = new Cylinder(40, 120);
        cylinder.setTranslateX(500);
        cylinder.setTranslateY(-25);
        cylinder.setTranslateZ(600);

        // Create a light
        PointLight light = new PointLight();
        light.setTranslateX(550);
        light.setTranslateY(500);
        light.setTranslateZ(200);

        // Add shapes and a light to the group
        Group root = new Group(box, sphere, cylinder, light);

        // Craete a Scene with depth buffer enabled
        Scene scene = new Scene(root, 600, 800, true, SceneAntialiasing.BALANCED);

        // Set a camera to view the 3D shapes
        PerspectiveCamera camera = new PerspectiveCamera(false);
        camera.setTranslateX(100);
        camera.setTranslateY(-550);
        camera.setTranslateZ(200);
        scene.setCamera(camera);

        stage.setScene(scene);
        stage.setTitle("Using 3D Shapes: Box, Sphere and Cylinder");
        stage.show();
    }
}

Antialiasing is set bySceneAntialiasing.BALANCED . Documentation can be seen here

enter image description here

0 comments: