Line Styles with BasicStroke

In the last couple of examples, we’ve used the BasicStroke class to draw lines that are wider than the one-pixel lines supported by the Graphics class. Wide lines are more complicated than thin lines, however, so BasicStroke allows you to specify other line attributes as well: the cap style of a line specifies how the endpoints of lines look, and the join style specifies how the corners, or vertices, of shapes look. These style options for endpoints and vertices are shown in Figure 12-6. The figure also illustrates the use of a dot-dashed patterned line, which is another feature of BasicStroke. The figure was produced using the code listed in Example 12-8, which demonstrates how to use BasicStroke to draw wide lines with a variety of cap and join styles and to draw patterned lines. (The example does not illustrate the use of the “miter limit” line style, which comes into play when two lines with a join style of JOIN_MITER intersect at a small angle; in this case the miter can become very long, and must be limited to some maximum length. See BasicStroke documentation for details.)

Line styles with BasicStroke

Figure 12-6. Line styles with BasicStroke

Example 12-8. LineStyles.java

package je3.graphics; import java.awt.*; import java.awt.geom.*; /** A demonstration of Java2D line styles */ public class LineStyles implements GraphicsExample { public String getName( ) { return "LineStyles"; ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.