JavaFX - 描邊填充屬性



JavaFX 應用程式支援顯示各種內容,例如文字、二維形狀、三維形狀等;每個物件都有預設設定,以便建立基本的應用程式。因此,在 JavaFX 應用程式上繪製二維形狀時,每個形狀也有一些預設設定,可以在單獨設定時修改它們。例如,二維形狀的預設填充顏色始終為黑色。

引入了各種屬性來提高這些形狀的質量;在之前的章節中,我們已經學習瞭如何更改形狀邊界的尺寸和位置。在本章中,我們將學習如何將特定二維形狀的預設黑色更改為其他顏色。

描邊填充屬性

JavaFX 二維形狀中的描邊填充屬性用於指定形狀要填充的顏色。此屬性屬於 javafx.scene.paint 包。您可以使用 setFill() 方法設定形狀的填充顏色,如下所示:

path.setFill(COLOR.BLUE);

預設情況下,描邊顏色的值為黑色。下圖是一個具有不同顏色的三角形。

Stroke Fill

示例

在這個示例中,我們將嘗試建立兩個二維圓形,並用黃色填充其中一個圓形,另一個圓形保持其預設顏色。目的是觀察兩種形狀之間的區別。將此檔案儲存為StrokeFillExample.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 StrokeFillExample extends Application { 
   @Override 
   public void start(Stage stage) {        
      //Creating a Circle 
      Circle circle1 = new Circle(200.0f, 150.0f, 50.0f); 
      Circle circle2 = new Circle(100.0f, 150.0f, 50.0f);

      circle1.setFill(Color.YELLOW);

      //Creating a Group object  
      Group root = new Group();
      root.getChildren().addAll(circle1, circle2);

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

      //Setting title to the Stage 
      stage.setTitle("Colouring 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 StrokeFillExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeFillExample

輸出

執行上述程式後,會生成一個 JavaFX 視窗,顯示兩個圓形,左側圓形具有其預設填充,而另一個圓形為黃色,如下所示。

Stroke Fill Output

示例

我們在前面的示例中建立了兩個簡單的二維形狀,但您也可以將填充顏色設定為使用路徑元素建立的複雜形狀。

在這個示例中,我們嘗試填充使用 SVG 路徑的線條命令建立的複雜形狀。將此檔案儲存為StrokeFillSVGPath.java

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.SVGPath; 
import javafx.stage.Stage; 
        
public class StrokeFillSVGPath extends Application {  
   @Override 
   public void start(Stage stage) { 
      //Creating a SVGPath object 
      SVGPath svgPath = new SVGPath();       
       
      String path = "M 100 100 H 190 V 190 H 150 L 200 200";
      
      //Setting the SVGPath in the form of string 
      svgPath.setContent(path);

      // Setting the stroke and fill of the path
      svgPath.setStroke(Color.BLACK);
      svgPath.setFill(Color.BLUE);	  
         
      //Creating a Group object  
      Group root = new Group(svgPath); 
               
      //Creating a scene object 
      Scene scene = new Scene(root, 300, 300);
      
      //Setting title to the Stage
      stage.setTitle("Drawing a Line"); 
         
      //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 StrokeFillSVGPath.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeFillSVGPath

輸出

執行上述程式後,會生成一個 JavaFX 視窗,顯示兩個圓形,左側圓形具有其預設填充,而另一個圓形為黃色,如下所示。

Stroke Fill SVG Path
廣告