如何将fxml文件与3d Box和PerspectiveCamera结合

时间:2019-07-13 12:57:03

标签: java javafx 3d fxml

作为一个新手,我正在尝试做一些自己的项目来练习Java,JavaFX和Raspberry编码。我为自己设定的最新目标是3D可视化张拉力。我设法实现了简单的图表和实际压力(以圆圈为单位)的简单可视化。 (https://imagizer.imageshack.com/img922/2663/spBwMs.gif) 现在,我想将其表示为3D条形。 我试图从Ensemble理解一些示例:jnlps://download.oracle.com/otndocs/products/javafx/8/samples/Ensemble/Ensemble.jnlp 我设法做这样的事情: (https://imagizer.imageshack.com/img921/2766/12te0a.gif) 但我想使用FXML文件实现类似的功能。

我尝试对其进行编码,但到目前为止没有效果。我希望您使用一些Hbox彼此​​相邻放置2个盒子,但是我不知道将Camera正确放置在哪里。实际上,使用javaFX进行3D对我来说有点不可思议;)

Main.java:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root,800,800, true, SceneAntialiasing.BALANCED));
        primaryStage.show();
    }


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

Controller.java:


import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.fxml.FXML;
import javafx.scene.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.util.Duration;


public class Controller{

    @FXML
    private AnchorPane pane;
    @FXML
    private Box box;
    @FXML
    private PerspectiveCamera camera;


    final Rotate rx = new Rotate(0, Rotate.X_AXIS);
    final Rotate ry = new Rotate(0, Rotate.Y_AXIS);
    final Rotate rz = new Rotate(0, Rotate.Z_AXIS);

    private Timeline animation;

    @FXML
    void initialize() {


        box.setMaterial(new PhongMaterial(Color.ORANGE));
/*      box.setDepth(100);
        box.setWidth(100);
        box.setHeight(100);*/
        rx.setAngle(90);
        ry.setAngle(25);
        box.getTransforms().addAll(rz, ry, rx);

        animation = new Timeline();
        animation.getKeyFrames().addAll(
                new KeyFrame(Duration.ZERO,

                        new KeyValue(box.depthProperty(), 0d),
                        new KeyValue(box.translateYProperty(),400d)),
                new KeyFrame(Duration.seconds(5),

                        new KeyValue(box.depthProperty(), 800d),
                        new KeyValue(box.translateYProperty(), 0d)));

        animation.setCycleCount(Timeline.INDEFINITE);

        camera.getTransforms().addAll (
                new Rotate(-35, Rotate.X_AXIS),
                new Translate(0, 0, 10)
        );

        box.sceneProperty().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                box.getScene().setCamera(camera);
                box.sceneProperty().removeListener(this);
            }
        });
        animation.play();
    }

}

main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.PerspectiveCamera?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Box?>

<AnchorPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="check.brakes.Controller">
   <children>
      <Box fx:id="box" depth="100.0" height="100.0" layoutX="380.0" layoutY="673.0" width="100.0">

            </Box>
      <PerspectiveCamera fx:id="camera" farClip="70.0" fieldOfView="35.0" nearClip="0.5" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</AnchorPane>

我希望这些条看起来像我提供的示例一样。

更新

我做到了,我确定它并不完美,但是我必须从某种角度开始。

顺便说一句。

为什么是这样,当我调整应用程序窗口大小时,摄像头角度正在改变?

0 个答案:

没有答案