Using alpha with Graphics.drawImage

Colorkeyed sprites in my game go through the following steps:

- Loading the image from getImage

- Running through a filter I made, ColorKeyFilter, which just sets all pixels of the specified color key with an alpha value of 0

It seems to work fine, but sometimes there is flicker where they fail to draw at all. It's very rare that it dosen't draw but sometimes it happens. Additional poking and prodding later I found out that drawImage returns false every time when rendering an image I set an alpha on, even if it appears ok.

I found that if I don't use ColorKeyFilter to set an alpha value then everything is beautiful (except for the fact that nothing is color keyed of course). drawImage returns fine, and there is no occasional flicker.

Should I just use .gif and it's transparency feature or something?

Also I'm quite new to java graphics programming, how would I go about making a blank image using an indexed color model, and specifying the transparent color?

I'd like to make it load indexed png files eventually. gif is evil.

[1100 byte] By [electric_monk] at [2007-9-27 14:34:15]
# 1
Check out:BufferedImage(int width, int height, int imageType, IndexColorModel cm)
zparticle at 2007-7-5 22:33:40 > top of Java-index,Other Topics,Java Game Development...
# 2
BTW I'm using transparent GIFs and PNGs with no problems, but I don't have to modify them after loading because they are already correct.
zparticle at 2007-7-5 22:33:40 > top of Java-index,Other Topics,Java Game Development...
# 3
I would guess you're trying to do a drawImage onto the Graphics object of an Image which doesn't support alpha. If it's a BufferedImage, make sure you use type BufferedImage.TYPE_INT_ARGB.
YATArchivist at 2007-7-5 22:33:40 > top of Java-index,Other Topics,Java Game Development...
# 4

Are you ensuring the Image is fully loaded before you apply your filter?

(MediaTracker allows an easy mechanism for doing this - there are alot of better, neater, more flexible ways of doing it - but stick with MediaTracker for now, its simple ;)

Images are loaded asynchronously (which im sure your aware of), this would explain why the problem only occurs occasionally.

rob,

p.s. it would be better to include the transparency info. in the actual image file, that way it requires less processing, smaller code, and wouldn't reduce the number of representable colors in the image.

Afterall thats why gif and png support for Alpha! So use it ! (get a copy of PSP, it can easily perform the tasks you need)

p.s. I agree, gif is absolute shite

Abuse at 2007-7-5 22:33:40 > top of Java-index,Other Topics,Java Game Development...
# 5
well i actually found the problem, it was the microsoft jvmit works fine under sun's jvm and is faster and looks better. yay.
electric_monk at 2007-7-5 22:33:40 > top of Java-index,Other Topics,Java Game Development...
# 6
were u using PixelGrabber? I seem to remember it is broken in M$ JVM
Abuse at 2007-7-5 22:33:40 > top of Java-index,Other Topics,Java Game Development...
# 7
yes i was using pixelgrabber, and so that could be it.
electric_monk at 2007-7-5 22:33:40 > top of Java-index,Other Topics,Java Game Development...