/*
 *	
 *  Filename: ComputeFigure.java
 *  Java Applet Demo
 *  Author: Achim E Karger
 *  Course: CSCI 111 Java I
 *  Assignment: Lab 4
 *  Date:   February 20, 2008
 *  compiler JCreator LE
 *  Client web application: lab4.html
 *  
 *  this Applet displays a circle or rectangle graphics in a 50x60 pixel graphics area 
 *  and some geometrical information about the figure
 *  the lab consists of modifying the provided program by 
 *  additionally  displaying geometrical information about the figure via drawstring() 
 *  the size, position and values are to be  formatted and displayed in such a way as to not interfere
 *  with the drawn figure  
 *
 */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;

//***********************************************************************
/**
 * The TheMouseHandler class handles mouse events
 * and sets the Applet data member x, y to the mouse location
 * and whether or not a button is pressed
 *
 * @author  Achim E Karger
 * @version 1.0 02/20/08
 * @see     java.lang.System
 */

class TheMouseHandler extends MouseAdapter implements MouseMotionListener
{
	ComputeFigure theCurrentApplet;

	public  TheMouseHandler(ComputeFigure x)
	{
		theCurrentApplet=x;
	}

	public void mouseEntered(MouseEvent e)
	{
		theCurrentApplet.x=0;
		theCurrentApplet.y=0;
		theCurrentApplet.width=0;
		theCurrentApplet.height=0;
		theCurrentApplet.repaint();	
		
	}
	
	public void mouseExited(MouseEvent e)
	{
		theCurrentApplet.x=0;
		theCurrentApplet.y=0;
		theCurrentApplet.width=0;
		theCurrentApplet.height=0;	
		theCurrentApplet.repaint();
	}

	public void mouseDragged(MouseEvent e)
	{
		theCurrentApplet.width=e.getX()-theCurrentApplet.x;
		theCurrentApplet.height=e.getY()-theCurrentApplet.y;
		
		theCurrentApplet.repaint();	
	}
	
	public void mouseMoved(MouseEvent e)
	{	
		//empty on purpose
	}

	public void mousePressed(MouseEvent e)
	{	
		theCurrentApplet.x=e.getX();
		theCurrentApplet.y=e.getY();	

		// to get x and y coordinates of a mouse event e one can also do
		// e.getPoint().x and e.getPoint().y
		// see the API java.awt.event.MouseEvent
	}
}

//*****************************************************************
/**
 * The TheButtonHandler class handles which button has been pushed
 * and sets the Applet data member 'figure' to the name of the pressed 
 * button 
 *
 * @author  Achim E Karger
 * @version 1.0 02/20/08
 * @see     java.lang.System
 */
class TheButtonHandler implements ActionListener
{
	
	ComputeFigure currentApplet;

	public void actionPerformed(ActionEvent e)
	{
		
		System.out.println(e.getActionCommand());
		currentApplet.figure=e.getActionCommand();
		
	}
	
	public TheButtonHandler(ComputeFigure x)
	{
		currentApplet=x;
	}

}

//*****************************************************************
/**
 * The ComputeFigure class applet allows the user to 
 * draw a figure (circle or rectangle) and obtain size and position in pixel
 * uses the java.lang.Math class to compute the area, perimeter of the circle
 *
 * @author  Achim E Karger
 * @version 1.0 02/20/08
 * @see     java.lang.System
 */
public class ComputeFigure extends Applet
{
	//gui elements
	Button circleButton;
	Button rectangleButton;
	TheMouseHandler mouseManager;
	TheButtonHandler buttonManager;
	Panel holdbuttons;

	//instance variables
	int x,y,height,width;
	String figure;

	public void init()
	{
		//instantiate classes 
		mouseManager=new TheMouseHandler(this);
		buttonManager=new TheButtonHandler(this);
		holdbuttons=new Panel(); 
		circleButton=new Button("Circle");
		rectangleButton=new Button("Rectangle");
		
		//set initial default values
		figure="Circle";
		x=50;
		y=50;
		width=50;
		height=50;
		
 		addMouseMotionListener(mouseManager);
		addMouseListener(mouseManager);
		
		circleButton.addActionListener(buttonManager);
		circleButton.setBackground(Color.red);
		
		rectangleButton.addActionListener(buttonManager);
		rectangleButton.setBackground(Color.green);
		
		setLayout(new BorderLayout());
		holdbuttons.add(circleButton);
		holdbuttons.add(rectangleButton);
		add("North",holdbuttons);
		setBackground(Color.pink);
		
		
	}

	public void paint(Graphics g)
	{
      int centerPoint_x, centerPoint_y, topLeft_x, topLeft_y;
      int lineHeight = 15;
      double area, perimeter;
	  String result;

      // Round the output to two decimal places
      DecimalFormat fmt = new DecimalFormat ("0.##");

		if(figure.equals("Circle") )
		{
			g.drawOval(x,y,width,height);

			//computations for circle (assume radius = width/2)
		    area = Math.PI * Math.pow(width/2, 2);
      		perimeter = 2 * Math.PI * width/2;
			//end circle computations
		}
		else
		{
			g.drawRect(x,y,width,height);

			//computations specific for rectangle
			area = (double)width*height;
      		perimeter = (double)(2*width + 2*height);
			//end Rectangle computations
		}

			// coordinates common to all figures
            centerPoint_x = x + width/2; 
			centerPoint_y = y + height/2;
			topLeft_x = x;
			topLeft_y = y;

			// compose and display result strings, offset by lineHeight
		    result = "Area: " + fmt.format(area)+", " + "Perimeter: "+ fmt.format(perimeter);
			g.drawString(result, x, y + height +lineHeight );
			
			result ="Center point (x,y): ("+centerPoint_x+","+centerPoint_y+")";
			g.drawString( result, x , y + height + 2*lineHeight);
			
			result ="Top left corner (x,y): ("+topLeft_x+","+topLeft_y+")";
			g.drawString(result, x , y + height + 3*lineHeight);

	}

}

