栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaFX 3D如何使用相对位置移动节点

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaFX 3D如何使用相对位置移动节点

根据此处看到的第一个示例,下面的示例使用aTimeline来动画化b1着色AQUA,朝向b2,朝着色的运动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;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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号