JavaFX 提供了哪些不同的 3D 形状?


一般来说,3D 形状是在 XYZ 平面绘制的几何图形。这些包括圆柱体、球体和长方体。

javafx.scene.shape.Shape3D 包提供了各种类,每个类都代表/定义了一个 3D 几何对象或对其的操作。名为 Shape3D 的类是 JavaFX 中所有三维形状的基类。

以下是您可以使用 JavaFX 绘制的各种几何形状:

  • 圆柱体 - 圆柱体是一个封闭的立体,它有两个平行的(大多是圆形的)底面,由一个曲面连接。

  • 球体 - 球体定义为在 3D 空间中与给定点距离 r 相同的所有点的集合。这个距离 r 是球体的半径,给定点是球体的中心。

  • 长方体 - 长方体是一个三维形状,具有长度(深度)、宽度和高度。

要创建所需的形状,您需要:

  • 实例化相应的类。

  • 设置其属性。

  • 将创建的对象添加到 Group。

示例

下面的 JavaFX 示例演示了所有可用 3D 形状的创建。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.stage.Stage;
import javafx.scene.shape.Box;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class JavaFX3DShapes extends Application {
   public void start(Stage stage) {
      //Drawing a Box
      Box cube = new Box(100, 120, 100);
      //Setting color and translating the box
      PhongMaterial material = new PhongMaterial();
      material.setDiffuseColor(Color.DARKRED);
      cube.setMaterial(material);
      cube.setTranslateX(30.0);
      cube.setTranslateY(180.0);
      cube.setTranslateZ(150.0);
      //Drawing a Cylinder
      Cylinder cylinder = new Cylinder(50, 150);
      //Setting the properties of the cylinder
      cylinder.setMaterial(material);
      cylinder.setTranslateX(250.0);
      cylinder.setTranslateY(180.0);
      cylinder.setTranslateZ(150.0);
      //Drawing a Sphere
      Sphere sphere = new Sphere(75);
      //Setting the properties of the sphere
      sphere.setMaterial(material);
      sphere.setTranslateX(480.0);
      sphere.setTranslateY(180.0);
      sphere.setTranslateZ(150.0);
      //Setting the perspective camera
      PerspectiveCamera cam = new PerspectiveCamera();
      cam.setTranslateX(-50);
      cam.setTranslateY(50);
      cam.setTranslateZ(0);
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 15);
      Text label1 = new Text("Box");
      label1.setFont(font);
      label1.setX(50);
      label1.setY(270);
      Text label2 = new Text("Cylinder");
      label2.setFont(font);
      label2.setX(210);
      label2.setY(270);
      Text label3 = new Text("Sphere");
      label3.setFont(font);
      label3.setX(400);
      label3.setY(270);
      //Setting the Scene
      Group root = new Group(cube, cylinder, sphere, label1,label2,label3);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      scene.setCamera(cam);
      stage.setTitle("Drawing 3D Shapes");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

输出

更新于:2020年5月16日

126 次浏览

启动您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.