package net.jacobandreas.cellular;

import javax.swing.*;
import java.awt.*;

public class CAView extends JComponent {

    private CAModel model;

    public CAView(CAModel m) {
        super();
        model = m;
        setPreferredSize(new Dimension(model.getColumns(), model.getRows()));
    }

    public void paint(Graphics g) {
        super.paint(g);

        int mrows = model.getRows();
        int mcols = model.getColumns();

        int cellHeight = getHeight() / mrows;
        int cellWidth = getWidth() / mcols;

        for(int i = 0; i < mrows; i++) {
            for(int j = 0; j < mcols; j++) {

                int x1 = j * cellWidth;
                int y1 = i * cellHeight;

                int val = model.getValue(i,j);

                if(val != 0) {
                    g.setColor(new Color(val, 0, 0));
                } else {
                    g.setColor(Color.WHITE);
                }
                g.fillRect(x1, y1, cellWidth, cellHeight);
                /*g.setColor(Color.GRAY);
                g.drawLine(x1, y1, x1+cellWidth, y1);
                g.drawLine(x1, y1, x1, y1+cellHeight);*/
            }
        }
    }

}

