Chapter 4. Painting and Stroking
In the last chapter, we saw how shapes are defined in the 2D API. But what can you do with shapes?
Painting is the process of filling the interior of the shape with a color, color gradient, or texture.
Stroking is the process of drawing the shape's outline. You can draw an outline using different line widths, line styles, and colors.
Painting and stroking are very closely related. Stroking, in fact, is just the process of creating a shape that represents an outline and filling it. You can draw outlines using any type of paint. Figure 4.1 shows some examples. On the left, a circle has been filled with a color gradient. In the middle, the same circle's outline is drawn with a thick line using a solid color. On the right, the circle's outline is drawn with the same thick line using the color gradient.
Figure 4.1. Examples of painting and stroking

The following class produces the window shown in Figure 4.1. It uses some unfamiliar methods and classes: setPaint(), setStroke(), GradientPaint, and BasicStroke. I'll explain these classes and methods, and more, in the rest of the chapter. For now, this example should show you some of the potential of painting and stroking:
import java.awt.*; import java.awt.geom.*; public class PaintingAndStroking extends ApplicationFrame { public static void main(String[] args) { PaintingAndStroking f = new PaintingAndStroking(); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access