import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class StartActionListener implements ActionListener
{    
  private Thread t = null; 
  private JButton startButton;
  private JPanel drawPanel;
  private ArrayList<Thing> things;
  
  public StartActionListener(JButton B, JPanel P, ArrayList<Thing> T)
  {
    startButton = B; drawPanel = P; things = T;
  }
  
  public void actionPerformed(ActionEvent e) 
  { 
    if (t == null)
    {
      t = new Thread(new Ticker());
      t.start();
      startButton.setText("Stop");
    }
    else
    {
      t.interrupt();
      t = null;
      startButton.setText("Start");
    }
  }
  
  class Ticker implements Runnable
  {
    public void run() 
    {
      try {
	while(!Thread.currentThread().isInterrupted())
	{
	  Thread.sleep(30);
	  for(Thing t : things) t.move(drawPanel.getBounds());
	  drawPanel.repaint();	  
	} 
      } catch(InterruptedException ex) { }      
    }
  }    
}