Clearing translucent images

I have BufferedImages in my game that I create with the Transparency constant of Transparency.TRANSLUCENT and I want to clear them sometimes. For example, in my menu, I hold on to the image so that I don’t have to recreate it every frame because it rarely changes. However when it does change and I nullify it and turn it into a new BufferedImage, there’s an obvious hiccup and it’s really quite unnatractive in cases where I have to recreate the image frequently. I can’t do away with caching it because it gives me over 60fps where before I was lucky to get 30-40.

So my question is: is there a way I can clear my BufferedImage to be as if I had drawn nothing on it? Turning it into a new BufferedImage takes way too long, and it seems silly that I should have to recreate the Object at all. What is the best way I should go about clearing my BufferedImage? Thanks!

Since the image is translucent you can try to set the raster alpha channel to 0 for every pixel and have an image fully trasparent, then you can re-create it redrawing on the image.

But this method is size-capped.
The bigger is the image to clear, the slower will be the speed.

mhh. set the compositing mode to SRC, set color to rgba(0,0,0,0) and fill it with a rectangle.

that’s much faster, thanks!

[quote]that’s much faster, thanks!
[/quote]
Good to know.
Be sure to use a java.awt.AlphaComposite.Src ‘static’ instance, and not a new one each time, by the way. (and sorry if that looks like a stupid/obvious advice)

ya I got that much :slight_smile: However I do have 2 more questions.

What’s the default Composite set on my BufferedImage? I feel silly holding a temp Composite for the old one and resetting it when I feel like I should just know it and set it to another static Composite when I’m done with the Src one. Should I worry about this? Could it change depending on display types or anything?

Also, how can I find more info on the other static AlphaComposites? Could there be one that may be faster for me in this case?

I had the same issue with the composite. I had to store the current one, put mine in the graphics context, do my draws, then put the original composite back in. I couldn’t figure out what the ‘normal’ one was.

Dr. A>

i’ve never read that you had to take care of previous compositing. I’d bet it is the SrcOver instance.

if you don’t change the Composite back and you try drawing a translucent image over the Graphics2D context, well, it completely overwrites instead of blends :wink: You don’t want that once you’re done with the clearing process.

SrcOver did in fact set things back to normal, woo! :slight_smile:

SrcOvr is the normal one alright, but it is good to always save the old and reset it before your method returns.

That should be done with all graphics primitives (Paints, Font, Transform and so on). This make your methods usage “transparent”. If you do actually change anything and don’t reset it to the previous value is should at least be thourougly documented, maybe in bold! :slight_smile:

If you change a lot in a method there might be easier to do a


g = (Graphics2D) g.create();

...some paint code that changes g's props...

g.dispose();

which makes a copy of the graphics object to work on instead. The create/dispose is actually quite fast.

Cheers
Mikael

Here’s what I’m doing now:


g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0, 0, width, height);
g2d.setComposite(AlphaComposite.SrcOver);

AlphaComposite.Clear actually neglects any color in or out, so when I draw, the range is actually cleared. Instead of Clear, I was using Src and then setting the Color to a clear color, which worked on Java 1.5, but not on 1.4.2. Using AlphaComposite.Clear works on both, and doesn’t require you set a clear color. Interesting huh?