add JavaFX 3D Navigation - Java javafx.geometry

Java examples for javafx.geometry:Point2D

Description

add JavaFX 3D Navigation

Demo Code


    //package com.java2s;

    import javafx.geometry.Point2D;
    import javafx.geometry.Point3D;
    import javafx.scene.*;

    import javafx.scene.transform.Rotate;

    public class Main {
        public static void add3DNavigation(SubScene scene, Node node) {

    new Runnable() {

        private Point2D mousePoint = new Point2D(0, 0);
        private Rotate xRotate = new Rotate(0, 0, 0, 0, new Point3D(1, 0, 0));
        private Rotate yRotate = new Rotate(0, 0, 0, 0, new Point3D(0, 1, 0));

        @Override//from  w  w  w  .ja  v a 2s . c  om
        public void run() {
            node.getTransforms().addAll(xRotate, yRotate);

            scene.setOnMousePressed(e -> {
                mousePoint = new Point2D(e.getSceneX(), e.getSceneY());
            });

            scene.setOnZoom((e) -> {
                System.out.println(node.getTranslateZ());
                node.setTranslateZ(node.getTranslateZ() + (1.0 - e.getZoomFactor()) * 100);
            });

            scene.setOnMouseDragged(e -> {
                double mouseDeltaX = (e.getSceneX() - mousePoint.getX());
                double mouseDeltaY = (e.getSceneY() - mousePoint.getY());
                mousePoint = new Point2D(e.getSceneX(), e.getSceneY());

                xRotate.setAngle(xRotate.getAngle() - mouseDeltaY * 0.1 * 2.0);
                yRotate.setAngle(yRotate.getAngle() + mouseDeltaX * 0.1 * 2.0);
            });
        }
    }.run();
}

        public static void add3DNavigation(Scene scene, Node node) {
    new Runnable() {

        private Point2D mousePoint = new Point2D(0, 0);
        private Rotate xRotate = new Rotate(0, 0, 0, 0, new Point3D(1, 0, 0));
        private Rotate yRotate = new Rotate(0, 0, 0, 0, new Point3D(0, 1, 0));

        @Override
        public void run() {
            node.getTransforms().addAll(xRotate, yRotate);

            scene.setOnMousePressed(e -> {
                mousePoint = new Point2D(e.getSceneX(), e.getSceneY());
            });

            scene.setOnZoom((e) -> {
                System.out.println(node.getTranslateZ());
                node.setTranslateZ(node.getTranslateZ() + (1.0 - e.getZoomFactor()) * 100);
            });

            scene.setOnMouseDragged(e -> {
                double mouseDeltaX = (e.getSceneX() - mousePoint.getX());
                double mouseDeltaY = (e.getSceneY() - mousePoint.getY());
                mousePoint = new Point2D(e.getSceneX(), e.getSceneY());

                xRotate.setAngle(xRotate.getAngle() - mouseDeltaY * 0.1 * 2.0);
                yRotate.setAngle(yRotate.getAngle() + mouseDeltaX * 0.1 * 2.0);
            });
        }
    }.run();
}
    }

Related Tutorials