Erasing transparent images

Hi

I’ve got a transparent image:

Image img=App.gc.createCompatibleImage(Game.width,Game.height,Transparency.BITMASK);

Now I past some graphics (with 1 bit transparence) at init time, and then past the image every graphic cicle to the screen (this to speed up things).

But what I’ve to do if I want to erase some area of that image using its TRANSPARENT COLOR?

Any color I use, makes the area opaque, when I simply want to a transparent area…

You need to use a 4 component colour with the alpha component set to 0, e.g.

Color col = new Color(0f,0f,0f,0f);

I think that should work

Kev

PS. Cicle = Cycle, Past = Passed? ???

As well as using a zero alpha color, you will also need to set the Composite operation to SRC.


Graphics g = img.getGraphics();
g.setColor(new Color(0x00000000,true));
g.setComposite(AlphaComposite.Src);
g.fillRect(0,0,img.getWidth(),img.getHeight());

Coo, I’ve never needed that, is it another windows works by default case?

Kev

Cool! ;D

Many thanks

Only a stupid formal correction…

Graphics2D g = (Graphics2D) img.getGraphics(); 
g.setColor(new Color(0x00000000,true)); 
g.setComposite(AlphaComposite.Src);
// Erase area
g.fillRect(area.x,area.y,area.width,area.height);
// Reset composite mode
g.setComposite(AlphaComposite.SrcOver);
// Draw anything...
// Dispose Graphic object
g.dispose();

[quote]Coo, I’ve never needed that, is it another windows works by default case?

Kev
[/quote]
um, nope.

If you draw using a completely transparent color with the default SRC_OVER rule you will end up painting nothing!
Changing the composite rule to SRC is essencial for it to work.

Is a newly created image on windows transparent black (0,0,0,0) or opaque black (0xff,0,0,0) ?

I haven’t had the time to check this myself, but I suspect that different platforms may initialize a newly created “empty” image
differently… You can see how this will effect the result when you draw something with transparency on top of the image. In one case it will appear as if the transparency is preserved, even though the wrong compositing rule is used.

[quote]Is a newly created image on windows transparent black (0,0,0,0) or opaque black (0xff,0,0,0) ?

I haven’t had the time to check this myself, but I suspect that different platforms may initialize a newly created “empty” image
differently…
[/quote]
yeah, on Windows its a transparent black 0x00000000.
on OSX I believe its opaque something. (black, white or gray i dunno, i’ve not got a mac ;))

Just for fun I checked this… and on the Mac you also get transparent black from a new 32 bit buffered image. So there goes that theory. Dang.

Maybe the byte order on the Mac makes a difference somehow? E.g. ABGR vs. ARGB. I think it boils down to bugs in the Mac JRE though. There are clearly bugs in this area.

oh! thats worrying ???

How were you creating the BufferedImage?

My presumptions were based off reports of others when working with the method :-


getGraphicsConfiguration().createCompatibleImage(width,height,Transparency.BITMASK); //might be worth checking TRANSLUCENT as well; you never know, they may behave differently 

I’d be grateful if you could test that method.

I was using sprite loading code that I think kevdog provided for a bug report I made to Apple.
It uses exactly the method you have above, it just queries the transparency from an existing image loaded from a .gif. I grab the raster of the image and loop through all the bands and print the values of the pixel at 0,0. It was 0 for all 4 bands.

Mind you the bug I was reporting was that the transparency was not copied to the newly created image even though the SRC composite rule was used when drawing the sprite to the compatible image.

Seems the Mac JRE is just buggy in this area. I guess I need to do more experiments… sigh…

Hmm. I just tried to create a simple transparent managed image using the suggested code:


getGraphicsConfiguration().createCompatibleImage(width,height,Transparency.BITMASK); 

…but it doesn’t work; compiler says “not implemented yet” ???

what’s the deal? how do i create a transparent managed image?

I guess u mean the Java runtime not the compiler, cos ‘not implemented yet’ is a runtime error.

What JRE & platform ru running?

oh, yes of course, the jre.
i use the jre 1.4.2 and windows xp. there shouldn’t be any problem, right?

hmmm. maybe somebody missed the question: how do i create a transparent managed image?

i first thought of starting a new thread in the newless clewbies section, but even here… where are you guys?

window.getGraphicsConfiguration().createCompatibleImage(width,height,Transparency.BITMASK);

or

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width,height,Transparency.BITMASK);

hmm. thanks. but that was my starting point a few posts ago:

[quote]Hmm. I just tried to create a simple transparent managed image using the suggested code:


getGraphicsConfiguration().createCompatibleImage(width,height,Transparency.BITMASK); 

…but it doesn’t work; compiler says “not implemented yet” ???

what’s the deal? how do i create a transparent managed image?
[/quote]
in order to receive response i expand the field of questions:
could my ati graphics card cause the java.lang.internalerror: not implemented yet?

yes,

Check for newer drivers.

Also, I seem to remember the JRE used to disable HW acceleration for ATI cards because of bugs in the drivers.
I don’t know if this still applies in 1.4.2.

Also, what color depth ru running in?
8bpp is abit buggy in my experience.