Thursday, September 27, 2007

Clear a BufferedImage in Java

In Java creating a new BufferedImage for each frame of an animation is slow, but reusing images with transparency from frame to frame is tricky as BufferedImage doesn't have an obvious way to set all of it's pixels to completely transparent.

Unlike other colors we can't just paint a transparent color over the image because, well, it's transparent and won't affect the pixels already in the image.

Most likely because the composite is still set to be clearing shapes rather than filling in.Try setting the composite back to something normal - easiest way to do this is just to create a new Graphics2D from the image and paint on that