FileDocCategorySizeDatePackage
StupidFace.javaAPI DocExample2078Tue Mar 06 13:53:50 GMT 2001None

StupidFace.java

//StupidFace.java by D Mullier 3/01
//needs two image file called 1.jpg and 2.jpg to be in the
//same directory as the class file

import java.awt.*;
import java.applet.*;
import java.math.*;

public class StupidFace extends Applet
{

  private TalkingFace Face;      //reference for our object, the class is defined below
  public void init()
  {

      Face=new TalkingFace();   //create an instance of a TalkingFace

  }

  public void paint(Graphics g)
  {
    Face.talk();//off we go, call the face drawing object
  }

  //NOW for our class definition
  public class TalkingFace
  {
    private Graphics gfx; //for our graphics context
    private Image StupidFace1,StupidFace2; //the images will be loaded into these
    private boolean talkingFlag=false;  //which animation frame to display
    private long talkCount=0;  //used to slow the talking down
    public TalkingFace()
    {
      StupidFace1 = getImage(getCodeBase(),"1.jpg");   //load the images into an object
      StupidFace2 = getImage(getCodeBase(),"2.jpg");
      gfx = getGraphics();  //get hraphics context, easy

    }
    public void talk()
    {

      for(;;)   //loop forever
      {
        long talkDelay=(long) ((Math.random()*10)+1);   //calculate a random delay between images
        //showStatus("w x h = "+width+" x "+height);//delay="+talkDelay);
        talkDelay*=500000;
        talkCount=1;

        while(talkCount!=0)
        {
          talkCount++;  //only change the animation frame for every "speed" calls of this method

            if(talkCount>=talkDelay)
            {
              if (talkingFlag==false)
                gfx.drawImage(StupidFace1,100,0,null); //draw the face and offset it by 100 pixels
              else
                gfx.drawImage(StupidFace2,100,0,null);
              talkingFlag=!talkingFlag; //flip the flag (i.e. false becomes true)
              talkCount=0;
            }//endif
         }//endwhile
       }//endfor
      }//end talk
     }//end talkingface

} //end applet