JavaFX - 建立球體



球體是在三維空間中一個完美的幾何圓形物體,是完全圓形球的表面。

球體定義為三維空間中所有與給定點距離均為 r 的點的集合。此距離r是球體的半徑,給定點是球體的中心。

3d Sphere

JavaFX 中的球體

在 JavaFX 中,球體由名為Sphere的類表示。此類屬於包javafx.scene.shape。透過例項化此類,可以在 JavaFX 中建立球體節點。

此類具有名為radius的雙精度資料型別屬性。它表示球體的半徑。要繪製球體,需要透過在例項化時將其傳遞給此類的建構函式來為此屬性設定值;或者,使用名為setRadius()的 setter 方法。

繪製 3D 球體的步驟

按照以下步驟在 JavaFX 中繪製球體 (3D)。

步驟 1:建立球體

透過例項化名為Sphere的類(屬於包javafx.scene.shape)在 JavaFX 中建立球體。可以在 start() 方法中例項化此類,如下所示。

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

步驟 2:設定球體的屬性

使用名為setRadius()的方法設定球體的半徑,如下所示。

//Setting the radius of the Sphere 
sphere.setRadius(300.0);

步驟 3:建立 Group 物件

透過將球體物件作為引數傳遞給其建構函式來例項化 Group 類,如下所示:

Group root = new Group(sphere);

步驟 4:啟動應用程式

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

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

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

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

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

  • 最後,應用程式在 Application 類中使用launch()方法啟動。

示例

以下程式演示如何使用 JavaFX 生成球體。將此程式碼儲存在名為SphereExample.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.shape.Sphere; 
         
public class SphereExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Sphere  
      Sphere sphere = new Sphere();  
      
      //Setting the properties of the Sphere 
      sphere.setRadius(50.0);   
       
      sphere.setTranslateX(200); 
      sphere.setTranslateY(150);      
       
      //Creating a Group object  
      Group root = new Group(sphere); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Sphere - draw fill");
      
      //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 SphereExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls SphereExample 

輸出

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

Drawing 3dSphere

示例

在下面的程式中,我們透過為 JavaFX 應用程式的場景著色來應用一些 JavaFX CSS。將此程式碼儲存在名為CSSSphereExample.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere; 
         
public class CSSSphereExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Sphere  
      Sphere sphere = new Sphere();  
      
      //Setting the properties of the Sphere 
      sphere.setRadius(50.0);   
       
      sphere.setTranslateX(100); 
      sphere.setTranslateY(150);      
       
      //Creating a Group object  
      Group root = new Group(sphere); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 300, 300);
      
	  scene.setFill(Color.ORANGE);	  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Sphere");
      
      //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 CSSSphereExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls CSSSphereExample 

輸出

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

Drawing 3dSphere
廣告