/*
 * Replace - a thread which handles replacing a tile
 *
 * by Adam Doppelt
 * http://www.cs.brown.edu/people/amd/
 */
import java.awt.*;

public class Replace extends Thread {
    final static int TICKS = 20;
    final static int SLEEP = 25;

    Board board_;
    Graphics offscreen_, onscreen_;
    Piece newPiece_;
    int x_, y_, row_, col_;
    
    static Color colors_[];
    
    static {
	colors_ = new Color[TICKS];
	int half = TICKS / 2;
	for (int loop = 0; loop < TICKS; ++loop) {
	    float distance = Math.abs(loop - half) / (float)half;
	    colors_[loop] = new Color(distance, distance, distance);
	}
    }
    
    public Replace(Board board, Graphics offscreen, Graphics onscreen,
		   Piece newPiece, int row, int col) {
	board_ = board;
	offscreen_ = offscreen;	
	onscreen_ = onscreen;
	newPiece_ = newPiece;
	x_ = col * (Piece.SIZE + 1) + 1;
	y_ = row * (Piece.SIZE + 1) + 1;
	row_ = row;
	col_ = col;
	Score.Replaced();
	start();
    }

    public void run() {
	for (int loop = 0; loop < TICKS; ++loop) {
	    onscreen_.setColor(colors_[loop]);
	    onscreen_.fillRect(x_, y_, Piece.SIZE, Piece.SIZE);
	    offscreen_.setColor(colors_[loop]);
	    offscreen_.fillRect(x_, y_, Piece.SIZE, Piece.SIZE);

	    try {
		Thread.sleep(SLEEP);
	    } catch (InterruptedException e) { ; }
	}
	board_.ReplaceIt(newPiece_, row_, col_);
    }
}
