/** Use JWS to modify MyApplet13.java (previously AnimatorApplet5.java) 
 ** so that the scrolling text displayed is phrased correctly, e.g.  
 ** "0th" is not displayed and "1st", "2nd", "3rd" is displayed instead  
 ** of "1th", "2th", "3th", respectively. Make sure to fix all such  
 ** incorrect occurrences. In addition, change scrollDistance and 
 ** scrollPosition so that the text scrolls onto the page incrementally  
 ** from the left, starting with the last character of the scrolling  
 ** text, instead of the complete text jumping onto the page all at once.   
 ** Also, edit MyApplet13.html in the project directory, both to run the 
 ** MyApplet13.class file you create, and so that everything looks nice 
 ** and works properly.                                               **/


/** USE EXISTING JAVA CLASSES **/


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 = 25;                      
        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;
       }        
       }	
    
	/*if  (HiNumber == 0) 
	framestr = "Hi STLCOP for the 1st time !";      
	
	else if  ((HiNumber % 10 == 1) &&  (HiNumber % 100 != 11))
	   framestr = "Hi STLCOP for the " + HiNumber + "st time !"; 
	else   if  ((HiNumber % 10 == 2)  &&  (HiNumber % 100 != 12))
	   framestr = "Hi STLCOP for the " + HiNumber + "nd time !"; 
	 else   if  ((HiNumber % 10 == 3)  &&  (HiNumber % 100 != 13))
	   framestr = "Hi STLCOP for the " + HiNumber + "rd time !"; 
	  else
	   framestr = "Hi STLCOP for the " + HiNumber + "th time !"; 
	   
	int scrollDistance = fontm.stringWidth(framestr) + d.width;                         // Compute scroll position 
	int scrollPosition = -fontm.stringWidth(framestr) + (frameNumber % scrollDistance);    // of leftmost character      
	preImageGraphics.drawString(                          // of framestr, and draw
	   framestr,scrollPosition,d.height/2);	              // framestr above current
	
	
	preImageGraphics.drawImage(                           // image.
	   gifImage[frameNumber % 10],                        // Compute current image,
	   scrollPosition + (fontm.stringWidth(framestr))/3,  // and draw below framestr.
	   d.height/2,this);                                                                                         
							 // Offscreen preImage completed.
	g.drawImage(preImage,0,0,this);	                // Paint preImage on the screen.
	} 
      } */

    /** STOP THE ANIMATION THREAD **/

   /* public void stop()
      {animatorThread = null;     // The user either clicked to stop or left the page,
       preImageGraphics = null;   // so wipe out the current thread and preImage context.
       preImage = null;
       }	*/
