java.awt.BasicStroke in java
Java's Abstract Window Toolkit (AWT) provides a set of classes for creating graphical user interfaces (GUIs) in Java programs. One of these classes is java.awt.BasicStroke
, which is used to define the width and style of lines drawn in a GUI.
A BasicStroke
object defines a stroke that can be used to draw lines or shapes. It specifies the width of the stroke, the end caps (the style of the endpoints of the line), the line join style (the style of the corner where two lines meet), and the dash pattern (if any) for dashed lines.
The width of the stroke is specified using a float value, which determines the thickness of the line drawn. The end caps can be one of three styles: CAP_BUTT
(the default), CAP_ROUND
, or CAP_SQUARE
. CAP_BUTT
leaves the ends of the line flat, while CAP_ROUND
rounds the ends of the line, and CAP_SQUARE
creates a squared-off end.
The line join style can be one of three styles as well: JOIN_BEVEL
(the default), JOIN_ROUND
, or JOIN_MITER
. JOIN_BEVEL
creates a straight line between the endpoints of the lines, while JOIN_ROUND
creates a rounded corner, and JOIN_MITER
creates a sharp corner.
Declaration of this class as follows.
public class java.awt.BasicStroke
implements java.awt.Stroke {
public static final int JOIN_MITER;
public static final int JOIN_ROUND;
public static final int JOIN_BEVEL;
public static final int CAP_BUTT;
public static final int CAP_ROUND;
public static final int CAP_SQUARE;
float width;
int join;
int cap;
float miterlimit;
float[] dash;
float dash_phase;
public java.awt.BasicStroke(float, int, int, float, float[], float);
public java.awt.BasicStroke(float, int, int, float);
public java.awt.BasicStroke(float, int, int);
public java.awt.BasicStroke(float);
public java.awt.BasicStroke();
public java.awt.Shape createStrokedShape(java.awt.Shape);
public float getLineWidth();
public int getEndCap();
public int getLineJoin();
public float getMiterLimit();
public float[] getDashArray();
public float getDashPhase();
public int hashCode();
public boolean equals(java.lang.Object);
}
Here are some of the methods of the BasicStroke class:
public float getLineWidth()
: This method returns the width of the stroke line.public float[] getDashArray()
: This method returns the dash array used for creating the stroke. The dash array is an array of floats that specifies the length of the "on" and "off" portions of the stroke.public float getDashPhase()
: This method returns the offset into the dash array at which the stroke begins.public int getEndCap()
: This method returns the type of the stroke line's end cap, which can be one of three types:BasicStroke.CAP_BUTT
,BasicStroke.CAP_ROUND
, orBasicStroke.CAP_SQUARE
.public int getLineJoin()
: This method returns the type of the stroke line's join, which can be one of three types:BasicStroke.JOIN_BEVEL
,BasicStroke.JOIN_MITER
, orBasicStroke.JOIN_ROUND
.public float getMiterLimit()
: This method returns the miter limit of the stroke line. The miter limit is the maximum allowed ratio of the miter length to the line width.public boolean equals(Object obj)
: This method tests if the specified object is equal to thisBasicStroke
instance.public int hashCode()
: This method returns the hash code for thisBasicStroke
instance.public String toString()
: This method returns a string representation of theBasicStroke
instance.
Code Example
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// create a basic stroke with a width of 5 and a join style of ROUND
BasicStroke stroke = new BasicStroke(5.0f,
cStroke.CAP_BUTT,
cStroke.JOIN_ROUND);
// set the stroke and color of the graphics context
g2d.setStroke(stroke);
g2d.setColor(Color.RED);
// draw a rectangle with the current stroke and color
g2d.drawRect(50, 50, 100, 100);
// set the line dash pattern of the stroke to 10 units on, 5 units off
float[] dash = {10.0f, 5.0f};
stroke = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 1.0f, dash, 0.0f);
// set the stroke and color of the graphics context to the new stroke and color
g2d.setStroke(stroke);
g2d.setColor(Color.BLUE);
// draw a line with the new stroke and color
g2d.drawLine(50, 200, 200, 200);
}
public static void main(String[] args) {
JFrame frame = new JFrame("BasicStroke Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.add(new Main());
frame.setVisible(true);
}
}
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment