JavaFX - 描邊寬度屬性



在前面的章節中,我們學習了各種 2D 形狀以及如何在 JavaFX 應用程式中繪製它們。但是,為了獲得更好的使用者體驗,有必要使您的應用程式儘可能地吸引人。這還包括增強 JavaFX 應用程式中 2D 形狀的外觀。

JavaFX 提供了一組用於此目的的屬性。它們用於提高應用程式中形狀的質量。在本章中,我們將詳細瞭解 Stroke Width 屬性。

描邊寬度屬性

Stroke Width 屬性用於設定 2D 形狀邊界線的粗細。此屬性的型別為 double,表示形狀邊界線的寬度。您可以使用 setStrokeWidth() 方法設定描邊寬度,如下所示:

Path.setStrokeWidth(3.0) 

預設情況下,形狀描邊的值為 1.0。以下是具有不同描邊寬度的三角形的示意圖。

Stroke Width

示例

在本示例中,我們將嘗試建立一個 2D 形狀(例如圓形),併為其描邊寬度設定一個值。將此檔案儲存為 StrokeWidthExample.java

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import javafx.scene.paint.Color;
import javafx.stage.Stage;  

public class StrokeWidthExample extends Application { 
   @Override 
   public void start(Stage stage) {        
      //Creating a Circle 
      Circle circle = new Circle(150.0f, 150.0f, 50.0f);  

      circle.setFill(Color.WHITE);
      circle.setStroke(Color.BLACK);
      circle.setStrokeWidth(6.0);

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

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

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

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

輸出

執行上述程式後,將生成一個 JavaFX 視窗,其中顯示一個具有如下所示描邊寬度的圓形。

Stroke Width Output

示例

現在,讓我們嘗試繪製一個具有相對較大寬度的三角形形狀,並觀察結果。將此檔案儲存為 StrokeWidthTriangle.java

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

public class StrokeWidthTriangle 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[]{ 
         150.0, 150.0, 
         220.0, 175.0, 
         150.0, 200.0,  
      });  

      triangle.setFill(Color.WHITE);
      triangle.setStroke(Color.BLACK);
      triangle.setStrokeWidth(100.0);
          
      //Creating a Group object  
      Group root = new Group(triangle); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 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 StrokeWidthTriangle.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeWidthTriangle

輸出

執行上述程式後,將生成一個 JavaFX 視窗,其中顯示一個具有如下所示相對較大描邊寬度的三角形。

Stroke Width Output
廣告