Javafx 简明教程
JavaFX - Stroke Fill Property
JavaFX 应用程序支持显示不同的内容,例如文本、2D 形状、3D 形状等;并且每个对象都有一个默认设置以创建一个基本应用程序。因此,在 JavaFX 应用程序上绘制 2D 形状时,每个形状也有一些默认设置,可以在单独设置时修改这些设置。例如,2D 形状中的默认填充颜色始终为黑色。
JavaFX application supports displaying diverse content like text, 2D shapes, 3D shapes, etc.; and every object has a default setting in order to create a basic application. Hence, while drawing 2D shapes on a JavaFX application, each shape also has some default settings that can be modified when they are set separately. For instance, the default fill colour in a 2D shape is always black.
引入了各种属性以改善这些形状的质量;并且在前面的章节中,我们已经学习了如何更改形状边界的尺寸和位置。在本节中,我们学习如何将特定 2D 形状的默认颜色黑色更改为其他颜色。
Various properties are introduced to improve the quality of these shapes; and we have already learned how to change the dimensions and placement of the shape boundaries, in previous chapters. In this chapter, we will learn how to change the default colour black of a specific 2D shape to other colours.
Stroke Fill Property
JavaFX 2D 形状中的描边填充属性用于指定要用来填充形状的颜色。此属性属于 javafx.scene.paint 包。你可以使用如下方法 setFill() 设置形状的填充色 −
Stroke Fill property in JavaFX 2D shapes is used to specify the colour which a shape is to be filled with. This property belongs to the javafx.scene.paint package. You can set the fill color of a shape using the method setFill() as follows −
path.setFill(COLOR.BLUE);
默认情况下,描边颜色的值为 BLACK 。以下是具有不同颜色的三角形的图表。
By default, the value of the stroke color is BLACK. Following is a diagram of a triangle with different colors.

Example
在本例中,我们将尝试创建两个 2D 圆,并用黄色填充其中一个圆圈,另一个将保持其默认颜色。目的在于观察两个形状之间的差异。使用名称 StrokeFillExample.java 保存此文件。
In this example, we will try to create two 2D circles, and fill one of the circle with Yellow colour and another will maintain its default colour. The aim is to observe the difference between both shapes. Save this file with the name 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 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeFillExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeFillExample
Example
在前面的示例中,我们创建了两个简单的 2D 形状,但是你也可以使用路径元素创建的复杂形状设置填充颜色。
We created two simple 2D shapes in the previous example, but you can also set a fill color to complex shapes created using path element.
在本例中,我们尝试使用 SVG 路径的线命令创建的复杂形状填充颜色。使用名称 StrokeFillSVGPath.java 保存此文件。
In this example, we are trying to fill a complex shape created using line commands of SVG Path. Save this file with the name 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 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeFillSVGPath.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeFillSVGPath