`Stressometer' example program: simple animation applet

/*==============================================================================

        Stress-o-meter

   A simple Java <applet;&gt. Note that, since it uses a fixed-size gif file
   for the background, the browser must request a size of 300 x 300 for the
   <applet;&gt, otherwise it looks ugly.

   The basic stress level is picked up from the parameter 'stresslevel' in the
   <&lt;applet;&gt> parameter block. This is a percentage. As the &lt;applet;&gt adds a randon
   amount between -10% and +10% to the stress level, you probably don't want
   to set this to more than 90%

   Kevin Boone, November 1997

==============================================================================*/

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.io.*;
import java.lang.*;

public class Stressometer extends java.&lt;applet;&gt.Applet implements Runnable
        {
   int BasicStressLevel;        // Obtained from the <&lt;applet;&gt> block
        Thread ThisProg;
   Image BackImage;                     // The background GIF file

   /*===========================================================================
   Init
        This function sets things up when the &lt;applet;&gt starts
   ===========================================================================*/
        public void init()
                {
      // Get the basic stress level from the <&lt;applet;&gt> block. If not
      //  specified, default to 40%
                String s = getParameter ("stresslevel");
                if (s == null)
        BasicStressLevel = 40;
      else
              BasicStressLevel = Integer.parseInt(s);

                // Load the background GIF file
                BackImage = getImage (getCodeBase(), "stressometer.gif");
                }

   /*===========================================================================
   Draw Colum
   Calculates a random offset to the basic stress level, and
   draws a mercury column of that height
   ===========================================================================*/
        private void DrawColumn (Graphics g)
                {
      int StressIncrement = (int)(Math.random() * 20 - 10);

      int StressLevel = BasicStressLevel + StressIncrement;     // Percent

      /* Calculate the size and shape of the white and red bar columns,
         and draw them */

      int UnstressLevel = 100 - StressLevel;
      g.setColor (Color.white);
      int BarHeight = 204;                                              // Pixel figures are from GIF file
                g.fillRect (49, 36, 8, BarHeight);
      g.setColor (Color.red);
                g.fillRect (49, 36 + UnstressLevel  * BarHeight / 100, 8, 38 + StressLevel  * BarHeight / 100);
                }

   /*===========================================================================
   paint
   Draw the background GIF and the mercury column whenever the &lt;applet;&gt has to
   be repainted
   ===========================================================================*/
        public synchronized void paint(Graphics g)
                {
                g.setColor(Color.black);
                g.drawImage(BackImage, 0, 0, this);
                DrawColumn (g);
                };

   /*===========================================================================
        Start, stop: standard runnable &lt;applet;&gt stuff
   ===========================================================================*/
        public void start()
                {
                ThisProg = new Thread(this);
                ThisProg.start();
      }

   public void stop()
                {
      ThisProg.stop();
      }

   /*===========================================================================
        Run
   At one second intervals, update the mercury column
   ===========================================================================*/
        public void run()
                {
                while (true)
                        {
                        try {Thread.currentThread().sleep(1000);} catch (InterruptedException e){}
                        Graphics WindowGC = getGraphics ();
                        DrawColumn (WindowGC);
                        }
                }
        }


/*
<applet code=stressometer>
  <param name=stresslevel value=60>
</applet>
*/

©1994-2003 Kevin Boone, all rights reserved