Keilly: Clear a BufferedImage in Java

Thursday, September 27, 2007

Clear a BufferedImage in Java

Lots of cool Animations require a transparent background and, of course, speed.

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.

I had a poke around and found this from IBM DeveloperWorks.

AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
Rectangle2D.Double rect =
new Rectangle2D.Double(0,0,width,height);

It would be a lot more useful if clearing to any color was part of the Image API, but until then this is good to know.


Anonymous said...

this is exactly what i needed to solve my problem. thank you.

knuspermagier said...

great, exactly what i was searching for. i love you!

Anonymous said...

11/10 for a great tip ! That's saved me a huge amount of time and angst. Thankyou

Randolf Richardson said...

Thank you. You have saved me a lot of time by sharing that information.

Unknown said...

thank you, very very usefully

Unknown said...

Wow. My search has finally ended! :D

Anonymous said...

Very good, but you can also
clear an image with following code:
yourBufferedImage.createGraphics ().clearRect (0, 0, width, height);

Neil Cochrane said...

To the previous poster: The javadoc for Graphics.clearRect(...) says it fills a rectangle with the current background color. This sounds quite different to really clearing an image.

Anonymous said...

How would you do this to only a section of your image and not the entire image?

Neil Cochrane said...

Just set the rect to the area you want to clear.

Anonymous said...

I am unable to paint new shapes after i have cleared part or all of my BufferedImage.

Neil Cochrane said...

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

Anonymous said...

Thanks. Really appreciate your article. I was stuck on this for many days.

Also, in my case I had to redraw the image and some new shapes. For this I did the following

Graphics2D newgraphics = panel1.getGraphics();

Composite backup=newgraphics.getComposite();

//set the composite to clear as per the Keilly's code
//Fill the cleared area with rectangle and then


//and then re-load the image from a file and draw it ->

Thanks, Keilly once again for sharing the article

Unknown said...

Great post. Helped a lot. Thanks.

Anonymous said...

Dude, thanks a bunch.

Stoodles said...

Thanks. Brilliant tip.

Unknown said...

hi, what type is the bufferedImage? When i use "TYPE_INT_ARGB", it doesn't work, but when i use "TYPE_INT_BGR", it works. Why?

Anonymous said...

Thank you so much. I spent a lot of days to solve my trouble with repainting on transparent background. Thank you very, very, very much!!!!