JavaFX 3D 如何使用相对位置移动节点 [英] JavaFX 3D How to move node using relative positions

查看:22
本文介绍了JavaFX 3D 如何使用相对位置移动节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动化某些节点的移动.我想将一个节点移动到另一个节点的顶​​部,但我无法使用通用方法实现它.

I.E.我是这样写的:

 public Point3D getPosition(Node referenceNode, Node nodeToPlace) {边界 refBounds = referenceNode.getBoundsInParent();双 refX = refBounds.getMinX() + (refBounds.getWidth()/2);double refY = refBounds.getMaxY() + nodeToPlace.getBoundsInParent().getHeight();双 refZ = refBounds.getMinZ() + (refBounds.getDepth()/2);double nodeToPlaceX = nodeToPlace.getBoundsInParent().getMinX() +(nodeToPlace.getBoundsInParent().getWidth()/2);double nodeToPlaceY = nodeToPlace.getBoundsInParent().getMinY() +(nodeToPlace.getBoundsInParent().getHeigth()/2);double nodeToPlaceZ = nodeToPlace.getBoundsInParent().getMinZ() +(nodeToPlace.getBoundsInParent().getDepth()/2);双平移X = refX - nodeToPlaceX;双平移Y = refY - nodeToPlaceY;双平移Z = refZ - nodeToPlaceZ;nodeToPlace.getTransforms().add(new Translate(translationX,翻译Y, 翻译Z));}

我做错了什么?我想我没有考虑一些重要的事情,但我想不通.我希望有人能以正确的方式向我解释...提前致谢.

解决方案

基于在

import javafx.animation.Animation;导入 javafx.animation.KeyFrame;导入 javafx.animation.KeyValue;导入 javafx.animation.Timeline;导入 javafx.application.Application;导入 javafx.geometry.Point3D;导入 javafx.scene.Group;导入 javafx.scene.PerspectiveCamera;导入 javafx.scene.Scene;导入 javafx.scene.input.MouseEvent;导入 javafx.scene.input.ScrollEvent;导入 javafx.scene.paint.Color;导入 javafx.scene.paint.PhongMaterial;导入 javafx.scene.shape.Box;导入 javafx.scene.transform.Rotate;导入 javafx.stage.Stage;导入 javafx.util.Duration;/**@@see https://stackoverflow.com/a/37516327/230513* @see https://stackoverflow.com/a/37370840/230513*/公共类 TimelineMove 扩展应用程序 {私有静态最终双倍大小 = 300;私有最终内容内容 = Content.create(SIZE);公共无效播放(){content.animation.play();}私有静态最终类内容{私有静态最终持续时间 DURATION = Duration.seconds(4);私有静态最终整数 W = 64;私人最终组组=新组();私人最终旋转 rx = new Rotate(0, Rotate.X_AXIS);私人最终旋转 ry = new Rotate(0, Rotate.Y_AXIS);私人最终旋转 rz = new Rotate(0, Rotate.Z_AXIS);私人决赛框 b1;私人决赛框 b2;私人最终动画动画;私有静态内容创建(双倍大小){内容 c = 新内容(大小);c.group.getChildren().addAll(c.b1, c.b2);c.group.getTransforms().addAll(c.rz, c.ry, c.rx);c.rx.setAngle(12);c.ry.setAngle(-12);返回 c;}私人内容(双倍大小){Point3D p1 = new Point3D(-size/4, -size/4, size/4);b1 = createBox(Color.AQUA, p1);Point3D p2 = new Point3D(size/4, size/4, -size/4);b2 = createBox(Color.CORAL, p2);动画 = createTimeline(p1, p2);}私人盒子 createBox(颜色颜色,Point3D p){框 b = 新框 (W, W, W);b.setMaterial(新的PhongMaterial(颜色));b.setTranslateX(p.getX());b.setTranslateY(p.getY());b.setTranslateZ(p.getZ());返回 b;}私人时间线 createTimeline(Point3D p1, Point3D p2) {时间线 t = 新时间线();t.setCycleCount(Timeline.INDEFINITE);t.setAutoReverse(true);KeyValue keyX = new KeyValue(b1.translateXProperty(), p2.getX() - p1.getX());KeyValue keyY = new KeyValue(b1.translateYProperty(), p2.getY() - p1.getY());KeyValue keyZ = new KeyValue(b1.translateZProperty(), p1.getZ() - p2.getZ());KeyFrame keyFrame = new KeyFrame(DURATION, keyX, keyY, keyZ);t.getKeyFrames().add(keyFrame);返回 t;}}@覆盖public void start(Stage primaryStage) 抛出异常 {primaryStage.setTitle("JavaFX 3D");场景场景 = 新场景(content.group, SIZE * 2, SIZE * 2, true);primaryStage.setScene(场景);场景.setFill(颜色.黑色);PerspectiveCamera 相机 = new PerspectiveCamera(true);camera.setFarClip(SIZE * 6);相机.setTranslateZ(-2 * SIZE);场景.setCamera(相机);Scene.setOnScroll((final ScrollEvent e) -> {camera.setTranslateZ(camera.getTranslateZ() + e.getDeltaY());});primaryStage.show();玩();}公共静态无效主(字符串 [] args){发射(参数);}}

