

import java.util.*;
import java.awt.*;
import java.applet.*;


public class LinesTest02 extends Applet implements Runnable {
	
	int w, h;
	int posx, posy;
	
	Thread design;

	Color c = new Color(0,0,0);

	private Image offImage;
   	private Dimension offDimension;
	private Graphics offGraphics;

	Vector linePoints = new Vector();

	ColoredRectangle[] coloredRectangles = new ColoredRectangle[12];
	Color[] rectangleColors = new Color[12];

	public void init() {
		design = new Thread(this); 
		design.start();
		
		Dimension d = this.size(); 

		w = d.width;
		h = d.height;
		
		if ((offImage == null) || (w != offDimension.width) || (h != offDimension.height)) {
			offImage = createImage(w, h);
			offDimension = d;
			offGraphics = offImage.getGraphics();
		}

		offGraphics.setColor(Color.white);
		offGraphics.fillRect(0,0,w,h);
		
		for (int i=0; i<12; i++) {
			rectangleColors[i] = new Color(Color.HSBtoRGB ((float)((double)i/(double)12) , (float)(0.6), (float)(0.8))); 
			coloredRectangles[i] = new ColoredRectangle(w-35, 5+i*40, 30, 30, rectangleColors[i], offGraphics, false);			
		}

		coloredRectangles[0].selected = true;
		c = coloredRectangles[0].rectangleColor;

		repaint();
	}
	
	public void start() {
		if (design == null) {
			design = new Thread(this);
			design.start();
		}
    }
	
    public void stop() {
		if ((design != null) && design.isAlive())
			design.stop();
		design = null;
	} 
	
	public void destroy() {
		if ((design != null) && design.isAlive())
			design.stop();
		design = null;
	}  
	
	public void update(Graphics g) {
		paint(g);
	}
	
	public void paint(Graphics g) {
		offGraphics.setColor(Color.black);
		offGraphics.fillRect(w-40,0,40,h);
		for (int i=0; i<12; i++) {
			coloredRectangles[i].paintRectangle();
		}
		g.drawImage(offImage, 0, 0, this);
	}

		
	public void paintLine() {	
		
		int lineSize = linePoints.size();

		if (lineSize != 0) {
			for (int i=0; i<lineSize-5; i++) {
					
				offGraphics.setColor(c);
				offGraphics.drawLine(((Point)(linePoints.elementAt(i))).x, ((Point)(linePoints.elementAt(i))).y, 
					((Point)(linePoints.elementAt(i+4))).x, ((Point)(linePoints.elementAt(i+4))).y);	
			}
		}

		repaint();

	}

	public void run() {
        while (Thread.currentThread() == design) {
			try {Thread.sleep(50);}
			catch (InterruptedException e) {}
        }
    }

	public boolean mouseDown(Event e, int x, int y) {	
		linePoints.addElement(new Point(x, y));

		for (int i=0; i<12; i++) {

			if ( coloredRectangles[i].coloredRectangle.inside(x,y) ) {
				setSelect(x,y);
			}
		}
		return true;
	}
	
	public void setSelect(int x, int y) {

		for (int i=0; i<12; i++) {
			if ( coloredRectangles[i].coloredRectangle.inside(x,y) ) {
				coloredRectangles[i].selected = true;
				c = coloredRectangles[i].rectangleColor;
			}
			else {		
				coloredRectangles[i].selected = false;
			}
		}	
		repaint();
	}

	public boolean mouseUp(Event e, int x, int y) {
		linePoints.removeAllElements();
		return true;
	}
	
	public boolean mouseDrag(Event e, int x, int y) {			
		linePoints.addElement(new Point(x, y));
		paintLine();
		return true;
	}
}


				










