©1994-2003 Kevin Boone
Home     Section index     K-Zone home Simple Java programs

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
Software development
Computing
Hello1.java
Add1.java
Add1_1.java
AwtTest.java
Button1.java
Button2.java
Calculator.java
CalculatorApp.java
ClockApplet.java
CountSpace1.java
DateTime.java
Div1.java
DoNothing.java
Factorial1.java
Factorial2.java
FontChooser.java
IfTest.java
Inheritance1.java
Loan.java
DiceThrower.java
MultiplicationTable.java
Queue.java
Reverse1.java
SoundApplet.java
TextReaderTest.java
WebMerge.java
Zener.java
`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>
*/