// Michael Tartaglia
// ColorClick: Program exemplifies use of MouseListener


import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;

public class ColorClick extends Applet
implements MouseListener
{
   private int index = 0,		// CURRENT CLICK MARKER
   				MAX_ARRAY = 10;// NUMBER OF CLICKS TO SHOW
   private Label instruct;		// INSTRUCTIONS LABEL
   private PointInfo[] points = new PointInfo[MAX_ARRAY];	// POINTS ARRAY ('PointInfo' CLASS BELOW)

   public void init() {
      setBackground(Color.white);
   
		// WRITE INSTRUCTIONS
      instruct = new Label("For psychadelia, just click anywhere!");
      instruct.setFont(new Font("Arial",Font.BOLD,12));
      instruct.setBackground(Color.white);
      
      add(instruct);
      addMouseListener(this);		// ADD LISTENER TO CANVAS
   }
   
	// MOUSE EVENT HANDLERS
   public void mousePressed(MouseEvent E) {}
   public void mouseReleased(MouseEvent E) {}
   public void mouseEntered(MouseEvent E) {}
   public void mouseExited(MouseEvent E) {}
   public void mouseClicked(MouseEvent E) {
	   if (index < MAX_ARRAY) {
		   points[index] = new PointInfo(E.getX(),E.getY());
		} else {
	   	points[index%MAX_ARRAY].setX(E.getX());	// SAVE X LOCATION
			points[index%MAX_ARRAY].setY(E.getY());	// SAVE Y LOACTION
		}
    	++index;	// INCREASE NUMBER OF TOTAL CLICKS 
     	repaint();
   }
   
   public void paint (Graphics toScreen) {
	// FUNCTION: method paints text message to screen at location
	//			specified by PointsInfo array
      int r, g, b;								// RED, GREEN, BLUE COLOR VALUES
      toScreen.setFont(new Font("Arial", Font.BOLD+Font.ITALIC, 10));
      for (int i = 0; i < index || i < MAX_ARRAY; ++i) {
         if (i == index) break;
         r = (int)(Math.random()*220);				// RANDOM AMOUNT OF RED
         g = (int)(Math.random()*220);				// " " " GREEN
         b = (int)(Math.random()*220);				// " " " BLUE
         toScreen.setColor(new Color(r,g,b));	// SET COLOR
         toScreen.drawString(points[i].getText(),
         	points[i].getX(),
         	points[i].getY());	// DRAW STRING AT LOACTION X,Y
      }
      try {Thread.sleep(100);}
      catch (InterruptedException ie) {}
      repaint();
   }
}



class PointInfo {
   private int x = 0,y = 0;		// x, y INTEGER COORDINATES
   private String text;				// TEXT TO SAVE

	// CONSTRUCTORS
   public PointInfo(int a, int b, String t) {
      x = a; y = b; text = t;
   }
   public PointInfo(int a, int b) {this(a,b,"Clicked at point ("+a+", "+b+") !");}
   
	// COORDINATE GET AND SET METHODS
   public void setX(int a) {x = a;}
   public int getX() {return x;}
   public void setY(int b) {y = b;}
   public int getY() {return y;}
	
	// TEXT MESSAGE GET AND SET METHODS
   public void setText(String t) {text = t;}
   public String getText() {return text;}
   
}