//Brad Culberson 04.23.98
//this is the same smiley face as myapplet10 with double buffering.

import java.awt.*;
import java.applet.Applet;

public class MyApplet11 extends Applet implements Runnable
   {int frameNumber = -1;              
    int delay;                          
    Thread animatorThread;
    boolean frozen = true;
    Font font=new Font("TimesRoman",Font.BOLD,24);
    String framestr = "";
    int rval, gval, bval;             
    Dimension preImageDim;              
    Image preImage;                     
    Graphics preImageGraphics;        



    public void init()
       {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; 
        }                                  

  

    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()
       {setBackground(Color.white);
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        long startTime = System.currentTimeMillis();            
        while (Thread.currentThread() == animatorThread)      
           {frameNumber++;                             
            framestr = "Frame " + 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 ((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.blue);           
       preImageGraphics.setColor(Color.blue);	  
       int xstart = size().width/2;	
       preImageGraphics.drawOval(xstart-frameNumber,20,frameNumber,frameNumber);
       preImageGraphics.drawOval(xstart+frameNumber-frameNumber,20,frameNumber,frameNumber);
       preImageGraphics.drawOval(xstart-frameNumber/6,frameNumber+40,frameNumber/3,frameNumber/3);
       preImageGraphics.drawArc(xstart-frameNumber/2*3/2,frameNumber+(frameNumber/3)+60,frameNumber*3/2,frameNumber/3,180,180);

                                                                            
       g.drawImage(preImage,0,0,this);	              
       } 

    public void stop()
      {animatorThread = null;     
       preImageGraphics = null;  
       preImage = null;
       }	
    }