I am trying to automate some node's movements. I would like to move a node on top of another node but I am not able to achieve it using a generic method.

I.E. I wrote something like this:

    public Point3D getPosition(Node referenceNode, Node nodeToPlace) {
            Bounds refBounds = referenceNode.getBoundsInParent();
            double refX = refBounds.getMinX() + (refBounds.getWidth() / 2);
            double refY = refBounds.getMaxY() + nodeToPlace.getBoundsInParent().getHeight();
            double refZ = refBounds.getMinZ() + (refBounds.getDepth() / 2);

            double nodeToPlaceX = nodeToPlace.getBoundsInParent().getMinX() + 
                    (nodeToPlace.getBoundsInParent().getWidth()/2);
            double nodeToPlaceY = nodeToPlace.getBoundsInParent().getMinY() + 
                    (nodeToPlace.getBoundsInParent().getHeigth()/2);
            double nodeToPlaceZ = nodeToPlace.getBoundsInParent().getMinZ() + 
                    (nodeToPlace.getBoundsInParent().getDepth()/2);

            double translationX = refX - nodeToPlaceX;
            double translationY = refY - nodeToPlaceY;
            double translationZ = refZ - nodeToPlaceZ;

            nodeToPlace.getTransforms().add(new Translate(translationX,
            translationY, translationZ));
        }

What am I doing wrong? I suppose that I am not considering something of important but I can't figure it. I hope that someone can explain me the right way... Thanks in advance.

解决方案

Based on the first example seen here, the example below uses a Timeline to animate the motion of b1, shaded AQUA, toward b2, tinted CORAL.

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 @ @see https://stackoverflow.com/a/37516327/230513
 * @see https://stackoverflow.com/a/37370840/230513
 */
public class TimelineMove extends Application {

    private static final double SIZE = 300;
    private final Content content = Content.create(SIZE);

    public void play() {
        content.animation.play();
    }

    private static final class Content {

        private static final Duration DURATION = Duration.seconds(4);
        private static final int W = 64;
        private final Group group = new Group();
        private final Rotate rx = new Rotate(0, Rotate.X_AXIS);
        private final Rotate ry = new Rotate(0, Rotate.Y_AXIS);
        private final Rotate rz = new Rotate(0, Rotate.Z_AXIS);
        private final Box b1;
        private final Box b2;
        private final Animation animation;

        private static Content create(double size) {
            Content c = new Content(size);
            c.group.getChildren().addAll(c.b1, c.b2);
            c.group.getTransforms().addAll(c.rz, c.ry, c.rx);
            c.rx.setAngle(12);
            c.ry.setAngle(-12);
            return c;
        }

        private Content(double size) {
            Point3D p1 = new Point3D(-size / 4, -size / 4, size / 4);
            b1 = createBox(Color.AQUA, p1);
            Point3D p2 = new Point3D(size / 4, size / 4, -size / 4);
            b2 = createBox(Color.CORAL, p2);
            animation = createTimeline(p1, p2);
        }

        private Box createBox(Color color, Point3D p) {
            Box b = new Box(W, W, W);
            b.setMaterial(new PhongMaterial(color));
            b.setTranslateX(p.getX());
            b.setTranslateY(p.getY());
            b.setTranslateZ(p.getZ());
            return b;
        }

        private Timeline createTimeline(Point3D p1, Point3D p2) {
            Timeline t = new Timeline();
            t.setCycleCount(Timeline.INDEFINITE);
            t.setAutoReverse(true);
            KeyValue keyX = new KeyValue(b1.translateXProperty(), p2.getX() - p1.getX());
            KeyValue keyY = new KeyValue(b1.translateYProperty(), p2.getY() - p1.getY());
            KeyValue keyZ = new KeyValue(b1.translateZProperty(), p1.getZ() - p2.getZ());
            KeyFrame keyFrame = new KeyFrame(DURATION, keyX, keyY, keyZ);
            t.getKeyFrames().add(keyFrame);
            return t;
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("JavaFX 3D");
        Scene scene = new Scene(content.group, SIZE * 2, SIZE * 2, true);
        primaryStage.setScene(scene);
        scene.setFill(Color.BLACK);
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setFarClip(SIZE * 6);
        camera.setTranslateZ(-2 * SIZE);
        scene.setCamera(camera);
        scene.setOnScroll((final ScrollEvent e) -> {
            camera.setTranslateZ(camera.getTranslateZ() + e.getDeltaY());
        });
        primaryStage.show();
        play();
    }

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

这篇关于JavaFX 3D 如何使用相对位置移动节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