Javafx 简明教程
JavaFX - Stroke Width Property
在前面的章节中,我们了解了各种 2D 形状以及如何在 JavaFX 应用程序中绘制它们。但是,为了提高用户体验,有必要使应用程序尽可能有吸引力。这还包括增强 JavaFX 应用程序中 2D 形状的外观和质感。
In the previous chapters, we have learned about various 2D shapes and how they are drawn in a JavaFX application. However, it is necessary to make your application as attractive as possible for better user experience. This also includes enhancing the look and feel of your 2D shapes within your JavaFX application.
JavaFX 提供了一组属性用于此目的。它们用于提高应用程序中形状的质量。在本章中,我们将详细了解 Stroke Width 属性。
JavaFX provides a set of properties for this purpose. They are used to improve the quality of your shapes on the application. In this chapter, we will learn about Stroke Width property in detail.
Stroke Width Property
Stroke Width 属性用于设置 2D 形状边界线的粗细。此属性为 double 类型,它表示形状边界线的宽度。可以使用 setStrokeWidth() 方法按如下方式设置笔触宽度 −
The Stroke Width Property is used to set thickness of the boundary line of a 2D shape. This property is of the type double and it represents the width of the boundary line of the shape. You can set the stroke width using the method setStrokeWidth() as follows −
Path.setStrokeWidth(3.0)
默认情况下,形状笔画的值为 1.0 。以下是具有不同笔触宽度的三角形图。
By default, the value of the stroke with of a shape is 1.0. Following is a diagram of a triangle with different values of stroke width.

Example
在此示例中,我们将尝试创建一个 2D 形状(例如圆),并为其笔触宽度设置一个值。将此文件保存为 StrokeWidthExample.java 。
In this example, we will try to create a 2D shape, say a circle, and set a value to its stroke width. Save this file with the name 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 文件。
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 StrokeWidthExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeWidthExample
Example
现在,让我们尝试绘制一个宽度相对较大的三角形形状,并观察结果。将此文件保存为 StrokeWidthTriangle.java 。
Now, let us try to draw a Triangle shape with relatively larger width and observe the results. Save this file with the name 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 文件。
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 StrokeWidthTriangle.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeWidthTriangle