import java.awt.*;
import javax.swing.*;

/*
  this is the View component
*/

class LifeView extends JPanel
{
    private static int SIZE = 60;
    private LifeCell[][] myGrid;

    public LifeView()
    {
        myGrid = new LifeCell[SIZE][SIZE];
    }

    // entry point from model, requests grid be redisplayed
    public void updateView( LifeCell[][] mg )
    {
        myGrid = mg;
        repaint();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int testWidth = getWidth() / (SIZE+1);
        int testHeight = getHeight() / (SIZE+1);
        // keep each life cell square
        int boxSize = Math.min(testHeight, testWidth);

        for ( int r = 0; r < SIZE; r++ )
        {
            for (int c = 0; c < SIZE; c++ )
            {
                if (myGrid[r][c] != null)
                {
                    if ( myGrid[r][c].isAliveNow() )
                        g.setColor( Color.blue );
                    else
                        g.setColor( new Color(235,235,255) );
                    g.fillRect( (c+1)*boxSize, (r+1)* boxSize, 
                                                boxSize-2, boxSize-2);
                }
            }
        }
    }
}
