import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.Timer;
import java.awt.*;

public class GameOfLifeComponent
  extends JComponent
  implements ActionListener {

   private Timer timer;            // timer instance variable
   
   private GameOfLife game;


   public GameOfLifeComponent( GameOfLife g ) {
     game = g;
   }
      
   
   /**
    Starts the timer going by constructing a Timer object with the 
    frequency (in milliseconds) of Timer activations and the object ('this')
    that will be handling the Timer events
    */
   public void animate() {
      timer = new Timer( 100, this );
      timer.start();
   }
   
   
   /**
    Processes a Timer activation event
    */
   public void actionPerformed( ActionEvent e ) {
      game.nextGeneration();
      repaint();     // repaint the component on the screen
   }

   
   public void paintComponent( Graphics g ) {
     super.paintComponent( g );
     game.draw( (Graphics2D) g );
   }
   
}
