import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;

/**
 * An image icon with a scaled/placed part
 * of an image. An ImagePuzzleIcon is constructed
 * with an image and a size --- the size is the length
 * of one side of the grid/model the icon will be used with.
 * <P>
 * Each ImagePuzzleIcon associated with the same image has
 * a self-determined number so that each "knows" what part of
 * the image it represents. This knowledge is used to scale
 * the image and show one portion of it --- the portion represented
 * by a particulare ImagePuzzleIcon.
 * <P>
 * A static count/variable will not work for determining how many
 * pieces there are of a particular image. A static count would work
 * if there is only one image ever used. Suppose, however, that three
 * images are used. Each image might be divided into 25 pieces --- and
 * each ImagePuzzleIcon object must know which of the 25 pieces it is.
 * But, if numbered 0,1,...,24, there will be a total of 75 different
 * ImagePuzzleIcons if there are three such larger collections, and
 * each one of the three must track which part it is.
 *
 * @author Owen Astrachan 
 */ 
public class ImagePuzzleIcon extends ImageIcon
{
    /**
     * construct from an image source with specified params
     */
    public ImagePuzzleIcon(Image im, String label,
			   int modelSize)
    {
	super(im);
	myLabel = label;
	init(modelSize);
    }

    private void init(int modelSize)
    {
	// determine what piece of image I am then adjust
	// my coordinates appropriately
	myCount = IconCounterCounter.getCount(getImage());
	mySize = PuzzleConsts.IMAGE_SIZE/modelSize;
	myX = myCount % modelSize * mySize;
	myY = myCount / modelSize * mySize;
    }

    /**
     * Paint me where I think I'm supposed to be
     * depending on what I am (blank or not)
     */
    public void paintIcon(Component c, Graphics g, int x, int y)
    {
	int fillSize = c.getWidth();
	if (! myLabel.equals(PuzzleConsts.BLANK)) {
	    g.drawImage(getImage(),0,0,fillSize,fillSize,
			myX,myY,myX+mySize,myY+mySize,c);
	}
	else {
	    g.setColor(c.getBackground());
	    g.fill3DRect(0,0,fillSize+PuzzleConsts.OFFSET,
			 fillSize+PuzzleConsts.OFFSET,true);
	}
    }

    protected int    myCount;      // what number image am I?
    protected int    myX;          // where am I (x,y) coordinates
    protected int    myY;
    protected String myLabel;      // determines if I'm blank or not
    protected int    mySize;       // used to break up/draw scaled image
}
