JavaFX - 繪製弧線



在簡單的幾何學中,弧線定義為橢圓或圓周的一部分。或者,簡單地說,它是一條連線兩個端點的曲線。弧線在圓心處形成小於或等於 360 度的角。它由以下屬性描述:

  • 長度 - 沿弧線的距離。

  • 角度 - 曲線在圓心處形成的角度。

  • radiusX - 當前弧線所屬的完整橢圓的寬度。

  • radiusY - 當前弧線所屬的完整橢圓的高度。

如果 radiusX 和 radiusY 相同,則該弧線是圓周的一部分。

ARC

JavaFX 中的弧線

在 JavaFX 中,弧線由名為 Arc 的類表示。此類屬於包 javafx.scene.shape

透過例項化此類,您可以在 JavaFX 中建立弧線節點。

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

  • centerX - 弧線中心的 x 座標。

  • centerY - 弧線中心的 y 座標。

  • radiusX - 當前弧線所屬的完整橢圓的寬度。

  • radiusY - 當前弧線所屬的完整橢圓的高度。

  • startAngle - 弧線的起始角度(以度為單位)。

  • length - 弧線的角度範圍(以度為單位)。

要繪製弧線,您需要向這些屬性傳遞值,可以透過在例項化時按相同的順序將它們傳遞給此類的建構函式,或者使用它們各自的 setter 方法來實現。

弧線的型別

在 JavaFX 中,您可以繪製三種類型的弧線:

  • 開啟 - 完全未閉合的弧線稱為開放弧線。

  • - 弦是一種用直線閉合的弧線。

  • 圓形 - 圓形弧線是透過連線起始點和結束點到橢圓中心來閉合的弧線。

Open Closed Round

您可以使用 setType() 方法透過傳遞以下屬性之一來設定弧線的型別:ArcType.OPEN, ArcType.CHORD, ArcType.ROUND

繪製弧線的步驟

要在 JavaFX 中繪製弧線,請按照以下步驟操作。

步驟 1:建立弧線

您可以透過例項化名為 Arc 的類(屬於包 javafx.scene.shape)來在 JavaFX 中建立弧線。您可以像下面這樣在 Application 類的 start() 方法內例項化此類。

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

步驟 2:設定弧線的屬性

指定橢圓(此弧線所屬的橢圓)中心的 x、y 座標。這些座標包括 – radiusX、radiusY、起始角度和弧線的長度,使用它們各自的 setter 方法,如下面的程式碼塊所示。

//Setting the properties of the arc 
arc.setCenterX(300.0f); 
arc.setCenterY(150.0f); 
arc.setRadiusX(90.0f); 
arc.setRadiusY(90.0f); 
arc.setStartAngle(40.0f); 
arc.setLength(239.0f);

步驟 3:設定弧線的型別

您還可以使用 setType() 方法設定弧線的型別(圓形、弦、開啟),如下面的程式碼塊所示。

//Setting the type of the arc 
arc.setType(ArcType.ROUND);

步驟 4:將弧線物件新增到 Group

start() 方法中,透過將上一步中建立的弧線物件作為引數值傳遞給其建構函式來例項化 Group 類:

Group root = new Group(arc);

步驟 5:啟動應用程式

建立二維物件後,請按照以下步驟正確啟動應用程式:

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

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

  • 現在,使用名為 Stage 的類的 setScene() 方法將 Scene 物件新增到舞臺。

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

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

示例

以下程式生成一個弧線。將此程式碼儲存在名為 ArcExample.java 的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.stage.Stage; 
import javafx.scene.shape.Arc;  
import javafx.scene.shape.ArcType; 
        
public class ArcExample extends Application {  
   public void start(Stage stage) { 
      //Drawing an arc 
      Arc arc = new Arc(); 
         
      //Setting the properties of the arc 
      arc.setCenterX(300.0f); 
      arc.setCenterY(150.0f); 
      arc.setRadiusX(90.0f); 
      arc.setRadiusY(90.0f); 
      arc.setStartAngle(40.0f); 
      arc.setLength(239.0f);  
      
      //Setting the type of the arc 
      arc.setType(ArcType.ROUND);         
         
      //Creating a Group object  
      Group root = new Group(arc); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing an Arc"); 
         
      //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 ArcExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcExample

輸出

執行上述程式後,將生成一個 JavaFX 視窗,其中顯示如下螢幕截圖所示的弧線。

Drawing Arc

示例

讓我們嘗試在下面的示例中繪製另一個“開啟”型別的弧線。將此程式碼儲存在名為 ArcOpen.java 的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.stage.Stage; 
import javafx.scene.shape.Arc;  
import javafx.scene.shape.ArcType; 
        
public class ArcOpen extends Application {  
   public void start(Stage stage) { 
      //Drawing an arc 
      Arc arc = new Arc(); 
         
      //Setting the properties of the arc 
      arc.setCenterX(300.0f); 
      arc.setCenterY(150.0f); 
      arc.setRadiusX(90.0f); 
      arc.setRadiusY(90.0f); 
      arc.setStartAngle(40.0f); 
      arc.setLength(239.0f);  
      
      //Setting the type of the arc 
      arc.setType(ArcType.OPEN);         
         
      //Creating a Group object  
      Group root = new Group(arc); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing an Open Arc"); 
         
      //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 ArcOpen.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcOpen

輸出

執行上述程式後,將生成一個 JavaFX 視窗,其中顯示如下螢幕截圖所示的弧線。

Drawing Open Arc

示例

現在,我們嘗試在下面的示例中繪製另一個“弦”型別的弧線。將此程式碼儲存在名為 ArcChord.java 的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.stage.Stage; 
import javafx.scene.shape.Arc;  
import javafx.scene.shape.ArcType; 
        
public class ArcChord extends Application {  
   public void start(Stage stage) { 
      //Drawing an arc 
      Arc arc = new Arc(); 
         
      //Setting the properties of the arc 
      arc.setCenterX(300.0f); 
      arc.setCenterY(150.0f); 
      arc.setRadiusX(90.0f); 
      arc.setRadiusY(90.0f); 
      arc.setStartAngle(40.0f); 
      arc.setLength(239.0f);  
      
      //Setting the type of the arc 
      arc.setType(ArcType.CHORD);         
         
      //Creating a Group object  
      Group root = new Group(arc); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Chord Arc"); 
         
      //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 ArcChord.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcChord

輸出

執行上述程式後,將生成一個 JavaFX 視窗,其中顯示如下螢幕截圖所示的弧線。

Drawing Chord Arc
廣告