//Brad Culberson 04.23.98
//image moving across screen, with text counting.

import java.awt.*;
import java.applet.Applet;

public class MyApplet13 extends Applet implements Runnable
   {int frameNumber = -1;                
    int delay;                          
    Thread animatorThread;
    boolean frozen = false;
    Font font=new Font("TimesRoman",Font.BOLD,24);
    FontMetrics fontm = getFontMetrics(font);
    String framestr = "";
    Dimension preImageDim;             
    Image preImage;                   
    Graphics preImageGraphics;        

    Image gifImage[];                  
    MediaTracker tracker;               
    int HiNumber;

    public void init()
       {setBackground(Color.red);
	String str;
        int fps = 10;                      
        str = getParameter("fps");       
        try                                
         {if (str != null)
           {fps = Integer.parseInt(str);} 
          }                               
        catch (Exception e) {}             
        delay = (fps > 0) ? (1000/fps): 100; 
                                            		     
	gifImage = new Image[10];
        tracker = new MediaTracker(this);         
        for (int i = 1; i <= 10; i++)             
         {gifImage[i-1] = getImage(getCodeBase(),
                        	   "images/T"+i+".gif");
	  tracker.addImage(gifImage[i-1],0);
          }
	}  
	
    public boolean mouseDown(Event e, int x, int y)
       {if (frozen)
         {frozen = false;
          start();}
        else
         {frozen = true;
          animatorThread = null;          
	  }                               
        return true;                       
        }                                

    public void start()
       {if (frozen) { }                  
        else                            
         {if (animatorThread == null)      
           {animatorThread = new Thread(this);}  
          animatorThread.start();                
          }                                    
        }

    public void run()
       {try                                            
	 {tracker.waitForAll();}                        
        catch (InterruptedException e) {}               
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        long startTime = System.currentTimeMillis();            
        while (Thread.currentThread() == animatorThread)        
           {frameNumber++;                               
            repaint();                                  
            try                                         
             {startTime += delay;
              Thread.sleep(Math.max(0,
                                    startTime-System.currentTimeMillis()));
              }
            catch (InterruptedException e) {break;}   
            }                                           
        }                                             
    
    public void paint(Graphics g)         
     {if (!frozen) update(g);}            
					
    public void update(Graphics g)      
     {Dimension d = size();              
      if (!tracker.checkAll())                    
       {g.clearRect(0, 0, d.width, d.height);     
	g.drawString("Please wait...",            
	             (d.width-20)/2, d.height/2);   
        }
      else	       	               
       {if ((preImageGraphics == null) ||          
	    (d.width != preImageDim.width) ||        
	    (d.height != preImageDim.height))        
	     {preImageDim = d;                    
	      preImage = createImage(d.width,d.height); 
	      preImageGraphics = preImage.getGraphics();
	      }                                     
	preImageGraphics.setColor(getBackground());      
	preImageGraphics.fillRect(0,0,d.width,d.height); 
	preImageGraphics.setFont(font);                 
	preImageGraphics.setColor(Color.black);        
		 					
	if ((frameNumber + 5) % 10 == 0) 
	    {HiNumber++;}
	if (HiNumber != 0)
	{int intend = HiNumber % 10;
	String strend = "th";
	if (intend == 1) strend = "st";
	if (intend == 2) strend = "nd";
	if (intend == 3) strend = "rd";
	intend = HiNumber % 100;
	if (intend == 11) strend = "th";
	if (intend == 12) strend = "th";
	if (intend == 13) strend = "th";
	framestr = "Hi StLCOP for the " + HiNumber + strend + " time !"; } 
	int scrollDistance = d.width + fontm.stringWidth("Hi StLCOP for the 0000th time !");                  
	int scrollPosition = d.width - frameNumber % scrollDistance;    

	preImageGraphics.drawString(                          
	   framestr,scrollPosition,d.height/2);	             
	preImageGraphics.drawImage(                      
	   gifImage[frameNumber % 10],                   
	   scrollPosition + (fontm.stringWidth(framestr))/3, 
	   d.height/2,this);                                                                                         
							
	g.drawImage(preImage,0,0,this);	             
	} 
      } 

    public void stop()
      {animatorThread = null;   
       preImageGraphics = null;  
       preImage = null;
       }	
    }