
/*
	TryAngle will draw an interactive triangle of various colors.
	
	version 1.0 - 16 Nov 1998 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 TryAngle extends java.applet.Applet {

    int activeDot=0;
    int rx[];
	int ry[];
	String colorItem;
	String vertex="ABC";
	float dist[];
	int ang[];
	Choice daColor = new Choice();
	
	boolean moving=false;
	boolean showLabels=false;
	int fudge=7; //fudge factor for sensitivity
	
    public void init() {
   		rx = new int[3];
		ry = new int[3];
		dist = new float[3];
		ang = new int[3];
	
		
		daColor.addItem("black");
		daColor.addItem("red");
		daColor.addItem("magenta");
		daColor.addItem("green");
		daColor.addItem("white");
		daColor.addItem("cyan");
		daColor.addItem("coral");
		daColor.addItem("black with Labels");
		
		
		add(daColor);
	

        setBackground(new Color(0xF0F080));
       
        	rx[0] = 100;rx[1] = 200;rx[2] = 200;
        	ry[0] = 200; ry[1] = 200;ry[2] = 100;
       
    }

    public void paint(Graphics g) {
     
    	switch (daColor.getSelectedIndex()) {
    		case(0):
    			g.setColor(Color.black);
    			showLabels = false;
    			break;
    		case(1):
    			g.setColor(Color.red);
    			showLabels = false;
    			break;
        	case(2):
    			g.setColor(Color.magenta);
    			showLabels = false;
    			break;
    		case(3):
    			g.setColor(Color.green);
    			showLabels = false;
    			break;
    		case(4):
    			g.setColor(Color.white);
    			showLabels = false;
    			break;
    		case(5):
    			g.setColor(Color.cyan);
    			showLabels = false;
    			break;
    		case(6):
    			g.setColor(new Color(0xF08080));
    			showLabels = false;
    			break;
    		
    		default:
    			g.setColor(Color.black);
    			showLabels = true;
    			
    	}
    	
        for (int i=0; i<2; i++){ 
        	g.drawLine(rx[i],ry[i], rx[i+1],ry[i+1]);
        }
        g.drawLine(rx[2],ry[2], rx[0],ry[0]);
        //vertices always red
        g.setColor(Color.red);
       
        for (int i=0; i<3; i++){
        	g.fillOval(rx[i]-3,ry[i]-3, 7,7);
        }

       if (moving) {
       		g.setColor(Color.blue);
       		int x = rx[activeDot]/10-15;
       		int y = 15 - ry[activeDot]/10;
        	g.drawString("Click again to place vertex "+vertex.charAt(activeDot)+" at ("+x+","+y+")", 10,290);
        	g.fillOval(rx[activeDot]-3,ry[activeDot]-3, 7,7);
        	} 
        else {
        	g.setColor(Color.black);
        	g.drawString("Click on a vertex to move it", 10,290);
        	for (int i=0; i<3; i++){ 
        		g.setColor(Color.black);
        		g.drawString(Float.toString(dist[i]), Math.round( (rx[i]+rx[(i+1)%3])/2) , Math.round( (ry[i]+ry[(i+1)%3])/2));
        		g.setColor(Color.gray);
        		g.drawString(Integer.toString(ang[i]), rx[i]-14, ry[i]+10);
        		g.setColor(Color.black);
        		if (showLabels) {
        			g.drawString(vertex.substring(i,i+1), rx[i],ry[i]-3);
        		}
        		
       		}
        }
       
    }
	public boolean action(Event e, Object arg) {
                   if (e.target instanceof Choice) {
                     repaint();
                      return true;
                  } else { return false;}
               
    }
    
    public boolean handleEvent(Event e) {
                  return super.handleEvent(e);
    }
             
    /*
     * Mouse methods
     */
    public boolean mouseDown(java.awt.Event evt, int x, int y) {
      
       
        if (!moving){ /* Find which dot to relocate */
          for ( int i=0; i<3; i++){
            if ( ( x > rx[i]-fudge && x < rx[i]+fudge)  &&
                 ( y > ry[i]-fudge && y < ry[i]+fudge) ) {
                 activeDot = i ;
                 moving=true; 
            }
          }
        } else {  /* Compute distances now that a place was picked */
  			moving=false;
  			for (int i=0; i< 3;i++){
  			    double d = Math.round(Math.sqrt( Math.pow((rx[i]-rx[(i+1)%3]),2) + Math.pow((ry[i]-ry[(i+1)%3]),2)));
  				dist[i]=(float)(d/10);
  			}
  			for (int i=0; i<3; i++){
  				/* use the law of cosines to find each angle */
  				double sideC2 = Math.pow(dist[(i+2)%3],2);
				double sideA2 = Math.pow(dist[(i)%3],2);
				double sideB2 = Math.pow(dist[(i+1)%3],2);
				double sideA  = dist[i];
				double sideC  = dist[(i+2)%3];
			
				double numerator=  sideA2 + sideC2 - sideB2 ;
				double denominator = 2 * sideA *sideC;
				double a = Math.round(  (180/Math.PI)*(Math.acos(numerator/denominator)) );
				ang[i] = (int)( a );
  				
  				
  			}
		  }

        repaint();
        return true;
    }
	
  	
  	
    public boolean mouseMove(java.awt.Event evt, int x, int y) {
         x = (x/10)-15;
         y = 15-(y/10);
         getAppletContext().showStatus("Location of mouse: (" + x + ", " + y + ")");
        
         if (moving){
         	requestFocus();
         	rx[ activeDot] = (x+15)*10;
        	ry[ activeDot] = (15-y)*10;  
        	repaint();
         }
        return true;
    }

    public void mouseEnter() {
        repaint();
    }

    public void mouseExit() {
       
        repaint();
    }

    /**
     * Focus methods
     */
    public void keyDown(int key) {
    }
    
    
}//end applet

