Flickering at start of applet

for my game applet i use normal double buffering and sprites.
all works fine except the thing, that right after starting the applet when the sprintes are drawn first time there’s flickering or even some animation frames are missing.

any idea how to avoid this ?

Sounds to me like you are loading your images asynchronously, and you arn’t using MediaTracker to wait for them to completely load.

Either use java.awt.MediaTracker to wait for them to completely load, or use the newer way of loading images, which avoids MediaTracker altogether -> javax.imageio.ImageIO.read(image);

im definitly using mediatracker …

if abuses’s suggestion of the use of javax.imageio.ImageIO.read(image) works in your code, it is certainly because there is a problem with the way you use the media trackers.

indeed there could be a problem with my use of the MT.
first i load the image-strips (a big image containing serveral frames) through the mediatracker. after completely loading i split those images into the frame parts by using createImage and ImageProducer.

i thought all operations on a correct loaded image would create another correct loaded image …

if you arn’t limited to the 1.1 libs, I would strongly suggest you stop using createImage/ImageProducer/MediaTracker and that whole host of methods.
They are old, slow, buggy, and hopefully soon to be depricated.

use javax.imageio.ImageIO.read() and java.awt.image.BufferedImage,
they are new, not so slow, and errrr still as buggy :smiley:

Its true what Abuse says, its better to use newer methods if you can. This is a common problem I’ve never heard a good explanaton for. The last best I’ve read is that MediaTracker is non blolcking so it will return wether your Image is loaded or not (I’ve tried every permutation imaginable with that class; mediaTracker.waitforAll, etc…) so you start working with Images that aren’t there yet.

One workaround is to use a pixel grabber to get your resources loaded delay execution of your main drawing code until all resources are loaded. Pixel grabber sucks but you only use it once.

i looked into a gaming book of mine (brackeen, developing games in java). the author suggests to use swing’s ImageIcon.

can somebody clear me up when to use ImageIcon or ImageIO.read() ?

btw: has anyone read brackeen’s book ?

new ImageIcon().getImage();

Is the lazy way of doing image loading, it uses the old toolkit.createImage() & MediaTracker process.

I would say that, if you can you should always use ImageIO.read(), if its available (1.4 upwards)

Unless of course you want an ImageIcon anyway :).
In 1.5 for sure use ImageIO… In 1.4.x be careful if you need to have your image “managed” (automatically accelerated in VRAM for you). ImagIO images won’t automatically be accelerated in 1.4, you need to blit them into a managed image or VolatileImage. Whereas the 1.4 toolkit methods give you managed images.

last question to this topic then i turn to the swing classes:

currently im still using imageicon. this class takes care of a proper use of the mediatracker. nevertheless i get those problems (not only flickering, in some cases i get a value of -1
when i call getWidth() …)

then i tried a dirty trick of a game book:

i created a temp. graphic object and drawed all image-strips once to it. now the loading time of the applet is horrible, but it works …

any explanations ?

It sounds like MediaTracker is broken, or it doesn’t like the images you are giving it.

Can you construct a small testcase that demonstrates the problem?

Oh, and did you try using imageIO?
did that solve the problem?
If so, then there is definitly something playing up inside MediaTracker.
(I wouldn’t dare to say what though, the code inside MediaTracker is pretty attrocious :P)

you’re interested in some code … wonderful:


public class ImageLoader
{
      private static ArrayList strips;
      Applet applet;    
      
      ImageLoader(Applet applet)
      {
            this.applet = applet; 
            strips = new ArrayList();
        }

        img = new ImageIcon(getClass().getResource("ingame/door_anim.png")).getImage();
            createAnimationStrip(img, 4);


         private void createAnimationStrip(Image img, int number)
      {
            int cellHeight = img.getHeight(null);
            int cellWidth  = img.getWidth(null) / number;
            
            ImageProducer sourceProducer = img.getSource();
            
            AnimationStrip anim = new AnimationStrip();
            
            for(int i=0; i<number; i++)
                  anim.add(loadCell(sourceProducer,i*cellWidth, 0, cellWidth, cellHeight));    
                  
            strips.add(anim);
      }


      private Image loadCell(ImageProducer imageProducer, int x, int y, int width, int height)
      {
             return applet.createImage(
                                                      new FilteredImageSource(
                                                                                          imageProducer, 
                                                                                          new CropImageFilter(x, y, width, height)
                                                                                       )
                                                  );
            
      }
}

that’s my code for convertint image-strips to animation-strips. after that i expected that i’m able to use them without any problems.

ah, thats your problem right there :-

MediaTracker/ImageIcon only fully loads the image for the given Image object.

The Image[s] returned by loadCell, although created from a fully loaded image, will need loading completely themselves also.

As has been said on several occasions in this Thread - if you arn’t limited to 1.1, I would seriously consider forgetting the old API for loading images, it is a pain to use, and is very inefficient.