/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.game.testgame;

import gecco.server.automaton.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

/**
 *  Description of the Class
 *
 */
public class AutomatonMonitor extends JFrame {

    /**
     *  Description of the Field
     */
    private Graphics gfx;
    /**
     *  Description of the Field
     */
    private BufferedImage bufImg;


    /**
     *  Constructor for the AutomatonMonitor object
     *
     * @param  aMap  Description of Parameter
     */
    public AutomatonMonitor(AutomatonMap aMap) {
        super("AutomatonMonitor");

        bufImg = new BufferedImage(aMap.getXBound(), aMap.getYBound(), BufferedImage.TYPE_INT_RGB);
        getContentPane().setSize(aMap.getXBound(), aMap.getYBound());
        pack();
        gfx = bufImg.createGraphics();
        gfx.setClip(0, 0, aMap.getXBound(), aMap.getYBound());
        for (int x = 0; x < aMap.getXBound(); x++) {
            for (int y = 0; y < aMap.getYBound(); y++) {
                setPixel(x, y, gfx, aMap.getAutomaton(x, y).getState());
            }
        }
        setVisible(true);
        repaint();
    }


    /**
     *  Description of the Method
     *
     * @param  graph  Description of Parameter
     */
    public void paint(Graphics graph) {
        super.paint(graph);
        System.out.println("PAINT ANROPAD!");
        graph.drawImage(bufImg, 0, 0, this);
    }


    /**
     *  Description of the Method
     *
     * @param  x         Description of Parameter
     * @param  y         Description of Parameter
     * @param  newState  Description of Parameter
     */
    public void updatePixel(int x, int y, int newState) {
        System.out.println("UPPDATERAR PIXEL!");
        setPixel(x, y, gfx, newState);
        repaint();
    }


    /**
     *  Sets the Pixel attribute of the AutomatonMonitor object
     *
     * @param  x      The new Pixel value
     * @param  y      The new Pixel value
     * @param  gfx    The new Pixel value
     * @param  state  The new Pixel value
     */
    private void setPixel(int x, int y, Graphics gfx, int state) {
        Color c = new Color(state * 47);
        gfx.setColor(c);
        gfx.drawRect(x, y, 10, 10);
    }

}
