/*
	DrawSteps is an  applet that Draws graphics 
	Step by step to help students understand the process 
	of several Geometric Constructions using Compass and straight Edge.  - 29 Nov 98 Fr. Chris Thiel, OFMCap <cct@ktb.net>
*/

import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
import java.lang.Math;


public class DrawSteps extends Applet
{
	//Global Variables
	Choice daMenu = new Choice();
	int currentStep=0;
	int lastStep=5;
	Color buttonColor;
	int buttonWidth=35;
	int buttonHeight=20;
	Rectangle go,back;
	Point A,B,M,C;
	
	private void circumscribe (Graphics g, int centerX, int centerY, int radius, int startArc, int endArc) {
		double theta [];
		theta = new double [21];
		g.setColor(Color.green);
		for (int j=0; j <= 20; j++ ) theta[j] = Math.PI*startArc/180 + j*((Math.PI*endArc/180)/20);
		for (int j=0; j < 20 ; j++) {
		 	g.drawLine(centerX +(int)(radius*Math.cos(theta[j])),(int)(centerY-(radius*Math.sin(theta[j]))), 
		              centerX +(int)(radius*Math.cos(theta[j+1])), (int)(centerY-(radius*Math.sin(theta[j+1]))));
		}
		g.setColor(Color.black);
	}
	private void dot (Graphics g, int x, int y, String label) {
	 	g.setColor(Color.red);
	 	g.fillOval(x-2,y-2,5,5);
	 	g.setColor(Color.black);
	 	g.drawString(label, x-4 , y-4);
	}
	public void init() {
	    buttonColor = new  Color( 200,250, 220);
	    setBackground(new Color(0xF0F0D0));
	    go = new Rectangle ( size().width-buttonWidth-10,size().height-buttonHeight-10,buttonWidth,buttonHeight);
	    back= new Rectangle ( 10, size().height-buttonHeight-10,buttonWidth,buttonHeight);
	    A = new Point (50, 100);
	    B = new Point (150,100);
	    M = new Point ( (int)((A.x+B.x)/2), (int)((A.y +B.y)/2) );
	    C = new Point (150,50);
	    daMenu.addItem("Bisect a Segment");
		daMenu.addItem("Bisect an Angle");
		daMenu.addItem("Copy an Angle");
		daMenu.addItem("Perpendicular");
		daMenu.addItem("Golden Spiral");
		
		add(daMenu);
		repaint();
	}
	public void bisectSegment (Graphics g) {
		switch (currentStep) {
			case (7):
			case (6):
			case (5):
				dot (g, M.x, M.y, "M");
			case (4):
				g.setColor(Color.blue);
				g.drawLine( M.x, 50, M.x, 150);
			case (3):
				circumscribe(g, B.x,B.y, 60, 120, 115);
				
			case (2):
				circumscribe(g, A.x,A.y, 60, 305 , 115);
			case (1):
				dot(g, A.x, A.y, "A");
				dot(g, B.x, B.y, "B");
				g.drawLine( A.x, A.y, B.x, B.y);
			default:
			}//of switch
	}// of bisectSegment
	public void bisectAngle (Graphics g) {
		switch (currentStep) {
			case (7):
				g.setColor(Color.blue);
				g.drawLine(A.x, A.y, 150, 77);
			case (6):
				circumscribe(g, 86,82, 30, 340, 25);
			case (5):
				dot(g, 86, 82,"");
			case (4):
				circumscribe(g, 89,100, 30, 10, 35);
			case (3):
				dot(g, 89, 100,"");
				
			case (2):
				circumscribe(g, A.x,A.y, 40, 350 , 45);
			case (1):
				dot(g, A.x, A.y, "A");
				dot(g, B.x, B.y, "B");
				dot(g, C.x, C.y, "C");
				g.drawLine( A.x, A.y, B.x, B.y);
				g.drawLine( A.x, A.y, C.x, C.y);
			default:
			}//of switch
	}// of bisectAngle
	public void copyAngle (Graphics g) {
		switch (currentStep) {
			case (9):
				g.setColor(Color.blue);
				g.drawLine(110, 100, 150, 81);
			case (8):
				circumscribe(g, 139, 100, 14, 75, 50);
			case (7):
				
				dot(g, 139,100,"");
			case (6):
				circumscribe(g, 79, 100, 14, 75, 50);
			case (5):
				
					dot(g, 79, 100,"");
			case (4):
				circumscribe(g, 110,100, 30, 350, 50);
			case (3):
			
				dot(g, 110, 100, "A'");
				
			case (2):
				circumscribe(g, A.x,A.y, 30, 350 , 50);
			case (1):
				dot(g, A.x, A.y, "A");
				//dot(g, B.x, B.y, "B");
				//dot(g, C.x, C.y, "C");
				g.drawLine( A.x, A.y, B.x, B.y);
				g.drawLine( A.x, A.y, C.x, C.y);
			default:
			}//of switch
	}// of copyAngle
	public void perp (Graphics g) {
		switch (currentStep) {
			case (9):
				
			case (8):
				g.setColor(Color.magenta);
				g.drawLine(M.x +8 , 100, M.x+8 , 92);
				g.drawLine(M.x  , 92, M.x+8 , 92);
				
			case (7):
				g.setColor(Color.blue);
				g.drawLine(M.x, 120, M.x, C.y);
				
			case (6):
				circumscribe(g, 121, 100, 30, 120, 50);
			case (5):
				
				dot(g, 121, 100,"");
			case (4):
				circumscribe(g, 79,100, 30, 25, 50);
			case (3):
			
				dot(g, 79, 100, "");
				
			case (2):
				circumscribe(g, M.x,C.y, 55, 230 , 80);
			case (1):
				dot(g, A.x, A.y, "A");
				dot(g, B.x, B.y, "B");
				dot(g, M.x, C.y, "P");
				g.drawLine( A.x, A.y, B.x, B.y);
				//g.drawLine( A.x, A.y, C.x, C.y);
			default:
			}//of switch
	}// of perp
	public void goldenRect (Graphics g) {
		switch (currentStep) {
		
			case (26):
				circumscribe(g, 132, 107, 8, 0, 90);
				//g.drawLine(132 , 107, 140 , 107);
			case (25):
				circumscribe(g, 132, 110, 11, 90, 90);
			case (24):	
				circumscribe(g, 140, 110, 20, 180, 90);
			case (23):
				circumscribe(g, 140, 99, 30, 270, 90);
			case (22):
				circumscribe(g, 120, 99, 49, 0, 90);
			case (21):
				circumscribe(g, 120, 130, 80, 90, 90);
			case (20):
				g.setColor(Color.magenta);
				g.drawLine(132 , 99, 132 , 110);
			case (19):
				g.setColor(Color.magenta);
				g.drawLine(120 , 110, 140 , 110);
			case (18):
				g.setColor(Color.magenta);
				g.drawLine(140 , 99, 140 , 130);
			case (17):
				g.setColor(Color.magenta);
				g.drawLine(120 , 99, 169 , 99);
			case (16):
				g.setColor(Color.magenta);
				g.drawLine(120 , 50, 120 , 130);
			case (15):
				g.setColor(Color.black);
				g.drawLine(169 , 50, 169 , 130);
				g.drawLine(169 , 50, 40 , 50);
				g.drawLine(40 , 50, 40 , 130);
				g.drawLine(40 , 130, 169 , 130);
				dot (g, 169 , 50, "E");
				dot(g, 169 , 130,"F");
				dot(g, 40, 50, "C");
				dot(g, 40, 130, "A");
				
				dot(g, 120, 50, "D");
				dot(g, 120, 130, "B");
				break;
				
			case (14):
				dot (g, 169 , 50, "E");
				dot(g, 169 , 130,"F");
				
			case (13):
				circumscribe(g, 80, 50, 90, 290, 80);
				circumscribe(g, 80, 130, 90, 355, 80);
			case (12):
				dot (g, 80, 50, "M");
				dot (g, 80, 130,"N");
				g.setColor(Color.orange);
				g.drawLine(80 , 135, 80 , 45);
			case (11):
				circumscribe(g, 40, 130, 60, 305, 115);
				circumscribe(g, 120, 130, 60, 120, 115);
			case (10):
				dot(g, 120, 50, "D");
				g.setColor(Color.blue);
				g.drawLine(40 , 50, 175 , 50);
			case (9):
				dot(g, 40, 50, "C");
				dot(g, 120, 130, "B");
				circumscribe(g, 40,50, 80, 348, 22);
				circumscribe(g, 120,130, 80, 75, 25);
			case (8):
				circumscribe(g, 40,130, 80, 75, 25);
				circumscribe(g, 40,130, 80, 348, 22);
			case (7):
				g.setColor(Color.blue);
				g.drawLine(40 , 130, 40 , 45);
			case (6):
				circumscribe(g, 59,130, 40, 100, 40);
			case (5):
				dot (g, 59,130, "");
			case (4):
				circumscribe(g, 21,130, 40, 40, 40);
			case (3):
				dot(g, 21,130,"");
			case (2):
				circumscribe(g, 40,130, 20, 348 , 22);
				circumscribe(g, 40,130, 20, 172 , 22);
			case (1):
				dot(g, 40, 130, "A");
				//dot(g, B.x, B.y, "B");
				g.drawLine( 20, 130,180, 130);
			default:
			}//of switch
	}// of goldenRect
	public void paint( Graphics g ) {
	  
		switch (daMenu.getSelectedIndex()) {
			case (0) : 
				bisectSegment(g);
				lastStep=5;
				A.move (50, 100);
	    		B.move(150,100);
	    		M.move( (int)((A.x+B.x)/2), (int)((A.y +B.y)/2) );
				break;
			case (1) : 
				bisectAngle(g);
				lastStep=7;
				break;
			case (2) : 
				copyAngle(g);
				lastStep=9;
				break;
			case (3) : 
				perp(g);
				lastStep=8;
				break;
			case (4) : 
				goldenRect(g);
				lastStep=26;
				break;
		}// of switch
			
		// update location of buttons in case window size changed
	   	go.move(size().width-buttonWidth-10,size().height-buttonHeight-10);
	   	back.move(10,size().height-buttonHeight-10);
		//draw colorful buttons and show which step we're on
		g.setColor(Color.red);
		g.drawString( "Step "+Integer.toString(currentStep), (int)(size().width / 2)-15, size().height-buttonHeight );
		g.setColor( buttonColor);
		g.fill3DRect(go.x, go.y, go.width, go.height,true);
		g.fill3DRect(back.x, back.y, back.width, back.height,true);
		g.setColor( Color.black);
		g.draw3DRect(go.x, go.y, go.width, go.height,true);
		g.draw3DRect(back.x, back.y, back.width, back.height,true);
		g.setColor(Color.blue);
		g.drawString("Back", back.x+(int)(.15*buttonWidth), back.y+(int)(.75*buttonHeight));
		g.drawString("Next", go.x+(int)(.15*buttonWidth),go.y+(int)(.75*buttonHeight));
	}
    /*
     * Mouse methods
     */
    public boolean mouseDown(java.awt.Event evt, int x, int y) {
        if ( go.inside(x,y) ) currentStep++;
        if ( back.inside(x,y) ) currentStep--;
        if (currentStep > lastStep) currentStep = lastStep;
        if (currentStep < 0) currentStep =0;
        repaint();
        return true;
    }
	
    public boolean mouseMove(java.awt.Event evt, int x, int y) {
        //repaint();
        return true;
    }
    public void mouseEnter() {
        //repaint();
    }

    public void mouseExit() {
       
       // repaint();
    }
    /*
     * Event Methods for pop up menu
     */
    public boolean action(Event e, Object arg) {
                   if (e.target instanceof Choice) {
                     currentStep=0;
                     repaint();
                      return true;
                  } else { return false;}
               
    }
    
    public boolean handleEvent(Event e) {
                  return super.handleEvent(e);
    }
}//of class declaration