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

public class Ex3MOD extends JApplet
{
  private JPanel buttonPanel, drawPanel;
  private JButton startButton;
  private ArrayList<Thing> things = new ArrayList<Thing>();

  public void init()
  {
    // Add a bunch of random Things
    Random r = new Random();
    double cY = 300;
    double cX = 200;
    for(int i = 0; i < 100; ++i)
    {
      double x = cX/2  + (cX - 5)*r.nextDouble();
      double y = cY/2  + (cY - 5)*r.nextDouble();
      double dx = 15*r.nextDouble() - 7;
      double dy = 15*r.nextDouble() - 7;
      things.add(new Thing(x,y,dx,dy));
    }

    // Prepare the GUI
    // setSize(600,600);
    // setLocation(30,30);
    // setTitle("Java Animation Demo From Class");  
    setLayout(new BorderLayout());
    add(  drawPanel = new DrawPanel(),BorderLayout.CENTER);
    add(buttonPanel = new JPanel(),BorderLayout.EAST);
    startButton = new JButton("Start");
    startButton.setPreferredSize(new Dimension(80,30));
    startButton.addActionListener(
       new StartActionListener(startButton,drawPanel,things)
				  );
    buttonPanel.add(startButton);
    //    setVisible(true);    
  }

  //-- modify paintComponent so the Thing's get painted! --//
  class DrawPanel extends JPanel
  {
    public void paintComponent(Graphics g)
    {     
      super.paintComponent(g);
      RenderingHints hints = new RenderingHints(null);
      hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      ((Graphics2D)g).setRenderingHints(hints);
      for(Thing t : things)
	t.paint((Graphics2D)g);
    }
  }
}