RGBImageFilter not always filtering

Hello,

I have a strange problem. I made a simple RGBImageFilter that just
returns 0xFFFF0000. Thats right, it simply replaces every pixel with
a full alpha red pixel.

My problem comes when using the filter…



      //filter     - is my red filter
      //myApplet   - is the applet I'm running this from
      //original   - is an image that was loaded from a .png file and has
                       transparency
      //newImage   - is a BufferedImage that I created already with the
                       same dimensions as the "original" image

      newContext = (Graphics2D)newImage.createGraphics();

      Image coloredImage = myApplet.createImage( 
             new FilteredImageSource(original.getSource(), filter));

      //draw the red image
      newContext.drawImage(coloredImage, 0,0, null);

      //draw the original image over top of the red background
      newContext.drawImage(original, 0,0, null);

      newContext.dispose();
      return newImage;

I have this replacing colors in a function, and as it turns out, calling
this repeatedly with the SAME original image, SOMETIMES results in a
red background.

Now originally I didn’t have my filter just returning red, I am just doing
this to illustrate my point. I would like to know, is there some way to
make sure the call to createImage(…) has finished making the Image
before I draw it to something else?

I am using the latest java 1.5.0_03 both for development and in my browser(IE 6),
and the appletviewer does the same thing.

Here is a pic of the problem in action:

http://hooble.dyndns.org/java/filterproblem.JPG

Thanks for any help you guys can provide! I don’t post here much, but I’m a long time reader.

Maybe using a java.awt.MediaTracker will work.

I have managed to make it work reliably enough that I am satisfied(for now). I guess my problem, is that I want some way to force the createImage() operation on my current thread, instead of asynchronously having to wait for an ImageObserver to get updated.

I am now using a custom ImageObserver with a synchronized function that waits until the image is finished(or a timeout). I do that by using the component.prepareImage(img, observer) function, and pass it my observer.

This is fine in my current situation because I do my image preparations before my game starts. However, if anyone were to try this approach during a rendering loop, it would devastate the performance. I would like a synchronous way to prepare the image on the current thread, so that I could use filters in a rendering loop.

I guess this whole asynchronous image loading/creating is too well rooted to hope for something different. I don’t even mind the asynch image loading, it’s just this component.createImage() function that is bugging me, since it seems to be the only way to use an RGBImageFilter.

It appears that my only option, if I want to do color swapping in real time, is to have bitmasks for each image so I can use drawImage routines(yak!, I don’t want more resources to load over the net).

I don’t know if MediaTracker will work. From the API it seems to only keep track of image loading, not other operations that need an ImageObserver(create/draw).