JavaFX - Stroke Line Join Property



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.

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.

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

The stroke line join is of three types. It is represented by the three constants of StrokeLineJoin as follows −

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

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

  • 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.

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

path.setStrokeLineJoin(StrokeLineJoin.[Type_of_Join]);

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

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

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

  • 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

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); 
   } 
}

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

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

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); 
   } 
}

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

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

Stroke Line Join Output
Advertisements