JavaFX - ArcTo 路徑物件



Path 元素Arc 用於從當前位置繪製到指定座標點處的弧線。

它由名為ArcTo的類表示。此類屬於javafx.scene.shape包。

此類具有 4 個 double 資料型別的屬性,即:

  • X - 弧形中心的 x 座標。

  • Y - 弧形中心的 y 座標。

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

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

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

繪製 PathElement Arc 的步驟

要在 JavaFX 中從當前位置繪製到指定點的弧線,請按照以下步驟操作。

步驟 1:建立 Path 物件

在 Application 類的 start() 方法內部例項化 Path 類來建立一個 Path 物件,如下所示。

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {
      //Creating a Path object 
      Path path = new Path();    
   }    
}    

步驟 2:建立 Path

建立MoveTo路徑元素並將 XY 座標設定為線的起點座標 (100, 150)。這可以使用MoveTo類的setX()setY()方法完成,如下所示。

//Moving to the starting point 
MoveTo moveTo = new MoveTo(); 
moveTo.setX(100.0f); 
moveTo.setY(150.0f);

步驟 3:建立 ArcTo 類的物件

透過例項化名為 ArcTo 的類來建立路徑元素二次曲線,該類屬於javafx.scene.shape包,如下所示:

//Creating an object of the class ArcTo  
ArcTo arcTo = new ArcTo()

步驟 4:設定弧線元素的屬性

指定橢圓(此弧線的一部分)的 x、y 座標。然後,可以使用它們各自的 setter 方法指定弧線的 radiusX、radiusY、起始角度和長度,如下所示。

//setting properties of the path element arc  
arcTo.setX(300.0); 
arcTo.setY(50.0); 
       
arcTo.setRadiusX(50.0); 
arcTo.setRadiusY(50.0);   

步驟 5:將元素新增到 Path 類的可觀察列表

將前面步驟中建立的路徑元素MoveToarcTo新增到Path類的可觀察列表中,如下所示:

//Adding the path elements to Observable list of the Path class   
path.getElements().add(moveTo); 
path.getElements().add(cubicCurveTo);

步驟 6:啟動應用程式

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

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

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

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

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

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

示例

以下程式使用 JavaFX 的 Path 類從當前點繪製到指定位置的弧線。將此程式碼儲存在名為ArcExample.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.MoveTo; 
import javafx.scene.shape.Path; 
         
public class ArcExample extends Application { 
   @Override 
   public void start(Stage stage) {         
      //Creating an object of the class Path  
      Path path = new Path();  
      
      //Moving to the starting point 
      MoveTo moveTo = new MoveTo(); 
      moveTo.setX(250.0); 
      moveTo.setY(250.0); 
      
      //Instantiating the arcTo class 
      ArcTo arcTo = new ArcTo(); 
         
      //setting properties of the path element arc  
      arcTo.setX(300.0); 
      arcTo.setY(50.0); 
       
      arcTo.setRadiusX(50.0); 
      arcTo.setRadiusY(50.0); 
         
      //Adding the path elements to Observable list of the Path class 
      path.getElements().add(moveTo); 
      path.getElements().add(arcTo);        
         
      //Creating a Group object  
      Group root = new Group(path); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage
      stage.setTitle("Drawing an arc through a path"); 
         
      //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 Path

示例

讓我們嘗試在 JavaFX 中繪製 ArcTo Path 以建立具有在 Arc Path 上來回移動的圓形擺錘的擺錘物件。將此程式碼儲存在名為ArcToAnimation.java的檔案中。

import javafx.application.Application;
import javafx.animation.PathTransition; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.shape.*;
import javafx.scene.shape.MoveTo; 
import javafx.scene.shape.Path;
import javafx.util.Duration; 
         
public class ArcToAnimation extends Application { 
   @Override 
   public void start(Stage stage) {
      // Drawing a circle
      Circle circle = new Circle(300.0f, 50.0f, 40.0f);  
	  
      //Creating an object of the class Path  
      Path path = new Path();  
      
      //Moving to the starting point 
      MoveTo moveTo = new MoveTo(); 
      moveTo.setX(250.0); 
      moveTo.setY(250.0); 
      
      //Instantiating the arcTo class 
      ArcTo arcTo = new ArcTo(); 
         
      //setting properties of the path element arc  
      arcTo.setX(300.0); 
      arcTo.setY(50.0); 
       
      arcTo.setRadiusX(50.0); 
      arcTo.setRadiusY(50.0); 
         
      //Adding the path elements to Observable list of the Path class 
      path.getElements().add(moveTo); 
      path.getElements().add(arcTo);

      //Creating a path transition 
      PathTransition pathTransition = new PathTransition(); 
      
      //Setting the duration of the path transition 
      pathTransition.setDuration(Duration.millis(1000));

      //Setting the node 
      pathTransition.setNode(circle);	  
      
      //Setting the path 
      pathTransition.setPath(path);  
      
      //Setting the orientation of the path 
      pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT); 
      
      //Setting the cycle count for the transition 
      pathTransition.setCycleCount(50); 
      
      //Setting auto reverse value to true 
      pathTransition.setAutoReverse(true); 
    
      //Playing the animation 
      pathTransition.play(); 	  
         
      //Creating a Group object  
      Group root = new Group();
      root.getChildren().addAll(circle, path); 	  
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage
      stage.setTitle("Drawing an arc through a path"); 
         
      //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 Pendulum
廣告
© . All rights reserved.