Search code examples
javaimagebufferedimageclip

Clip a BufferedImage to an Area


I'm trying to draw an image within a certain area. Right now I have code that fills an area with a RadialGradientPaint.

Area lightArea = ...
// fill the polygon with the gradient paint
g.setPaint(light.paint);
g.fill(lightArea);

I would like to draw a BufferedImage in that area instead of drawing a RadialGradientPaint. Is there a way I can do that?


Solution

  • Use Graphics.setClip:

    g.setClip(lightArea);
    g.drawImage(yourImage, x, y, null);
    

    See http://docs.oracle.com/javase/tutorial/2d/advanced/clipping.html for more details.