/** Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  ** Extends Arthur van Hoff's animation template, to create an animated
  ** version of SimpleApplet5.
  **
  ** Modified to AnimatorApplet2 by John Pais, January 1998.              **/
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  AnimatorApplet2.html

/** USE EXISTING JAVA CLASSES **/

import java.awt.*;
import java.applet.Applet;

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

public class AnimatorApplet2 extends Applet implements Runnable
   {int frameNumber = -1;                // Define and/or initialize some
    int delay;                                     // AnimatorApplet2 class variables.
    Thread animatorThread;
    boolean frozen = true;
    Font font=new Font("TimesRoman",Font.BOLD,24);
    String framestr = "";
    int rval, gval, bval;                        // Define red, green, blue color variables.

    /** INITIALIZE THE APPLET **/

    public void init()
       {String str;
        int fps = 1;                                // 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.
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  AnimatorApplet2.html
 
    /** CLICK TO START OR STOP THE APPLET **/

    public boolean mouseDown(Event e, int x, int y)
       {if (frozen)
         {frozen = false;
          start();}
        else
         {frozen = true;
          stop();}
        return true;
        }

    /** GENERATE AND/OR START THE ANIMATION THREAD **/

    public void start()
       {if (frozen) { }                                           // The animation is supposed to stay frozen.
        else                                                          // Otherwise, generate and/or start the
         {if (animatorThread == null)                     // animation thread. Apparently, if run
           {animatorThread = new Thread(this);}    // breaks after catching an exception,
          animatorThread.start();                            // the animatorThread though not null
          }                                                            // will still need to be restarted.
        }
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  AnimatorApplet2.html
 
    /** CREATE AND DISPLAY FRAMES OF THE ANIMATION THREAD **/

    public void run()
       {setBackground(Color.white);
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);   // Set the priority low.
        long startTime = System.currentTimeMillis();                              // Remember start time.
        while (Thread.currentThread() == animatorThread)                     // The animation loop.
           {frameNumber++;                                                                  // Advance the frame number.
            framestr = "Frame " + frameNumber;                                     // Create the frame string.
            repaint();                                                                               // Display the frame.
            try                                                                                         // Sleep for the delay period.
             {startTime += delay;
              Thread.sleep(Math.max(0,
                                    startTime-System.currentTimeMillis()));
              }
            catch (InterruptedException e) {break;}                                  // In case the try caused
            }                                                                                           // something strange, exit
        }                                                                                               // the while loop.
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  AnimatorApplet2.html
 
    /** CREATE THE CURRENT FRAME OF THE ANIMATION THREAD **/
 
    public void paint(Graphics g)
     {if (!frozen)
       {g.setFont(font);
        g.setColor(Color.blue);
        g.drawString(framestr,155,27);
        for (int j = 40; j < (size().height - 25); j += 30)       // Set up vertical spacing.
         {for (int i = 5; i < (size().width - 25); i += 30)       // Set up horizontal spacing.
            {rval =(int) Math.floor(Math.random()*256);    // Generate a random color
             gval =(int) Math.floor(Math.random()*256);     // red, green, blue mix.
             bval =(int) Math.floor(Math.random()*256);     // Note the "casting" here.
 
             g.setColor(new Color(rval,gval,bval));              // Paint a small box with
             g.fillRect(i,j,25,25);                                          // the current random color.
             g.setColor(Color.black);                                  // Paint a black border around
             g.drawRect(i-1,j-1,26,26);                                // the edge of the small box.
             }
           }
         }
       }

    /** STOP THE ANIMATION THREAD **/

    public void stop()
       {animatorThread = null;}                 // The user either clicked to stop or left the page,
    }                                                       // so wipe out the current thread.