如何在 JavaFX 中建立一個球體 (3D)?


球體是定義為一個點到三維空間中給定點的距離均為 r 的點集。此距離 r 即為球體的半徑,給定點即為球體中心。

在 JavaFX 中,球體由 javafx.scene.shape.Sphere 類表示。此類包含一個名為 radius 的屬性。此屬性表示圓柱體的半徑,你可以使用 setRadius() 方法為該屬性設定值。

若要建立 3D 盒體,你需要 −

  • 例項化此類。

  • 使用設定器方法或將其作為建構函式的引數傳遞設定必需的屬性。

  • 將建立的節點(形狀)新增到 Group 物件。

示例

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.CullFace;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.Sphere;
public class DrawingSphere extends Application {
   public void start(Stage stage) {
      //Drawing a sphere
      Sphere sphere = new Sphere();
      //Setting the properties of the Box(cube)
      sphere.setRadius(140.0);
      //Setting other properties
      sphere.setCullFace(CullFace.BACK);
      sphere.setDrawMode(DrawMode.FILL);
      PhongMaterial material = new PhongMaterial();
      material.setDiffuseColor(Color.BROWN);
      sphere.setMaterial(material);
      //Translating
      sphere.setTranslateX(300.0);
      sphere.setTranslateY(150.0);
      sphere.setTranslateZ(150.0);
      //Setting the perspective camera
      PerspectiveCamera cam = new PerspectiveCamera();
      cam.setTranslateX(-50);
      cam.setTranslateY(25);
      cam.setTranslateZ(0);
      //Setting the Scene
      Group root = new Group(sphere);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      scene.setCamera(cam);
      stage.setTitle("Drawing A Sphere");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新日期:16-5-2020

222 次瀏覽

開啟您的 職業生涯

完成本課程獲取認證

開始學習
廣告