package earth;

import java.applet.Applet;
import java.awt.*;
import java.lang.Object;
import java.util.TimerTask;
import java.util.Timer;

public class earthimage extends Applet {
   private Image earth[];
   private int totalImages = 33,  // total number of images
               currentImage = 0,  // current image subscript
               sleepTime = 100;    // milliseconds to sleep
   MediaTracker imageTracker;
    Timer myTimer;

// load the images when the applet begins executing
   public void init()
   {
      myTimer = new Timer(true);
      myTimer.schedule(
                  new TimerTask ( ){    
                          public void run( ) {
                                                           repaint( );
                                                        }
                                       }
                  ,0, sleepTime);

      earth = new Image[ totalImages ];
      imageTracker = new MediaTracker( this );
      for ( int i = 0; i < earth.length; i++ ) {
         earth[ i ] = getImage( getDocumentBase(),
            "images/earth" + (i+1) + ".gif" );

// track loading image
         imageTracker.addImage( earth[ i ], i );
      }

      try {
         imageTracker.waitForID( 0 ); } catch( InterruptedException e ) { }
   }

   public void start(Graphics g)
   {  
      g.drawImage(earth[0], 0,0, 300, 300,this);
      currentImage = 1;
   }   


public void paint( Graphics g )
   {
      if ( imageTracker.checkID( currentImage, true ) ) 
     {   
          g.drawImage(earth[ currentImage ], 0, 0, 300, 300, this );
          if (currentImage== 0)
                  earth[totalImages-1].flush( );
          else  earth[currentImage-1].flush( );

         currentImage = ++currentImage % totalImages;
      }
      else
         postEvent( new Event( this, Event.MOUSE_ENTER, "" ) );

   }

// override update to eliminate flicker
   public void update( Graphics g )
   {
      paint( g );
   }
}
