Download presentation
Presentation is loading. Please wait.
1
Advanced AWT Strokes
2
Strokes The draw operation of the Graphics2D class draws the boundary of a shape by using the currently selected stroke. By default, the stroke is a solid line that is 1 pixel wide. We can select a different stroke by calling the setStroke method. We must supply an object of a class that implements the Stroke interface. The Java 2D API defines only one such class, called BasicStroke.
3
End Cap Styles For example, To draw lines that are 10 pixels wide.
g2.setStroke(new BasicStroke(10.0F)); g2.draw(new Line2D.Double(. . .)); When a stroke is more than a pixel thick, then the end of the stroke can have different styles. A butt cap simply ends the stroke at its end point. A round cap adds a half-circle to the end of the stroke. A square cap adds a half-square to the end of the stroke.
4
Join Styles When two thick strokes meet, there are three choices for the join style A bevel join joins the strokes with a straight line that is perpendicular to the bisector of the angle between the two strokes. A round join extends each stroke to have a round cap. A miter join extends both strokes by adding a "spike."
5
The miter join is not suitable for lines that meet at small angles
The miter join is not suitable for lines that meet at small angles. If two lines join with an angle that is less than the miter limit, then a bevel join is used instead. That usage prevents extremely long spikes. By default, the miter limit is 10 degrees. new BasicStroke(10.0F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND) new BasicStroke(10.0F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 15.0F)
6
Dash Patterns We can specify dashed lines by setting a dash pattern.
The dash pattern is a float[] array of numbers that contains the lengths of the "on" and "off" strokes The dash phase indicates where in the dash pattern each line should start.
7
float[] dashPattern = { 10, 10, 10, 10, 10, 10, 30, 10, 30, ... };
g2.setStroke(new BasicStroke(10.0F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0F /* miter limit */, dashPattern, 0 /* dash phase */ ) );
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.