import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.Random;
import javax.imageio.*;
import javax.swing.*;

/**
 * An example of animation
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class FishTank extends    JComponent 
                      implements ActionListener
{
    private BufferedImage      ocean;    
    private Fish[]             fish;    
    private Timer              timer;

    private static final Random  rng = new Random();


    /**
     * Default Constructor
     */
    public FishTank()
    {
       BufferedImage         threeImages;       

       try
       {
          ocean        = ImageIO.read(new File("ocean.png"));
          threeImages  = ImageIO.read(new File("fish.png"));
       }
       catch (IOException ioe)
       {
          ocean       = null;
          threeImages = null;          
       }

       fish = new Fish[5];       
       for (int i=0; i<5; i++) 
          fish[i] = new Fish(threeImages, 640, 480, rng.nextInt(3)+1);

       timer = new Timer(10, this);
       timer.start();
    }

    /**
     * Handle actionPerformed messages (required by ActionListener)
     *
     * @param  event   The ActionEvent that generated the message
     */
    public void actionPerformed(ActionEvent event)
    {
       repaint();
    }

    /*
     * Render this JComponent
     *
     * @param g   The rendering engine to use
     */
    public void paint(Graphics g)
    {
       Graphics2D           g2;
       

       g2 = (Graphics2D)g;

       if (ocean != null) g2.drawImage(ocean, 0, 0, null);

       for (int i=0; i<fish.length; i++) fish[i].paint(g);       
    }
}
