JavaFX - Stroke Miter Limit 屬性



JavaFX 允許使用者使用多條線建立 2D 物件,而不是為每個存在的 2D 形狀建立類。有一些形狀不屬於傳統 2D 形狀的範疇。在這種情況下,您可以透過將多條線組合在一起,並在這些線組合時應用 JavaFX 支援的多個屬性來形成非傳統的 2D 形狀。Stroke Line Join 屬性就是其中之一。

Stroke Line Join 屬性用於在將多個線物件組合成另一個 2D 形狀時設定連線的型別。此屬性有三種類型,如下所示:

  • 斜角 - 在斜角連線中,交點的外部邊緣用線段連線。

  • 斜接 - 在斜接連線中,交點的外部邊緣連線在一起形成一個銳角。

  • 圓角 - 在圓角連線中,交點的外部邊緣透過圓角連線,該圓角的半徑正好是連線寬度的二分之一。

預設情況下,形狀的 Stroke Line Join 為斜接。但是,此斜接連線還具有其他屬性以使連線更好。此屬性稱為 Stroke Miter Limit 屬性。

Stroke Miter Limit 屬性

此屬性的型別為 double。它表示連線的內點和連線的外點之間距離的限制。如果這兩個點之間的距離超過給定的限制,則斜接會在邊緣處被截斷。

您可以使用以下方法將值設定為此屬性setStroke() 如下所示:

path.setStrokeMiterLimit(4);

預設情況下,描邊斜接限制值為 10,描邊的顏色為黑色。以下是具有不同描邊限制的三角形的示意圖。

Stroke Limit

示例

讓我們看一個演示如何在三角形上使用 Stroke Line Join 屬性的示例。將此檔案儲存為StrokeMiterLimitExample.java

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Polygon;
import javafx.scene.shape.StrokeLineJoin;
import javafx.scene.paint.Color;
import javafx.stage.Stage;  

public class StrokeMiterLimitExample extends Application { 
   @Override 
   public void start(Stage stage) {        
      //Creating a Triangle 
      Polygon triangle = new Polygon();  

      //Adding coordinates to the polygon 
      triangle.getPoints().addAll(new Double[]{ 
         100.0, 50.0, 
         170.0, 150.0, 
         100.0, 250.0,  
      });
      triangle.setFill(Color.BLUE);
      triangle.setStroke(Color.BLACK);
      triangle.setStrokeWidth(7.0);
      triangle.setStrokeMiterLimit(4.0);

      //Creating a Group object  
      Group root = new Group(triangle); 

      //Creating a scene object 
      Scene scene = new Scene(root, 300, 300);  

      //Setting title to the Stage 
      stage.setTitle("Drawing a Triangle"); 

      //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 StrokeMiterLimitExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeMiterLimitExample

輸出

執行後,上述程式會生成一個 JavaFX 視窗,顯示一個斜接限制為 4 的三角形,如下所示。

Stroke Miter Limit Output

示例

讓我們看一個演示如何在多邊形上使用 Stroke Miter Limit 屬性的示例。在這裡,我們將嘗試傳遞一個比預設斜接限制相對較高的值。將此檔案儲存為StrokeMiterLimitPolygon.java

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Polygon;
import javafx.scene.shape.StrokeLineJoin;
import javafx.scene.paint.Color;
import javafx.stage.Stage;  

public class StrokeMiterLimitPolygon extends Application { 
   @Override 
   public void start(Stage stage) {        
      //Creating a Polygon 
      Polygon polygon = new Polygon();  

      //Adding coordinates to the polygon 
      polygon.getPoints().addAll(new Double[]{ 
         100.0, 50.0, 
         170.0, 50.0, 
         170.0, 150.0,
         100.0, 150.0,
         135.0, 200.0,		 
      });
      polygon.setFill(Color.ORANGE);
      polygon.setStroke(Color.BLACK);
      polygon.setStrokeWidth(5.0);
      polygon.setStrokeLineJoin(StrokeLineJoin.MITER);
      polygon.setStrokeMiterLimit(1000.0);

      //Creating a Group object  
      Group root = new Group(polygon); 

      //Creating a scene object 
      Scene scene = new Scene(root, 300, 300);  

      //Setting title to the Stage 
      stage.setTitle("Drawing a Polygon"); 

      //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 StrokeMiterLimitPolygon.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeMiterLimitPolygon

輸出

執行後,上述程式會生成一個 JavaFX 視窗,顯示一個斜接限制為 4 的三角形,如下所示。

Stroke Miter Limit Output
廣告