JavaFX – Stroke Miter Limit Property


JavaFX – Stroke Miter Limit Property


”;


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.

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 −

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

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

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.

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

path.setStrokeMiterLimit(4);

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

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

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

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

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

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

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

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *