// Michael Tartaglia
// Draw: User draws line on screen using mouse


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Draw extends Applet {
	Point startPt, endPt;					// START AND END POINTS FOR LINE
	Choice colors = new Choice();			// CHOICE OF COLOR FOR LINE
	Label instruct = new Label("Choose A Color: ");	// LABEL FOR COLOR CHOICE

	public void init()
	{
		// CREATE COLOR LIST
		colors.addItem("Random");
		colors.addItem("Red");
		colors.addItem("Green");
		colors.addItem("Blue");
		colors.addItem("Black");
		colors.addItem("Grey");
		colors.addItem("White");
		colors.select(0);
		add(instruct);		// ADD INSTRUCTION LABEL
		add(colors);		// ADD COLOR SELECTION BOX

		setBackground(Color.white);
	}

	// WHEN MOUSE BUTTON IS PRESSED, CREATE START POINT
	public boolean mouseDown(Event e, int x, int y) {
	   startPt = new Point(x, y);
		return true;
	}

	// AS MOUSE IS DRAGGED, SHOW LINE ON SCREEN
	public boolean mouseDrag(Event e, int x, int y) {
			Graphics g = getGraphics();
		   endPt = new Point(x, y);
			setDrawColor(g);
			g.drawLine(startPt.x, startPt.y, endPt.x, endPt.y);
			startPt = endPt;
			return true;
	}

	// READ IN USER'S COLOR CHOICE AND SET IT
	public void setDrawColor(Graphics g) {
			int red = 0, green = 0, blue = 0;

			if (colors.getSelectedIndex() == 0) {
				red = (int)Math.floor(Math.random() * 255);
			   green = (int)Math.floor(Math.random() * 255);
			   blue = (int)Math.floor(Math.random() * 255);
			} else if (colors.getSelectedIndex() == 1) {
				red = 255;
				green = blue = 0;
			} else if (colors.getSelectedIndex() == 2) {
				green = 180;
				red = blue = 0;
			} else if (colors.getSelectedIndex() == 3) {
				blue = 225;
				red = green = 0;
			} else if (colors.getSelectedIndex() == 4) {
				red = green = blue = 0;
			} else if (colors.getSelectedIndex() == 5) {
				red = green = blue = 180;
			} else if (colors.getSelectedIndex() == 6) {
				red = green = blue = 255;
			}

			g.setColor(new Color(red,green,blue));
		}
}