 // A Working Counting Applet - by Derek Molloy

  import java.applet.*;
  import java.awt.*;
  import java.awt.event.*;

  public class BadCounterFixed extends Applet implements ActionListener, Runnable
  {
	private int count = 0;
	private Button start, stop;
	private TextField countField;
	private boolean running = true;
	private Thread theThread;

	public void init() 
	{
	  countField = new TextField(10);
	  this.add(countField);

	  start = new Button("Start");
	  start.addActionListener(this);
	  this.add(start);

	  stop = new Button("Stop");
	  stop.addActionListener(this);
	  this.add(stop);

	  this.theThread = new Thread(this);
	}

	public void run() 
	{
	  while (running) 
	  {
	    try 
	    { 
	      Thread.currentThread().sleep(100); 
	    } 
	    catch (InterruptedException e)
	    {  }  //do nothing
	    countField.setText(Integer.toString(count++));
	  }
	}
	
	public void actionPerformed(ActionEvent e) 
	{
	  if (e.getSource().equals(start))
	  {
	    this.theThread.start();
	  }
	  else if (e.getSource().equals(stop))
	  {
	    this.running = !this.running;
	  }
	}
  } 
