By setting a clipping shape in the graphics context, you constrain all drawing operations to the interior of that clipping shape.
g2.setClip(clipShape); // but see below g2.draw(shape); // draws only the part that falls inside the clipping shape
However, in practice, you don’t want to call the
setClip operation because it replaces any existing clipping shape that the graphics context might have. For example, as you will see later in this chapter, a graphics context for printing comes with a clip rectangle that ensures that you don’t draw on the margins. Instead, call the
g2.clip(clipShape); // better
clip method intersects the existing clipping shape with the new one that you supply. ...