我第一次使用 JavaFX 尝试制作一个应用程序,我可以用它来演示带有按钮控件的简单动画。为此,我在初级阶段使用了 BoarderPane,左侧、右侧和底部都使用了 Gridpanes。
但是,对于中心,我需要能够绘制一个球体,其中有一条线,我可以针对不同的 View 进行旋转,同时能够对其中的线进行动画处理,或者至少快速移动。
我试过使用 Pane 作为中心,但不起作用。我试过让它成为自己的场景和子场景,但这是行不通的。我不能使用 Canvas ,因为它仅用于 2D 动画。
有没有一种方法可以在保持我创建的 BoarderPane 布局的同时为线条设置动画或旋转相机?
我之前曾尝试查看以下内容以了解我可以做什么,但大多数似乎与 BoarderPane 不兼容:
JavaFX Rotating Camera Around a Pivot
JavaFX Canvas rotate image with fixed center (and without bouncing)
最佳答案
每当您想混合 2D 和 3D(和相机)时,您必须使用 SubScene
container对于 3D 内容:
SubScene provides separation of different parts of a scene, each of which can be rendered with a different camera, depth buffer, or scene anti-aliasing. A SubScene is embedded into the main scene or another sub-scene.
如果你有一个 BorderPane 容器,你可以完美地将子场景添加到它的中心。
对于类似的用例,您可以查看 here 中的 Qubit3D
类,主要是一个包含球体和圆柱体子场景的组(均来自 FXyz 3D library)。
您可以轻松地将这个组添加到您的 2D 场景中:
private final Rotate rotate = new Rotate(0, 0, 0, 0, javafx.geometry.Point3D.ZERO.add(1, 1, 1));
@Override
public void start(Stage primaryStage) throws Exception {
final Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(10),
new KeyValue(rotate.angleProperty(), 360)));
final Qubit3D qubit = new Qubit3D();
final BorderPane root = new BorderPane(qubit);
final Button buttonAnimate = new Button("Animate");
buttonAnimate.setOnAction(e -> {
rotate.setAngle(0);
timeline.playFromStart();
});
root.setLeft(new StackPane(buttonAnimate));
final Button buttonStop = new Button("Stop");
buttonStop.setOnAction(e -> timeline.stop());
root.setRight(new StackPane(buttonStop));
Scene scene = new Scene(root, 600, 400, true, SceneAntialiasing.BALANCED);
scene.setFill(Color.BISQUE);
primaryStage.setScene(scene);
primaryStage.setTitle("Qubit3D Sample");
primaryStage.show();
qubit.rotateRod(rotate);
}
我添加到 Qubit3D 的唯一修改是:
public void rotateRod(Rotate rotate) {
rodSphere.getTransforms().setAll(rotate);
}
如果你运行它:
请注意,您可以与球体交互(通过鼠标拖动事件),同时您还可以开始/停止球体和杆的完整旋转。
关于JavaFX 动画/带 Pane 的 3d View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51194308/