JavaFX - 描邊型別屬性



在幾何學中,二維 (2D) 形狀通常是指只有兩個度量維度的結構,通常是長度和寬度,並位於 XY 平面上。這些結構可以是開放或封閉圖形。

開放圖形包括三次曲線、四邊曲線等曲線,而封閉圖形包括所有型別的多邊形、圓形等。

在 JavaFX 中,可以透過匯入 **javafx.scene.shape** 包在應用程式上繪製這些 2D 形狀。但是,為了提高形狀的質量,可以對其執行各種屬性和操作。

在本章中,我們將詳細瞭解描邊型別屬性。

描邊型別屬性

二維形狀的描邊型別屬性用於指定二維形狀必須具有的邊界線的型別。在 JavaFX 中,此屬性使用 StrokeType 類設定。您可以使用以下方法設定描邊的型別:**setStrokeType()** −

Path.setStrokeType(StrokeType.stroke_type);

形狀的描邊型別可以是:

  • **內部 (Inside)** - 邊界線將繪製在形狀邊緣(輪廓)內部 (StrokeType.INSIDE)。

  • **外部 (Outside)** - 邊界線將繪製在形狀邊緣(輪廓)外部 (StrokeType.OUTSIDE)。

  • **居中 (Centered)** - 邊界線將以這樣的方式繪製:形狀的邊緣(輪廓)正好穿過線的中心 (StrokeType.CENTERED)。

預設情況下,形狀的描邊型別為居中。以下是具有不同描邊型別的三角形的圖:

Stroke Type

示例

讓我們來看一個演示在二維形狀上使用 StrokeType 屬性的示例。將此檔案儲存為 **StrokeTypeExample.java**。

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

public class StrokeTypeExample 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.setStrokeType(StrokeType.CENTERED);

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

輸出

執行後,上述程式將生成一個 JavaFX 視窗,其中顯示一個具有居中描邊型別的三角形,如下所示。

Stroke Type Output

示例

讓我們來看一個演示在二維形狀上使用 INSIDE StrokeType 屬性的示例。為了正確顯示 INSIDE 和 CENTERED 描邊型別之間的區別,我們在此處使用了更大的寬度。將此檔案儲存為 **StrokeTypeEllipse.java**。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.StrokeType;
import javafx.scene.paint.Color;
import javafx.stage.Stage;  

public class StrokeTypeEllipse extends Application { 
   @Override 
   public void start(Stage stage) {        
      //Drawing an ellipse 
      Ellipse ellipse = new Ellipse(); 

      //Setting the properties of the ellipse 
      ellipse.setCenterX(150.0f); 
      ellipse.setCenterY(100.0f); 
      ellipse.setRadiusX(100.0f); 
      ellipse.setRadiusY(50.0f); 

      ellipse.setFill(Color.ORANGE);
      ellipse.setStroke(Color.BLACK);
      ellipse.setStrokeWidth(20.0);
      ellipse.setStrokeType(StrokeType.INSIDE);

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

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

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

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

輸出

執行後,上述程式將生成一個 JavaFX 視窗,其中顯示一個具有居中描邊型別的三角形,如下所示。

Stroke Type Output
廣告