解釋 JavaFX 中 3D 物件的屬性?


以下是 3D 物件的各種屬性 -

  • 剔除面 - 通常,剔除是指移除形狀中方向不正確的部件(在視區中不可見)。

    可以使用 setCullFace() 方法(Shape 類)將值設定為 3d 物件的剔除面屬性。

    JavaFX 支援三種剔除面型別,用名為 CullFace 的列舉中的三個常量表示,即 NONE、FRONT、BACK。

  • 繪圖模式 - 此屬性定義/指定用於繪製 3D 形狀的模式。

    可以使用 setDrawMode() 方法(Shape 類)將值設定為 3d 物件的繪圖模式屬性。

    JavaFX 支援兩種繪圖模式,由名為 DrawMode 的列舉的常量表示,即 FILL 和 LINE

  • 材質 - 該屬性指定 3D 物件應該用什麼型別的材質覆蓋。可以使用 setMaterial() 方法為該屬性設定值。

示例

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.CullFace;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.DrawMode;
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 JavaFX3DShapesProperties extends Application {
   public void start(Stage stage) {
      //Drawing a Box
      Box cube = new Box(100, 120, 100);
      cube.setTranslateX(30.0);
      cube.setTranslateY(180.0);
      cube.setTranslateZ(150.0);
      //Setting the property "cull face"
      cube.setCullFace(CullFace.FRONT);
      //Drawing a Cylinder
      Cylinder cylinder = new Cylinder(50, 150);
      //Setting the properties of the cylinder
      cylinder.setTranslateX(250.0);
      cylinder.setTranslateY(180.0);
      cylinder.setTranslateZ(150.0);
      //Setting the cull face "material"
      PhongMaterial material = new PhongMaterial();
      material.setDiffuseColor(Color.DARKRED);
      cylinder.setMaterial(material);
      //Drawing a Sphere
      Sphere sphere = new Sphere(75);
      sphere.setTranslateX(480.0);
      sphere.setTranslateY(180.0);
      sphere.setTranslateZ(150.0);
      //Setting the property "draw mode"
      sphere.setDrawMode(DrawMode.LINE);
      //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("CullFace: front");
      label1.setFont(font);
      label1.setX(10);
      label1.setY(270);
      Text label2 = new Text("Material: colored");
      label2.setFont(font);
      label2.setX(180);
      label2.setY(270);
      Text label3 = new Text("Draw Mode: line");
      label3.setFont(font);
      label3.setX(370);
      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("JavaFX 3D Shape properties");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出


更新時間: 16-05-2020

107 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

現在開始
廣告