Home > Articles > Programming > Graphic Programming

  • Print
  • + Share This
This chapter is from the book

Clipping

The clipping path is a state attribute that specifies which part of a shape is to be rendered. In order to specify the clipping region, a path (see Listing 3.2 for the definition of a path) is created such that only the parts of the shape within the path are rendered. The clipping region can be specified by any valid Shape (for example, a GeneralPath or Rectangle2D). For example, a simple clipping path could be used in the myCustomRenderer class in our basic java graphics program recipe by adding the following:

GeneralPath gp = new GeneralPath();
gp.moveTo((50,50); //initial starting point
gp.lineTo(30,200);
gp.lineTo(110,140);
gp.lineTo(90,190);
gp.lineTo(300,50);
gp.closePath();

g2.setClip(gp);
g2.setColor(new GradientPaint();
g2.fill(new Rectangle2D.float(0.f,0.f,500.f,500.f);
  • + Share This
  • 🔖 Save To Your Account