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...
- 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;
- 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.
- 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.
- In this second / real init method, create the Image objects and the MediaTracker?, and block while the images are downloading.
- Once the images have finished downloading (waitForAll() completes) set the boolean showWaitMsg flag to false.
- 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:
- Create an instance of Image
- Create an instance of MediaTracker
- Add the Image to the MediaTracker instance
- Call the MediaTracker object's waitForAll() method. The method will block until the image is fully downloaded.
- 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 : (
Code
import java.applet.* ;
import java.awt.* ;
public class LoadAGifMsg extends Applet
{
private Image theHorse ;
private Image theMoose ;
private boolean showWaitMsg = true ;
public void init()
{
}
public void loadGraphics()
{
theHorse = getImage( getCodeBase() , "horsenav.gif" );
theMoose = getImage( getCodeBase() , "moosenav.gif" );
MediaTracker mt = new MediaTracker( this );
mt.addImage( theHorse , 0 );
mt.addImage( theMoose , 1 );
try
{
mt.waitForAll();
}
catch ( InterruptedException e )
{
}
showWaitMsg = false ;
repaint();
}
public void paint( Graphics g )
{
if ( showWaitMsg )
{
g.drawString( "Please wait... loading..." , 20 , 20 );
loadGraphics();
}
else
{
g.drawImage( theHorse , 25 , 25 , this );
g.drawImage( theMoose , 25 , 130 , this );
}
}
}
CodeBarnApplets
|