/*
 * PieceType - stores all of the data associated with a type of pipe
 *
 * by Adam Doppelt
 * http://www.cs.brown.edu/people/amd/
 */
import java.awt.*;

public class PieceType {
    Image stamp_;
    int x1_[], y1_[], x2_[], y2_[];
    boolean visible_[], red_, green_, blue_;
    int legalDirections_, flipDirections_;

    public PieceType(Component c) {
	stamp_ = c.createImage(Piece.SIZE, Piece.SIZE);
	x1_ = new int[Piece.SLICES]; y1_ = new int[Piece.SLICES];
	x2_ = new int[Piece.SLICES]; y2_ = new int[Piece.SLICES];	
	visible_ = new boolean[Piece.SLICES];
    }

    public void GenerateImage() {
	Graphics stampG = stamp_.getGraphics();
	
	stampG.setColor(Board.BACKGROUND);
	stampG.fillRect(0, 0, Piece.SIZE, Piece.SIZE);
	for (int loop = 0; loop < Piece.SLICES; ++loop)
	    if (visible_[loop])
		ShadeLine(stampG, x1_[loop], y1_[loop], x2_[loop], y2_[loop]);
	stampG.dispose();
    }

    static void ShadeLine(Graphics g, double x1, double y1,
			  double x2, double y2, double minG, double maxG) {
	double length = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);	
	if (length <= 1) {
	    float gray = (float)((minG + maxG) / 2.0);
	    g.setColor(new Color(gray, gray, gray));
	    g.drawLine((int)Math.round(x1), (int)Math.round(y1),
		       (int)Math.round(x2), (int)Math.round(y2));
	}
	else {
	    double midX = (x1 + x2) / 2.0;
	    double midY = (y1 + y2) / 2.0;
	    double midG = (minG + maxG) / 2.0;
	    ShadeLine(g, x1, y1, midX, midY, minG, midG);
	    ShadeLine(g, midX, midY, x2, y2, midG, maxG);
	}
    }

    static void ShadeLine(Graphics g, int x1, int y1, int x2, int y2) {
	double midX = (x1 + x2) / 2.0;
	double midY = (y1 + y2) / 2.0;
	ShadeLine(g, x1, y1, midX, midY, 0.1, 0.9);
	ShadeLine(g, midX, midY, x2, y2, 0.9, 0.1);
    }
}

