JavaFX 3D 可视化编程一

 JavaFx 提供了类似QT的界面设计方法,JavaFX 3D是JavaFX 与Java3D的结合,对三维可视化更方便。

下图是JavaFX 3D中的坐标系,

X从左到右,Y从上到下,Z从近到远。

JavaFX 3D coordinates

JavaFX 3D 提供了四种基本类型,球是其中一种,下面这个例子展示以下功能:

(1)如何创建三维效果的球

(2)如何移动球的位置

(3)如何设置相机

(4)如何设置画布

(5)如何通过键盘移动物体的位置,

package sample;

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;

public class Main extends Application {

    private static final int WIDTH=1400;
    private  static  final  int HEIGHT=800;

    @Override
    public void start(Stage primaryStage) throws Exception{
        Sphere sphere = new Sphere( 50);
        Group group = new Group();
        group.getChildren().add(sphere);

        Camera camera = new PerspectiveCamera();

        Scene scene =new Scene(group,  WIDTH,  HEIGHT);
        scene.setFill(Color.SILVER);
        scene.setCamera(camera);

        sphere.translateXProperty().setValue(WIDTH/2);
        sphere.translateYProperty().setValue(HEIGHT/2);

        primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event->{
            switch (event.getCode()){
                case W:
                    sphere.translateZProperty().set(sphere.getTranslateZ()+100);
                    break;
                case S:
                    sphere.translateZProperty().set(sphere.getTranslateZ()-100);
                    break;
            }
        });


        primaryStage.setTitle("Sphere3D");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

效果如下

javafx sphere3d

 下面的例子与上面的例子效果相同,但是方式不同。

下面这个例子是通过移动相机的位置,而不是移动物体的位置,达到放大缩小的效果。

package chapt2;

import javafx.application.Application;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;

public class Cmaera3D extends Application {

    private static final int WIDTH=1400;
    private  static  final  int HEIGHT=800;

    @Override
    public void start(Stage primaryStage) throws Exception{
        Sphere sphere = new Sphere( 50);
        Group group = new Group();
        group.getChildren().add(sphere);

        Camera camera = new PerspectiveCamera(true);

        Scene scene =new Scene(group,  WIDTH,  HEIGHT);
        scene.setFill(Color.SILVER);
        scene.setCamera(camera);

        camera.translateXProperty().setValue(0);
        camera.translateYProperty().setValue(0);
        camera.translateZProperty().setValue(-500);

        camera.setNearClip(1);
        camera.setFarClip(100000);

        primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event->{
            switch (event.getCode()){
                case W:
                    camera.translateZProperty().set(camera.getTranslateZ()+100);
                    break;
                case S:
                    camera.translateZProperty().set(camera.getTranslateZ()-100);
                    break;
            }
        });


        primaryStage.setTitle("Camera3D");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

 

posted @ 2022-08-21 10:13  Oliver2022  阅读(596)  评论(1编辑  收藏  举报