/** After checking out the difference in behavior between 
 ** AnimatorApplet2.java and AnimatorApplet3.java, use JWS 
 ** to change the version of MyApplet10.java that you have
 ** created to a new file called MyApplet11.java, which 
 ** includes double buffering as in AnimatorApplet3.java.
 **
 ** NOTE:  Other than the instructions you are reading,
 ** I have NOT provided you with any code or files--you
 ** are completely on your own. Good luck !               **/
 
/** USE EXISTING JAVA CLASSES **/

import java.awt.*;
import java.applet.Applet;

/** EXTEND THE JAVA APPLET CLASS AND IMPLEMENT THE RUNNABLE INTERFACE **/

public class MyApplet11 extends Applet implements Runnable
   {int frameNumber = -1;                
    int delay;                           
    Thread animatorThread;
    boolean frozen = true;
    Font font=new Font("TimesRoman",Font.BOLD,18);
    String framestr = "";
    Dimension preImageDim;     
    Image preImage;             
    Graphics preImageGraphics;   
    
    /** INITIALIZE THE APPLET **/    
    /*
    public void init()       
    {String str;
        int fps = 15;                      
        str = getParameter("fps");         
        try                                
           {fps = Integer.parseInt(str);}  
                                          
        catch (Exception e)   {}           
        delay = (fps > 0) ? (1000/fps): 100; 
        }     
	}
	*/
  public void init()
       {String str;
        int fps = 15;                       // Define the default fps (frames per second).
        str = getParameter("fps");         // Possibly, get HTML string specifying the
        try                                // fps.
         {if (str != null)
           {fps = Integer.parseInt(str);}  // If possible, convert HTML fps to an
          }                                // integer.
        catch (Exception e) {}             // In case the try caused something strange.
        delay = (fps > 0) ? (1000/fps): 100; // Convert fps to the milisecond delay
        }                                    // time between frames.
	
								      
															      			                                      
    /** CLICK TO START OR STOP THE APPLET **/
  
  public boolean mouseDown(Event e, int x, int y)
       {if (frozen)
         {frozen = false;
          start();}
        else
         {frozen = true;
          animatorThread = null;           // Instead of calling stop() here, since
	  }                                // stop() now nullifies our preImage context,
        return true;                       // nullify only the animation thread in case
        }                                  // we want to build on our current preImage.

   /*   
    public boolean mouseDown(Event e, int x, int y)
	{if (frozen)
         {frozen = false;
	  start();
	  }
       else    
	 {frozen = true;
          stop();}
	  return true;
	  animatorThread = null;
	  }
	  return true;
	  }
    */
    
    /** GENERATE AND/OR START THE ANIMATION THREAD **/
    public void start()
       {if (frozen) { }                    
        else                               
         {if (animatorThread == null)      
           {animatorThread = new Thread(this);}  
          animatorThread.start();                
          }                                      
        }    
    
    /** CREATE AND DISPLAY FRAMES OF THE ANIMATION THREAD **/
   
     public void run()       
	{setBackground(Color.black);
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 
        long startTime = System.currentTimeMillis();             
        while (Thread.currentThread() == animatorThread)         
           {frameNumber++;                               
            framestr = "This is how much pharmacists make $  " + frameNumber;            
	    repaint();                                   
        try                                          
             {startTime += delay;
	     Thread.sleep(Math.max(0, startTime-System.currentTimeMillis()));
              }
        catch (InterruptedException e)
	{break;}     
            }                                           
        }                                               
    /** CREATE THE CURRENT FRAME OF THE ANIMATION THREAD **/
  public void paint(Graphics g)  
     {if (!frozen) update(g);}      
					  
    public void update(Graphics g)    
     {Dimension d = size();                     
      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.green);        
       preImageGraphics.drawString(framestr,20,110);	   
	
	                                                                                    
       g.drawImage(preImage,0,0,this);	     
       }     
        /** 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;
       }	
    }     
       
       
       
       
       