Go2FullStack
Go2FullStack
How to use BasicStroke properly in Java 2D

Basic Concept

In JAVA 2D development, BasicStroke is used to define the characteristics of lines, and we can call the setStroke method in the Graphics2D class to set the newly created BasicStroke object. The construction method of BasicStroke is as follows:

public BasicStroke(float width, int cap, int join, float miterlimit,
                 float dash[], float dash_phase({
.....
}

For programmers who are not familiar with the technical terms of graphics, it is a little too difficult to understand the use of this method according to the API documentation, so I have sorted it out here for your understanding.

Constructor parameters

width

This parameter is the easiest to understand and represents the width of the line

cap

Only three values can be taken: CAP_BUTT, CAP_ROUND or CAP_SQUARE, representing different line ends. It means:

https://go2fullstack.com/wp-content/uploads/2020/05/image-3.png

join

This parameter shows the shape of the JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER when the two lines are connected. It means:

https://go2fullstack.com/wp-content/uploads/2020/05/image-4.png

miterlimit

When you use the JOIN_MITER strategy to represent the jointed shapes, the Angle of connection between the two wires may be small, resulting in a very long Angle! This parameter is used to limit the maximum length of that sharp Angle! When you use the JOIN_MITER policy, this parameter must be greater than 1, and the default value for this parameter is 10.0f.

dash[]

When drawing a dotted line, use this parameter. The dotted line is formed by: “line + notch + line + notch + line + notch…” . So this parameter is the length of these lines and the gap defined: {the length of the line, the length of the gap, the length of the line, the length of the gap… }

dash_phase

The last parameter is the one that matches the dash[] array, meaning that when you draw a dashed line, you start at an offset.

The following figure explains the meaning of dash[] and dash_phase:

https://go2fullstack.com/wp-content/uploads/2020/05/image-5.png

Original Published: https://blog.csdn.net/li_tengfei/article/details/6098093

Leave a Reply

textsms
account_circle
email

Go2FullStack

How to use BasicStroke properly in Java 2D
Basic Concept In JAVA 2D development, BasicStroke is used to define the characteristics of lines, and we can call the setStroke method in the Graphics2D class to set the n…
Scan QR code to continue reading
2010-12-25