JavaFX - 繪圖模式屬性



您可以使用 Shape3D 類在 JavaFX 應用程式中繪製 3D 形狀。此類允許您構建三種類型的 3D 形狀:長方體、圓柱體和球體作為其直接子類。

但是,根據您嘗試建立的 JavaFX 應用程式的性質,您還可以使用 Shape3D 類提供的屬性來增強 3D 形狀的外觀。有三個這樣的屬性可以應用於 JavaFX 應用程式的 3D 形狀。它們列舉如下:

  • 剔除面屬性

  • 繪圖模式屬性

  • 材質屬性

在本節中,我們將學習 JavaFX 中的繪圖模式屬性。

繪圖模式屬性

繪圖模式是屬於DrawMode列舉型別的屬性,它表示用於繪製當前 3D 形狀的繪圖模式型別。在 JavaFX 中,您可以選擇兩種繪圖模式來繪製 3D 形狀,它們是:

  • 填充 - 此模式繪製並填充 2D 形狀 (DrawMode.FILL)。

  • 線條 - 此模式使用線條繪製 3D 形狀 (DrawMode.LINE)。

預設情況下,三維形狀的繪圖模式為填充。但是您仍然可以使用 setDrawMode() 方法選擇繪圖模式來繪製 3D 形狀,如下所示:

box.setDrawMode(DrawMode.FILL); 

示例

以下程式是一個示例,它演示了 3D 長方體上的 LINE 繪圖模式。將此程式碼儲存在名為BoxDrawModeLine.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;  
import javafx.scene.shape.Box; 
import javafx.scene.shape.DrawMode; 
import javafx.stage.Stage; 
         
public class BoxDrawModeLine extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Box 
      Box box1 = new Box(); 
      
      //Setting the properties of the Box 
      box1.setWidth(100.0); 
      box1.setHeight(100.0);   
      box1.setDepth(100.0); 
      
      //Setting the position of the box 
      box1.setTranslateX(200); 
      box1.setTranslateY(150); 
      box1.setTranslateZ(0);
      
      //Setting the drawing mode of the box 
      box1.setDrawMode(DrawMode.LINE);     
         
      //Creating a Group object   
      Group root = new Group(box1); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 300, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Box"); 
         
      //Adding scene to the stage 
      stage.setScene(scene);
      
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 java 檔案。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls BoxDrawModeLine.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls BoxDrawModeLine

輸出

執行後,上述程式將生成一個 JavaFX 視窗,顯示一個繪圖模式為 LINE 的長方體,如下所示:

Drawing Modes

示例

讓我們再來看另一個示例,該示例顯示了在 3D 形狀上使用 FILL 繪圖模式的情況。為了正確顯示這些模式的差異,我們將再次將此模式應用於 3D 長方體。將此程式碼儲存在名為BoxDrawModeFill.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.PerspectiveCamera; 
import javafx.scene.Scene;  
import javafx.scene.shape.Box; 
import javafx.scene.shape.DrawMode; 
import javafx.stage.Stage; 
         
public class BoxDrawModeFill extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Box 
      Box box1 = new Box(); 
      
      //Setting the properties of the Box 
      box1.setWidth(100.0); 
      box1.setHeight(100.0);   
      box1.setDepth(100.0); 
      
      //Setting the position of the box 
      box1.setTranslateX(200); 
      box1.setTranslateY(150); 
      box1.setTranslateZ(0);
      
      //Setting the drawing mode of the box 
      box1.setDrawMode(DrawMode.FILL); 
      
      //Creating a Group object   
      Group root = new Group(box1); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300); 
       
      //Setting camera 
      PerspectiveCamera camera = new PerspectiveCamera(false); 
      camera.setTranslateX(100); 
      camera.setTranslateY(50); 
      camera.setTranslateZ(0); 
      scene.setCamera(camera);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Box"); 
         
      //Adding scene to the stage 
      stage.setScene(scene);
      
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 java 檔案。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls BoxDrawModeFill.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls BoxDrawModeFill

輸出

執行後,上述程式將生成一個 JavaFX 視窗,顯示一個繪圖模式為 FILL 的長方體,如下所示:

Drawing Modes
廣告