JavaFX - 描边线连接属性



JavaFX 不仅支持一次绘制单个二维图形,还允许您将多个二维图形对象连接起来以形成另一个更大的对象。例如,您可以使用 Rectangle 类绘制矩形对象,也可以将四个线对象连接起来形成一个矩形。

在这种情况下,当使用多条线形成二维图形时,JavaFX 支持将各种属性应用于这些线,同时将它们组合在一起。其中一个属性是描边线连接属性。

描边线连接属性

描边线连接属性用于指定在形成另一个形状时用于组合两个线对象的连接形状。

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

描边线连接有三种类型。它由 StrokeLineJoin 的三个常量表示,如下所示:

  • **斜接 (Bevel)** - 在斜接连接中,交叉点的外部边缘用线段连接。

  • **斜角 (Miter)** - 在斜角连接中,交叉点的外部边缘连接在一起,形成一个锐利的边缘。

  • **圆角 (Round)** - 在圆角连接中,交叉点的外部边缘通过将角圆角连接起来,其半径将恰好是连接宽度的一半。

您可以使用以下方法设置描边的线连接:**setStrokeLineJoin()**

path.setStrokeLineJoin(StrokeLineJoin.[Type_of_Join]);

连接类型可以使用以下任何关键字设置:

  • **StrokeLineJoin.BEVEL** - 将描边线连接设置为斜接连接。

  • **StrokeLineJoin.MITER** - 将描边线连接设置为斜角连接。

  • **StrokeLineJoin.ROUND** - 将描边线连接设置为圆角连接。

默认情况下,形状的描边线连接是斜角。下图是一个具有不同线连接类型的三角形的示意图。

Stroke Line Join

示例

让我们看一个演示在三角形上使用描边线连接属性的示例。将此文件保存为 **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 文件。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeLineJoinExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeLineJoinExample

输出

执行后,上述程序将生成一个 JavaFX 窗口,显示如下所示具有斜接描边线连接的三角形。

Stroke Line Join Output

示例

让我们看一个演示在矩形上使用 ROUND 描边线连接属性的示例。将此文件保存为 **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 文件。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeLineJoinRectangle.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeLineJoinRectangle

输出

执行后,上述程序将生成一个 JavaFX 窗口,显示如下所示具有斜接描边线连接的三角形。

Stroke Line Join Output
广告
© . All rights reserved.