JavaRanch Home    
This page:         last edited 15 October 2007         What's Changed?         Edit

Code Barn Load A Gif Msg   

You must turn on Java for applets to work.

Explanation of LoadAGifMsg applet

Number of files: 3 (LoadAGifMsg.class, and two .gif filess - horsenav.gif and moosenav.gif)

What it does: Displays two .gif images in an applet. Both images are fully downloaded before being displayed. Displays a "wait...loading" message while the images are being loaded.

How to display a "wait" message while images are downloading...

  1. Create a boolean flag which represents whether a Wait message should be shown (images haven't fully loaded) or the images should be drawn: boolean showWaitMsg = true;
  2. Don't put your graphics loading code in your init method! The init() will always run BEFORE paint() is called. You can make your own "myInit" method (I called mine loadGraphics()) where you put all the code that WOULD have been in your init method.
  3. In paint(), include an if test using the flag instance variable to determine if the loading message should be shown. If true, then as soon as the load message is displayed, call the method (your second / real init method) that will load the actual applet graphics in.
  4. In this second / real init method, create the Image objects and the MediaTracker?, and block while the images are downloading.
  5. Once the images have finished downloading (waitForAll() completes) set the boolean showWaitMsg flag to false.
  6. Now when paint is called again, the showWaitMsg test will be false, so the actual images will be drawn rather than then Wait message.

How to display a gif file in an applet:

  1. Create an instance of Image
  2. Create an instance of MediaTracker
  3. Add the Image to the MediaTracker instance
  4. Call the MediaTracker object's waitForAll() method. The method will block until the image is fully downloaded.
  5. Override the applet's paint() method, and add a method call to drawImage().

You may skip steps 2, 3 and 4 if you don't mind that the paint() method will attempt to draw the image before it is fully downloaded.

A JavaDoc:java.awt.MediaTracker is just what the name suggests -- an object whose job is to track the download status of media elements. Sadly, it was implemented only for Images. Originally, it was designed to allow adding other media, such as audio files. So a more realistic name would be ImageTracker : (


import java.applet.* ;
import java.awt.* ;
public class LoadAGifMsg extends Applet 
    private Image theHorse ; 
    private Image theMoose ;
    private boolean showWaitMsg = true ; // will be set to false after image downloads
    public void init() 
        // anything in here will run BEFORE the paint() method is called,
        // even if you call repaint() now inside init
    } // close init
    public void loadGraphics() 
        // now load the graphics - this is like your "real" init
        theHorse = getImage( getCodeBase() , "horsenav.gif" );
        theMoose = getImage( getCodeBase() , "moosenav.gif" );
        MediaTracker mt = new MediaTrackerthis );
        mt.addImage( theHorse , 0 );
        mt.addImage( theMoose , 1 );
            mt.waitForAll();  // block here until images are downloaded
        catch ( InterruptedException e ) 
        showWaitMsg = false ; // it is safe for paint to draw the image now
    } // close loadGraphics 
    public void paint( Graphics g ) 
        // test a boolean to if the "loading" message should be displayed
        if ( showWaitMsg )
            g.drawString( "Please wait... loading..." , 20 , 20 );
            loadGraphics(); // call the method that actually loads the graphics
            g.drawImage( theHorse , 25 , 25 , this );
            g.drawImage( theMoose , 25 , 130 , this );
    } // close paint
// close applet


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton