import java.awt.*;
import java.applet.*;
import java.lang.Math;

// IMPORTANT  NOTE:
// Must compile with
// javac -deprecation At.java
// to deal with the fact that this is  earlier
// version Java, able to run in browsers

/*
By Futrelle for COM1370 7/1/01
Draws delayed fat pixels using threads.
Code  adapted from our  earlier fat pixel / grid example
plus timing  code from Dori  Smith at:
http://www.chalcedony.com/java/code/prog10.5/listing5.html

All that's required is to implement the Runnable interface
with the methods start() and run() and an instance variable
of type Thread.    It's that simple!

Will be used in the future to move  objects around on the
screen at controlled rates.
*/

public class At extends Applet implements Runnable {
	
    int size = 24;  // of each pixel, in pixel units
    int dims = 16; // dimension of the square fat pixel array

    Thread runner;

	public void init() {
		setBackground(Color.white);
	}

    // The following is called by the system when loaded
    // into the browser.
    public void paint(Graphics g) {

	g.setColor(Color.black); // color  for grid
	drawGrid(g); // draws the entire grid
	// and labels it
	g.drawString("R. Futrelle COM1370 Summer 2001 Timer Demo, 7/1/01", 
		     2*size, 17*size);
	g.setColor(randomColor()); // set random  color
	drawFatPixel(g,randomInt(16),randomInt(16)); // random pixel  location


    } // paint

    // 0 to max - 1
    public int  randomInt(int  max){
	return (int) (max*Math.random());
    }

    public Color randomColor() {
	return new Color((float)Math.random(), 
			 (float)Math.random(), 
			 (float)Math.random());
    }
    
    public void start() {
	runner = new Thread(this);
	runner.start();
    }

    public void run() {
	for (int i=1;i<10;i++) {
	    try {
		Thread.sleep(1000);
	    }
	    catch (InterruptedException e) {
	    }

	    repaint();
	}
    }

    public  void drawGrid(Graphics g) {
	// sets the instance variables

	// draws empty outlined squares
	for(int i=0; i < dims; i++)
	    for(int j=0;j < dims; j++)
		g.drawRect(i*size, j*size, size, size);

    } // drawGrid()

    // fills one  "fat pixel" with current color
    public void drawFatPixel(Graphics g,  int x, int y) {

	g.fillRect(x*size, y*size, size, size);

    } // drawFatPixel()

}

