如何用 JavaFX 建立 path 元素弧線?


此類表示路徑元素弧線。有助你從當前座標繪製一條弧線到指定(新)座標。

建立 line 路徑元素 -

  • 例項化ArcTo 類。

  • 使用 setter 方法將值設定為此類的屬性,或繞過它們到建構函式。

  • 例項化 Path 類。

  • 使用getElements() 方法獲取上面建立的 Path 的可觀察列表物件。

  • 使用add() 方法將上面建立的 ArcTo 物件新增到可觀察列表中。

  • 最後,將路徑新增到 Group 物件。

示例

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.VLineTo;
public class ArcToExample extends Application {
   public void start(Stage stage) {
      //Creating PathElement objects
      MoveTo moveTo = new MoveTo(490, 50);
      LineTo line1 = new LineTo(250, 250);
      //Instantiating the arcTo class
      ArcTo arcTo = new ArcTo();
      arcTo.setX(300.0);
      arcTo.setY(50.0);
      arcTo.setRadiusX(50.0);
      arcTo.setRadiusY(50.0);
      //Creating the HLineTo object
      VLineTo vLine = new VLineTo();
      vLine.setY(180);
      //Creating a Path
      Path path = new Path();
      path.getElements().addAll(moveTo, line1, arcTo, vLine);
      //Setting other properties
      path.setStrokeWidth(8.0);
      path.setStroke(Color.DARKSLATEGREY);  
      //Preparing the Stage object
      Group root = new Group(path);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("JavaFX Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於:13-04-2020

184 瀏覽量

啟動你的職業

完成課程認證

開始
廣告