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);
}
}輸出

廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP