10.6. Stroke Styles

In the AWT, the drawXxx methods of Graphics resulted in solid, 1-pixel-wide lines. Furthermore, drawing commands that consisted of multiple-line segments (e.g., drawRect and drawPolygon) had a predefined way of joining the line segments and terminating segments that did not join to others. Java 2D gives you much more flexibility. In addition to setting the pen color or pattern (through setPaint, as discussed in the previous section), with Java 2D you can set the pen thickness and dashing pattern and specify the way in which line segments end and are joined together. To control how lines are drawn, first create a BasicStroke object, then use the setStroke method to tell the Graphics2D object to use the BasicStroke object.

Get Core Web Programming, Second 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.