import java.awt.*;
import javax.swing.*;

/*
 *  Genetic Algorithm Exploration
 *  View component
 *  @author Roger Frank
 *  @version 1.1
 */

class GAview extends JPanel {

    private int[][] myGrid;

    /*
     * entry point from model, requests grid be redisplayed
     */
    public void updateView( int[][] myGrid ) {
        this.myGrid = myGrid;
        repaint();
    }

    /*
     * paintComponent goes through the grid and displays each path.
     * paths may overlap; the color corresponding to the last path 
     * over a cell sets the color.  
     * start and end cells are marked in red.
     */

    public void paintComponent( Graphics g ) {
        super.paintComponent( g );
        int testWidth = getWidth() / ( GA.GRIDX );
        int testHeight = getHeight() / ( GA.GRIDY );
        int boxSize = Math.min( testHeight, testWidth );

        for ( int r = 0; r < GA.GRIDY; r++ ) {
            for ( int c = 0; c < GA.GRIDX; c++ ) {
                if ( (r==1&&c==1) || (r==GA.GRIDY-2 && c==GA.GRIDX-2) ) {
                    g.setColor( Color.red );
                } else
                    if ( myGrid != null ) {
                        switch ( myGrid[ r ][ c ] ) {
                        case 0:
                            g.setColor( new Color( 242, 242, 200 ) );       // empty
                            break;
                        case - 1:
                            g.setColor( Color.black );                      // wall
                            break;
                        default:                                            // on path
                            int cr = 100 + (  31 + myGrid[ r ][ c ] * 17 ) % 155;
                            int cg = 100 + (  97 + myGrid[ r ][ c ] * 23 ) % 155;
                            int cb = 100 + ( 199 + myGrid[ r ][ c ] * 29 ) % 155;
                            g.setColor( new Color( cr, cg, cb ) );
                        }
                    }
                g.fillRect( (c+1)*boxSize, (r+1)*boxSize, boxSize-1, boxSize-1 );
            }
        }
    }
}
