4.3. Overlap

What happens if you both stroke and paint a shape? Interestingly, the results depend on whether you stroke or paint first. A shape's outline actually overlaps its interior, as shown in Figure 4.14. The shape's real outline, strictly speaking, is an infinitesimally thin line, shown black in the figure. The process of stroking creates an outline shape around the real outline. Some of the stroked outline extends outside the shape, and some of the stroked outline overlaps with the shape's interior.

Figure 4.14. The stroked outline and interior of a circle

If you're using opaque colors, then you'll get different results depending on the order in which you do things. If you use partially transparent colors, then you'll be able to observe the overlap of stroked outlines and filled interiors in your results. The following shows how this happens in practice. First, it strokes the outline and fills the interior of a circle using a partially transparent color:

Color smokey = new Color(128, 128, 128, 128);
    g2.setPaint(smokey);
    g2.fill(e);
    g2.draw(e);

Then, in a different location, it strokes the circle's outline using a solid black color and fills the interior using a solid gray:

g2.setPaint(Color.black);
    g2.draw(e);
    g2.setPaint(Color.gray);
    g2.fill(e);

Finally, it fills the circle with gray and then strokes the outline with black:

g2.setPaint(Color.gray); g2.fill(e); g2.setPaint(Color.black); ...

Get Java 2D Graphics 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.