// Sample applet demonstrating the continuous display of a counter
// with start, stop & reset buttons
// could be adapted for assignment
// J harvey, April 2001
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.lang.Runnable;
import java.text.*;
public class Counter extends Applet implements Runnable, ActionListener
{
private Button start, stop, reset;
private Panel testPan;
private TextField T1, T2;
private Label L1, L2;
private DecimalFormat nf;
Thread counterThread;
boolean on=false;
private double value=0;
public void init()
{
T1 = new TextField(12);
T2 = new TextField(12);
L2=new Label("Value");
L1=new Label("Volume");
start = new Button("STARTX");
start.addActionListener(this);
stop = new Button("STOP");
stop.addActionListener(this);
reset = new Button("RESET");
reset.addActionListener(this);
testPan = new Panel();
testPan.add(start);
testPan.add(stop);
testPan.add(reset);
testPan.add(L1);
testPan.add(T1);
testPan.add(L2);
testPan.add(T2);
add(testPan);
} // end init
public void actionPerformed(ActionEvent ae)
{
if ((ae.getSource() == start))
{
on = true;
counterThread = new Thread(this, "");
counterThread.start();
}
if ((ae.getSource() == stop))
{
on=false;
repaint();
}
if((ae.getSource() == reset))
{
if(on==false) // Don't want reset button to work if on is true
{
nf = new DecimalFormat("000.00");
value=0.0;
T1.setText("" + value + nf.format(value));
repaint();
} // endif
} // endif
} // end actionPerformed
public void paint(Graphics g)
{
} // end paint
public void run()
{
while (on==true)
{
value = value+0.05;
T1.setText("" + value); // output needs formatting properly
repaint();
try
{
counterThread.sleep(50);
}
catch (InterruptedException ie)
{
}
}// end while
} // end run
} // end Counter
|