// Author: David Riley
// Date: July, 2002

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*  invariant
        An Tile object...
            Is a 20 by 20 square object.
            and has a position that is getX() pixels from the left
                and getY() pixels from the top
            and is colored either gray, red, or white */
public class ClickTile extends Container implements MouseListener {
    private boolean isEmptyTile;
    private String string;
    private Label lab;
    private Color color;
    
    /*	post:	A new Tile object is created and placed upon a
                and  getX() == x  and  getY() == y  and isEmpty()  
                and  stringValue() == "" and  getColor() == white */
    public ClickTile( int x, int y, Frame a )   {
        super();
        setBounds(x, y, 21, 21);
        a.add( this );
        isEmptyTile = true;
        string = "";
        lab = new Label("");
        lab.setBounds(5, 5, 10, 10);
        Font newFont = new Font( "Courier", Font.PLAIN, 8 );
        lab.setFont( newFont );
        lab.setForeground(Color.black);
        lab.setBackground(Color.white);
        repaint();
        addMouseListener(this);
    }

    /*	post:	isEmpty()  and  stringValue() == "" */
    public void setToEmpty( )   {
        removeAll();
        isEmptyTile = true;
        color = Color.white;
        string = "";
        repaint();
    }

    /*	post:	!isEmpty()  and  stringValue() == "" */
    public void setColor(Color c)   {
        removeAll();
        isEmptyTile = false;
        color = c;
        string = "";
        repaint();
    }
    
    /*	post:	stringValue() == s */
    public void setStringValue( String s )   {
        removeAll();
        string = s;
        isEmptyTile = false;
        lab.setText(s);
        add(lab);
        lab.repaint();
        repaint();
    }
    
    /*	post:	result == isEmptyTile
        note:	result is true exactly when the tile is being drawn in white */
    public boolean isEmptyCell()   {
        return isEmptyTile;
    }
    
    /*	post:	(isEmpty() OR stringValue.equals(""))
                    implies this cell is drawn in white with a black border
                and (!isEmpty()  implies  this cell is drawn in gray)
        note:	paint is not called directly, but is the result of a call to repaint() */
    public final void paint(Graphics g)   {
        if (isEmptyCell() || !string.equals(""))   {
            g.setColor( Color.black );
            g.drawRect(0, 0, 20, 20);
        } else if (!isEmptyCell()) {
            g.setColor( color );
            g.fillRect(0, 0, 20, 20 );
            g.setColor( Color.black );
            g.drawRect(0, 0, 20, 20);
        }
    }
        
// Mouse event handlers
    
    /*	post:	stringValue() == ""
                and (old isEmpty() == !isEmpty())  
        informally:	the tile toggles from white to gray or vice versa */
    public void mouseClicked(MouseEvent e)   {
        if (isEmptyCell())  {
            setColor(Color.gray);
        } else {
            setToEmpty();
        }
        repaint();
    }

// The mouse event handlers below do nothing but must be included to fully implement the interface
    public void mousePressed(MouseEvent e)
    {}

    public void mouseReleased(MouseEvent e)
    {}

    public void mouseEntered(MouseEvent e)
    {}

    public void mouseExited(MouseEvent e)
    {}


}

