JavaFX - 建立圓柱體



圓柱體是一個封閉的立體,它有兩個平行的(通常是圓形的)底面,由一個曲面連線而成。為了便於理解,您可以將 3D 圓柱體想象成一堆 2D 圓形,這些圓形堆疊到一定高度;因此,即使它由兩個引數描述,它也成為一個三維形狀。

圓柱體的引數有 – 其圓形底面的**半徑**和圓柱體的**高度**,如下面的圖所示:

Cylinder

JavaFX 中的圓柱體

在 JavaFX 中,圓柱體由名為**Cylinder**的類表示。此類屬於包**javafx.scene.shape**。透過例項化此類,您可以在 JavaFX 中建立一個圓柱體節點。

此類具有 2 個雙精度資料型別的屬性,即:

  • **height** - 圓柱體的高度。

  • **radius** - 圓柱體的半徑。

要繪製圓柱體,您需要透過將其傳遞給此類的建構函式來為這些屬性傳遞值。這可以在例項化 Cylinder 類時按相同的順序完成;或者,使用它們各自的 setter 方法。

繪製 3D 圓柱體的步驟

要在 JavaFX 中繪製圓柱體(3D),請按照以下步驟操作。

步驟 1:建立類

透過例項化名為 Cylinder 的類(屬於包**javafx.scene.shape**)在 JavaFX 中建立 Cylinder 物件。您可以在 start() 方法中例項化此類,如下所示:

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {
      //Creating an object of the Cylinder class       
      Cylinder cylinder = new Cylinder();   
   }    
}

步驟 2:設定圓柱體的屬性

使用其各自的 setter 設定圓柱體的**高度**和**半徑**,如下所示。

//Setting the properties of the Cylinder 
cylinder.setHeight(300.0f); 
cylinder.setRadius(100.0f); 

步驟 3:建立 Group 物件

現在,透過例項化名為**Group**的類(屬於包**javafx.scene**)來建立一個 Group 物件。然後,將上一步驟中建立的 Cylinder(節點)物件作為引數傳遞給 Group 類的建構函式。這應該按順序完成,以便將其新增到組中,如下所示:

Group root = new Group(cylinder);

步驟 4:啟動應用程式

建立 3D 物件後,請按照以下步驟啟動 JavaFX 應用程式:

  • 透過將 Group 物件作為引數值傳遞給其建構函式來例項化名為**Scene**的類。您還可以將應用程式螢幕的尺寸作為可選引數傳遞給建構函式。

  • 使用**Stage**類的**setTitle()**方法設定舞臺的標題。

  • 使用名為**Stage**的類的**setScene()**方法將場景物件新增到舞臺。

  • 使用名為**show()**的方法顯示場景的內容。

  • 最後,應用程式在 Application 類中的**launch()**方法的幫助下啟動。

示例

以下程式顯示瞭如何使用 JavaFX 生成圓柱體。將此程式碼儲存在名為**CylinderExample.java**的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.CullFace; 
import javafx.scene.shape.Cylinder; 
import javafx.stage.Stage;

public class CylinderExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Cylinder 
      Cylinder cylinder = new Cylinder(); 
         
      //Setting the properties of the Cylinder 
      cylinder.setHeight(300.0f); 
      cylinder.setRadius(100.0f); 
               
      //Creating a Group object  
      Group root = new Group(cylinder); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a cylinder"); 
         
      //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 CylinderExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls CylinderExample

輸出

執行上述程式後,會生成一個 JavaFX 視窗,其中顯示如下所示的圓柱體。

Drawing 3dCylinder

示例

您還可以對 3D 形狀應用變換。在此示例中,我們嘗試對 3D 圓柱體應用平移變換,並將其重新定位到應用程式上。將此程式碼儲存在名為**TranslateCylinderExample.java**的檔案中。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Cylinder;
import javafx.scene.paint.Color;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class TranslateCylinderExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Cylinder 
      Cylinder cylinder = new Cylinder(); 
         
      //Setting the properties of the Cylinder 
      cylinder.setHeight(150.0f); 
      cylinder.setRadius(100.0f);

      Translate translate = new Translate();       
      translate.setX(200); 
      translate.setY(150); 
      translate.setZ(25); 
      
      cylinder.getTransforms().addAll(translate);	  
               
      //Creating a Group object  
      Group root = new Group(cylinder); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 400, 300); 
	  
      scene.setFill(Color.web("#81c483"));
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a cylinder"); 
         
      //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 TranslateCylinderExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateCylinderExample

輸出

執行上述程式後,會生成一個 JavaFX 視窗,其中顯示如下所示的圓柱體。

Drawing 3dCylinder
廣告

© . All rights reserved.