Javafx 简明教程
JavaFX - Stroke Line Cap Property
几何学中的线通常是存在于二维平面上的、宽度可忽略不计的一维图形。然而,JavaFX 也会像其他二维图形一样,提供方法来增强线的质量。这包括以不同的方式设置其边缘的结构。
A line in geometry is usually one dimensional figure with negligible width that exists in a two dimensional plane. However, like other 2D figures, JavaFX also provides ways to enhance the quality of a line. This includes setting the structure of its edges in a different ways.
线的末端也称为端点。默认情况下,这些端点是尖锐的。但是,使用 JavaFX 提供的各种属性,用户可以更改这些端点的结构。此属性称为描边线帽属性。
The ends of a line are also known as end caps. These end caps are sharp, by default. However, using various properties provided by JavaFX, a user can change the structure of these end caps. This property is known as Stroke Line Cap Property.
Stroke Line Cap Property
描边线帽指定/定义线的端点样式。此属性类型为 StrokeLineCap ,可以使用 javafx.scene.shape.Shape 类的方法 setStrokeLineCap() 设置,如下面的代码块所示:
The Stroke Line Cap specifies/defines the end cap style of the line. This property is of the type StrokeLineCap and can be set using the method setStrokeLineCap() of javafx.scene.shape.Shape class as shown in the following code block −
line.setStrokeLineCap(StrokeLineCap.SQUARE);
描边线帽可以是:
The stroke line cap can be −
-
Butt − The butt line cap is applied at the end of the lines (StrokeLineCap.BUTT).
-
Square − The square line cap is applied at the end of the lines (StrokeLineCap.SQUARE).
-
Round − The round line cap is applied at the end of the lines (StrokeLineCap.ROUND).
默认情况下,图形的描边线帽是方形。以下是带有不同线帽类型的三角形的图表。
By default, the Stroke Line cap a shape is square. Following is the diagram of a triangle with different line cap types.
Example
让我们来看一个展示在矩形上使用描边线帽属性的示例。我们不在没有边缘的图形上使用此属性。使用名称 StrokeLineCapExample.java 保存此文件。
Let us see an example demonstrating the usage of Stroke Line Cap property on a rectangle. We do not use this property on shapes with no edges. Save this file with the name StrokeLineCapExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class StrokeLineCapExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Triangle
Rectangle rect = new Rectangle(50.0, 50.0, 200.0, 70.0);
rect.setFill(Color.BLUE);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(7.0);
rect.setStrokeLineCap(StrokeLineCap.BUTT);
//Creating a Group object
Group root = new Group(rect);
//Creating a scene object
Scene scene = new Scene(root, 300, 300);
//Setting title to the Stage
stage.setTitle("Drawing a Rectangle");
//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 StrokeLineCapExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeLineCapExample
Output
执行时,以上程序将生成一个 JavaFX 窗口,该窗口显示一个描边线帽为平切类型的矩形,如下所示。
On executing, the above program generates a JavaFX window displaying a rectangle with butt type stroke line cap as shown below.
请注意,此属性仅应用于线形。如果在曲线形上使用它,结果可能没有任何差异。
Note that this property is only applied on line shapes. If it is used on curved shapes, the results may not show any difference.