Javafx 简明教程

JavaFX - Stroke Line Join Property

JavaFX 不仅支持一次绘制单个 2D 形状,还允许您加入多个 2D 形状对象以形成另一个更大的对象。例如,您可以使用 Rectangle 类绘制矩形对象,还可以将四条线对象连接在一起以形成一个矩形。

JavaFX does not just support drawing a single 2D shape at a time, but also allows you to join multiple 2D shape objects to form another bigger object. For instance, you can use the Rectangle class to draw a rectangle object and also join four line objects together to form a rectangle.

在这种情况下,当使用多条线来形成 2D 形状时,JavaFX 支持各种属性应用于这些线,同时将它们组合在一起。其中一个属性是笔划线条连接属性。

In such cases where multiple lines are used to form a 2D shape, JavaFX supports various properties to be applied on these line while combining them together. One such property is Stroke Line Join Property.

Stroke Line Join Property

笔划线条连接属性用于指定用于将两个线对象组合在一起形成另一形状时使用的关节的形状。

The Stroke Line Join Property is used to designate the shape of the joint used to combine two line objects while forming another shape.

此属性的类型为 StrokeLineJoin ,它表示形状边缘使用的连接类型。

This property is of the type StrokeLineJoin, it represents the type of joining that is used at the edges of the shape.

笔划线条连接有三种类型。它由 StrokeLineJoin 的三个常量表示,如下所示 −

The stroke line join is of three types. It is represented by the three constants of StrokeLineJoin 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.

按照如下方法通过 setStrokeLineJoin() 方法可以设置笔划的线条连接方式:

You can set the line join of the stroke using the method setStrokeLineJoin() as follows −

path.setStrokeLineJoin(StrokeLineJoin.[Type_of_Join]);

Type_of_Join 可以使用下面任一关键词设置:

The Type_of_Join can be set using any of the following keywords:

  1. StrokeLineJoin.BEVEL − To set the stroke line join to bevel join.

  2. StrokeLineJoin.MITER − To set the stroke line join to miter join.

  3. StrokeLineJoin.ROUND − To set the stroke line join to round join.

默认情况下,图形的笔划线条连接是直角连接。下面是具有不同线条连接类型的三角形的图表:

By default, the Stroke Line Joining a shape is miter. Following is a diagram of a triangle with different line join types −

stroke line join

Example

让我们通过一个示例来展示三角形的笔划线条连接属性的使用方式。使用 StrokeLineJoinExample.java 名称保存此文件。

Let us see an example demonstrating the usage of Stroke Line Join property on a triangle. Save this file with the name StrokeLineJoinExample.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 StrokeLineJoinExample 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.setStrokeLineJoin(StrokeLineJoin.BEVEL);

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

Output

执行后,上述程序将生成一个 JavaFX 窗口,其中显示一个具有斜角笔划线条连接的三角形,如下所示:

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

stroke line join output

Example

让我们通过一个示例来展示圆角的笔划线条连接属性在矩形上的使用方式。使用 StrokeLineJoinRectangle.java 名称保存此文件。

Let us see an example demonstrating the usage of ROUND Stroke Line Join property on a Rectangle. Save this file with the name StrokeLineJoinRectangle.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 StrokeLineJoinRectangle extends Application {
   @Override
   public void start(Stage stage) {
      //Creating a Rectangle
      Polygon rectangle = new Polygon();

      //Adding coordinates to the polygon
      rectangle.getPoints().addAll(new Double[]{
         100.0, 50.0,
         170.0, 50.0,
         170.0, 250.0,
         100.0, 250.0,
      });
      rectangle.setFill(Color.ORANGE);
      rectangle.setStroke(Color.BLACK);
      rectangle.setStrokeWidth(5.0);
      rectangle.setStrokeLineJoin(StrokeLineJoin.ROUND);

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

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

Output

执行后,上述程序将生成一个 JavaFX 窗口,其中显示一个具有斜角笔划线条连接的三角形,如下所示:

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

strokelinejoin rectangle