
import java.applet.*;
import java.awt.*;
import java.util.*;
import DrawingFrame;

// Main Class for applet Drawing

public class Drawing extends Applet implements Runnable {
	
	private Thread	 design = null;
	
	private boolean m_fStandAlone = false;
	
	int w, h;

	int posx, posy;

	long currentTime;

	private Image offImage;
	private Graphics offGraphics;

	private Image artImage;
	private Graphics artGraphics;

	private JoeyGraphics joeyGraphics;
	
	Vector lineObjects = new Vector(200);

	int step = 1;
	int drawingInterval = 15;
	
	boolean fading = true;
	
	Color drawingColor = new Color(0,0,0);
	
	int id = 6;

	Rectangle clear = new Rectangle(80,460,400,10);
	Rectangle drawingArea = new Rectangle(40, 40, 400, 400);
	Color interfaceColor = new Color(30,30,90);

	ColorSelectionRectangle[] colorRectangles = new ColorSelectionRectangle[10];	
	LittleNumber[] numbers = new LittleNumber[10];	
	Image dee;


	public static void main(String args[])	{
		DrawingFrame frame = new DrawingFrame("Drawing");

		frame.show();
        frame.hide();
		frame.resize(frame.insets().left + frame.insets().right  + 481,
			frame.insets().top  + frame.insets().bottom + 481);
		Drawing applet_Drawing = new Drawing();
		
		frame.add("Center", applet_Drawing);
		applet_Drawing.m_fStandAlone = true;
		applet_Drawing.init();
		applet_Drawing.start();
        frame.show();
	}
	

	
	public Drawing() {
		
	}
	
	public String getAppletInfo()	{
		return "Joanna Maria Berzowska";
	}
	
	
	public void init()	{


		dee = this.getImage(this.getDocumentBase(), this.getParameter("image"));
	
		resize(481, 481);
		Dimension d = this.size(); 		
		w = d.width;
		h = d.height;
		
		this.setBackground(Color.white);

		offImage = createImage(w, h);		
		offGraphics = offImage.getGraphics();
			
		artImage = createImage(400, 400);			
		artGraphics = artImage.getGraphics();
	

		joeyGraphics = new JoeyGraphics(artGraphics);

		for (int i=0; i<10; i++) {
			numbers[i] = new LittleNumber(Integer.toString(i), 450, 80+i*22);
		}

		colorRectangles[0] = new ColorSelectionRectangle(new Color(0,0,0), 0, 60, 20, 40);
		colorRectangles[1] = new ColorSelectionRectangle(new Color(177,95,130), 0, 100, 20, 40);
		colorRectangles[2] = new ColorSelectionRectangle(new Color(87,77,163), 0, 140, 20, 40);

		colorRectangles[3] = new ColorSelectionRectangle(new Color(220,200,43), 0, 180, 20, 40);
		colorRectangles[4] = new ColorSelectionRectangle(new Color(33,135,96), 0, 220, 20, 40);
		colorRectangles[5] = new ColorSelectionRectangle(new Color(235,155,243), 0, 260, 20, 40);

		colorRectangles[6] = new ColorSelectionRectangle(new Color(212,127,13), 0, 300, 20, 40);
		colorRectangles[7] = new ColorSelectionRectangle(new Color(132,123,83), 0, 340, 20, 40);
		colorRectangles[8] = new ColorSelectionRectangle(new Color(82,123,243), 0, 380, 20, 40);
		colorRectangles[9] = new ColorSelectionRectangle(new Color(169,214,13), 0, 420, 20, 40);
		
		
	

		drawInterface();
		
		artGraphics.setColor(Color.white);
			artGraphics.fillRect(0,0,399,399);
			artGraphics.setColor(new Color(230,230,230));
			artGraphics.drawRect(0,0,399,399);

	}
	

	public void start()	{
		if (design == null)	{
			design = new Thread(this);
			design.start();
		}

	}
	
	
	public void stop() {
		if (design != null)	{
			design.stop();
			design = null;
		}
	}
	
	public void destroy() {
		if (design != null)	{
			design.stop();
			design = null;
		}
	}
	
	public void paint(Graphics g) {
	
		offGraphics.drawImage(dee, 320, 0, this);

		offGraphics.drawImage(artImage, 40, 40, this);
		g.drawImage(offImage, 0 , 0, this);
	
	}
	
	public void update(Graphics g) {
		paint(g);
	}
	

