/*==============================================================================
Stress-o-meter
A simple Java <applet;>. 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;>, otherwise it looks ugly.
The basic stress level is picked up from the parameter 'stresslevel' in the
<<applet;>> parameter block. This is a percentage. As the <applet;> 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.<applet;>.Applet implements Runnable
{
int BasicStressLevel; // Obtained from the <<applet;>> block
Thread ThisProg;
Image BackImage; // The background GIF file
/*===========================================================================
Init
This function sets things up when the <applet;> starts
===========================================================================*/
public void init()
{
// Get the basic stress level from the <<applet;>> 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 <applet;> 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 <applet;> 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