Home Segments Top Top Previous Next

1132: Mainline

You can supply a Graphics2D graphics context not only with an AffineTransform instance, which alters position, but also with a BasicStroke instance, which alters appearance. You use strokes to make lines thick or thin and plain or dashed. You also use strokes to determine how lines are capped and joined.

For example, to create a stroke, stroke, that draws lines 10 units wide, capped with butt ends, and joined to other lines with mitered corners, you write the following statement, using two class variables in the BasicStroke package to control caps and joins:

BasicStroke stroke  
 = new BasicStroke(10, BasicStroke.CAP_BUTT,  
                       BasicStroke.JOIN_MITER); 

Then, to attach the stroke to a Graphics2D graphics context, g, you use the setStroke method:

g.setStroke(stroke);