	public void drawInterface() {

		offGraphics.setColor(Color.white);
		offGraphics.drawRect(0,0,480,480);

		for (int i=0; i<10; i++) {
			colorRectangles[i].drawColorSelectionRectangle(offGraphics);
			numbers[i].drawLittleNumber(offGraphics);
		}
	
		offGraphics.setColor(interfaceColor);
		offGraphics.drawRect(80,460,400,10);

		//		offGraphics.drawRect(0,40,20,2);
		//		offGraphics.drawRect(0,44,20,2);
		//		offGraphics.drawRect(0,48,20,2);
		//		offGraphics.drawRect(0,52,20,2);
		//		offGraphics.drawRect(0,56,20,2);
		
		//		offGraphics.drawRect(0,462,20,2);
		//		offGraphics.drawRect(0,466,20,2);
		//		offGraphics.drawRect(0,470,20,2);
		//		offGraphics.drawRect(0,474,20,2);
		//		offGraphics.drawRect(0,478,20,2);
		
	
	//	offGraphics.drawImage(dee, 320, 0, this);
	
		offGraphics.drawRect(0,0,480,480);
		
		repaint();
	}

		
	public void makeLastLine() {
		
		((DrawingLine)lineObjects.lastElement()).paintLine(joeyGraphics, drawingColor, step);

		repaint();
	}
	
	
	public void makeFadingLines() {
		int lineObjectsSize = lineObjects.size();
		
		if (lineObjectsSize != 0) {
			for (int i=0; i<lineObjectsSize; i++) {
				
			//	drawingColor = new Color(0,0,0);
				
				if ( ((DrawingLine)lineObjects.elementAt(i)).dead ) { 
					(((DrawingLine)lineObjects.elementAt(i)).linePoints).removeAllElements(); 
				}
				int s = ((DrawingLine)lineObjects.elementAt(i)).linePoints.size();
				
			//	if (fading) {
					drawingColor = ((DrawingLine)lineObjects.elementAt(i)).lighterColor(currentTime);
			//	}
			//	else  {
			//		drawingColor = ((DrawingLine)lineObjects.elementAt(i)).lineColor;
			//	}
				
			//	offGraphics.setColor(drawingColor);		
				
				for (int j=0; j<s-1; j++) {
						((DrawingLine)lineObjects.elementAt(i)).paintLine(joeyGraphics, drawingColor, step);

					


		//			joeyGraphics.drawIdLine(
		//				((DrawingLine)lineObjects.elementAt(i)).id,
		//				((DrawingPoint)(((DrawingLine)lineObjects.elementAt(i)).linePoints).elementAt(j)).x,
		//				((DrawingPoint)(((DrawingLine)lineObjects.elementAt(i)).linePoints).elementAt(j)).y,
		//				((DrawingPoint)(((DrawingLine)lineObjects.elementAt(i)).linePoints).elementAt(j+1)).x,
		//				((DrawingPoint)(((DrawingLine)lineObjects.elementAt(i)).linePoints).elementAt(j+1)).y);
				}
			}
			
		}	
		repaint();
	}


	
	public void run() {
		

		while (Thread.currentThread() == design) {
			
			
					currentTime = System.currentTimeMillis();		
			try {
				Thread.sleep(50);
			}
			catch (InterruptedException e) {
				stop();
			}
        }
	}
	
	public boolean mouseDown(Event e, int x, int y) {
		
		if (drawingArea.inside(x, y)) {	
	
			switch (id)	{
			case 4:
				step = 5;
				break;
			case 5:
				step = 5;
				break;
			case 7:
				step = 5;
				break;
			case 8:
				step = 5;
				break;
			case 9:
				step = 4;
				break;
			default:
				step=1;
			}

			lineObjects.addElement(new DrawingLine(currentTime, x-40, y-40, Color.blue, id));
			posx = x;
			posy = y;
		}
		return true;
	}
	
	
	public boolean mouseUp(Event e, int x, int y) {

//		if (drawingArea.inside(x, y)) {
			((DrawingLine)lineObjects.lastElement()).startTime = e.when;
//		}
		return true;
	}
	
	
	public boolean mouseDrag(Event e, int x, int y) {

//		if (drawingArea.inside(x, y)) {
			
			if ( ( Math.abs(posx-x)+Math.abs(posy-y) ) > drawingInterval ) {
				((DrawingLine)lineObjects.lastElement()).linePoints.addElement(new DrawingPoint(e.when, x-40, y-40));
				((DrawingLine)lineObjects.lastElement()).startTime = currentTime;
				
				posx = x;
				posy = y;
				
				makeLastLine();
	
				repaint();
			}
//		}		
		return true;
	}


	public boolean mouseMove(Event e, int x, int y) {

		for (int i=0; i<10; i++) {
			if (colorRectangles[i].rect.inside(x,y)) drawingColor = colorRectangles[i].c;
			if (numbers[i].rect.inside(x,y)) id = i;
		}

		if (clear.inside(x,y)) {
			
			artGraphics.setColor(Color.white);
			artGraphics.fillRect(0,0,399,399);
			artGraphics.setColor(new Color(230,230,230));
			artGraphics.drawRect(0,0,399,399);
			repaint();
		}

		return true;
	}	
}
