// Adam Beguelin, adamb@cs.cmu.edu
//
// ideas: 
//	put line in it's own panel
//	make different verticies fixed, randomly or user selected
//	show multiple lines
//	allow radius to change as well
//	use different colors for the segments

import java.awt.Graphics;
import java.applet.*;
import java.awt.*;


public class wig extends java.applet.Applet implements Runnable {
	polar line[] = new polar[10];
	int len = 10;
	int delay = 100;
	int centerX = 150, centerY = 150;
	Thread wigout = null;
	boolean threadSuspended = false; 
	boolean showVerts = true;
	double radius = 15.0;
	double angleVar = 0.1;
	Choice radChoice, varChoice, delChoice;
	Checkbox vertBox;
	Panel controls;

	public boolean mouseDown(Event e, int x, int y) {
   		if (threadSuspended) { 
			wigout.resume();
			} 
		else {
			wigout.suspend();
			}
		threadSuspended = !threadSuspended;
		return true;
	}

	public void start() {
		if (wigout == null) {
			wigout = new Thread(this);
			wigout.start();
			}
	}

	public void run() {
	  while (wigout != null) {
		try {Thread.sleep(delay);} catch (InterruptedException e){}
		repaint();
		}
	  wigout = null;
	}

	public void init() {
		Panel c1, c2, c3;

   		resize(500,500); 

		initLine();

		// the vertBox
		vertBox = new Checkbox();
		vertBox.setLabel("Show Verticies");
		vertBox.setState(true);

		// the radius choices
		radChoice = new Choice();
		radChoice.addItem("5.0");
		radChoice.addItem("10.0");
		radChoice.addItem("15.0");
		radChoice.addItem("20.0");
		radChoice.addItem("25.0");
		radChoice.addItem("30.0");

		radChoice.select("15.0");

		// the angle variance choices
		varChoice = new Choice();
		varChoice.addItem("0.01");
		varChoice.addItem("0.05");
		varChoice.addItem("0.1");
		varChoice.addItem("0.2");
		varChoice.addItem("0.3");
		varChoice.addItem("0.4");
		varChoice.addItem("0.5");

		// the angle variance choices
		delChoice = new Choice();
		delChoice.addItem("10");
		delChoice.addItem("100");
		delChoice.addItem("500");
		delChoice.addItem("1000");

		controls = new Panel();
		controls.setLayout(new GridLayout(0,2));
		c1 = new Panel();
		c2 = new Panel();
		c3 = new Panel();

		c1.setLayout(new GridLayout(0,2));
		c2.setLayout(new GridLayout(0,2));
		c3.setLayout(new GridLayout(0,2));

		c1.add(new Label("Segment Length"));
		c1.add(radChoice);
		c2.add(new Label("Angle Variance"));
		c2.add(varChoice);
		c3.add(new Label("Millisecond Delay"));
		c3.add(delChoice);

		controls.add(c1);
		controls.add(c2);
		controls.add(c3);

		add(controls);

		controls.add(c1);
		controls.add(c2);
		controls.add(c3);
		controls.add(vertBox);
	}
	public boolean action(Event e, Object arg) {
		// probably should distinguish which choice...
        if (e.target instanceof Choice) {
			// set the new radius
			radius = Double.valueOf(radChoice.getSelectedItem()).doubleValue();
			if (e.target == radChoice) initLineRad();

			// set the new angle variance
			angleVar =Double.valueOf(varChoice.getSelectedItem()).doubleValue();

			// set the new delay
			delay = Integer.valueOf(delChoice.getSelectedItem()).intValue();

            return true;
			}
        if (e.target instanceof Checkbox) {
			// set showVerts
			showVerts = vertBox.getState();
			return true;
			}
        return false;
    }


	void initLineRad() {
		for (int i = 0; i < len; i++) {
			line[i].r = radius;
			}
    }

	void initLine() {
		for (int i = 0; i < len; i++) {
			line[i] = new polar();
			line[i].r = radius;
			line[i].theta = 0.0;
			}
    }

    public void paint(Graphics g) {
		int x,y, x1,y1;
		int i;

		move();

		// plot the line
		x = 0; y = 0;
		if (showVerts) 
			g.drawRect(centerX + x-1, centerY + y-1, 2, 2);
		for (i = 0; i < len; i++) {
			x1 = x + (int)(line[i].r * Math.cos(line[i].theta));
			y1 = y + (int)(line[i].r * Math.sin(line[i].theta));
			if (showVerts) 
				g.drawRect(centerX + x1-1, centerY + y1-1, 2, 2);
			g.drawLine(centerX + x, centerY + y, 
				centerX+x1, centerY+y1);
			x = x1; y = y1;

			}
    }

	// tweak the angles
	void move() {
		for (int i = 0; i < len; i++) {
			line[i].theta += Math.PI * (Math.random()* angleVar - angleVar/2.0);
			}
	}

	public void stop() { wigout = null; }

}

class polar {
	double r, theta;
}
