Javafx 简明教程

JavaFX - Stroke Miter Limit Property

JavaFX 允许用户使用多条线创建 2D 对象,而不是为存在的每个 2D 形状创建类。有几种形状不属于常规 2D 形状的类别。在这种情况下,可以通过对这些线应用 JavaFX 支持的几个属性,并在将它们组合在一起时将多条线连接起来,从而形成非传统的 2D 形状。其中一个属性是描边线连接属性。

JavaFX allows a user to create a 2D object using multiple lines, instead of having classes for each 2D shape that exists. There are several shapes that do not fall under the category of conventional 2D shapes. In such cases you can join multiple lines to form an unconventional 2D shape, by applying several properties supported by JavaFX on these line while combining them together. One such property is Stroke Line Join Property.

描边线连接属性用于在组合多个线对象以形成另一个 2D 形状时设置连接的类型。此属性有三种类型,如下所示 −

The Stroke Line Join Property is used to set the type of the joint while combining multiple line objects to form another 2D shape. This property is of three types as follows −

  1. Bevel − In bevel join, the outside edges of the intersection are connected with a line segment.

  2. Miter − In miter join, the outside edges of the intersection are joined together forming a sharp edge.

  3. Round − In round join, the outside edges of the intersection are joined by rounding off the corner, the radius of this will be exactly half the width of the join.

默认情况下,形状的描边线连接是平角。但是,该平角连接还有其他属性使连接更好。此属性称为描边斜接限制属性。

By default, the Stroke Line Joining a shape is miter. However, this miter join also has additional properties to make the join better. This property is known as Stroke Miter Limit Property.

Stroke Miter Limit Property

此属性的类型为 double。它表示连接的内点和外点之间的距离限制。如果这两个点之间的距离超过给定的限制,平角会在边缘处被切断。

This property is of the type double. It represents the limit for the distance between the inside point of the joint and the outside point of the joint. If the distance between these two points exceeds the given limit, the miter is cut at the edge.

可以使用 setStroke() 方法将值设置为此属性,如下所示 −

You can set value to this property using the method setStroke() as follows −

path.setStrokeMiterLimit(4);

默认情况下,描边斜接限制值为 10 的描边为黑色。以下是具有不同描边限制的三角形的图表。

By default, the stroke miter limit value id 10 of the stroke is black. Following is a diagram of a triangle with different stroke limits.

stroke limit

Example

让我们看一个示例,演示在三角形上使用描边线连接属性。将此文件保存为 StrokeMiterLimitExample.java

Let us see an example demonstrating the usage of Stroke Line Join property on a triangle. Save this file with the name StrokeMiterLimitExample.java.

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

public class StrokeMiterLimitExample 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.setStrokeMiterLimit(4.0);

      //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 文件。

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

Output

执行时,上述程序将生成一个 JavaFX 窗口,其中显示一个描边线连接为 4 的斜接限制的三角形,如下所示。

On executing, the above program generates a JavaFX window displaying a triangle with stroke line join of miter limit 4 as shown below.

stroke miter limit output

Example

以下是一个示例,演示在多边形上使用 Stroke Miter Limit 属性。在这里,我们将尝试传递一个高于默认斜接限制的值。使用名称 StrokeMiterLimitPolygon.java 保存此文件。

Let us see an example demonstrating the usage of Stroke Miter Limit property on a polygon. In here, we will try to pass a value that is relatively higher than the default miter limit. Save this file with the name StrokeMiterLimitPolygon.java.

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

public class StrokeMiterLimitPolygon extends Application {
   @Override
   public void start(Stage stage) {
      //Creating a Polygon
      Polygon polygon = new Polygon();

      //Adding coordinates to the polygon
      polygon.getPoints().addAll(new Double[]{
         100.0, 50.0,
         170.0, 50.0,
         170.0, 150.0,
         100.0, 150.0,
         135.0, 200.0,
      });
      polygon.setFill(Color.ORANGE);
      polygon.setStroke(Color.BLACK);
      polygon.setStrokeWidth(5.0);
      polygon.setStrokeLineJoin(StrokeLineJoin.MITER);
      polygon.setStrokeMiterLimit(1000.0);

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

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

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

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

Output

执行时,上述程序将生成一个 JavaFX 窗口,其中显示一个描边线连接为 4 的斜接限制的三角形,如下所示。

On executing, the above program generates a JavaFX window displaying a triangle with stroke line join of miter limit 4 as shown below.

strokemiterlimit polygon