//************************************************************************
//Program name:
//Problem Desrciption:
//
//Name:
//Date:
//class:
//************************************************************************

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

//***********************************************************************
// class that handles mouse events
//
// keeping track of the mouse location and whether 
// or not a button is pressed
//***********************************************************************
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
	}
}

//******************************************************************
// class that handles which button has been pushed
//
//
//******************************************************************
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;
	}

}

//*****************************************************************
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;
	double radius, area, perimeter, center1, center2, center, tlcorner;

	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=500;
		y=500;
		width=500;
		height=500;
		
 		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)
	{
	
		if(figure.equals("Circle") )
		{
			g.drawOval(x,y,width,height);

			//put computations here for circle
			radius = .5 * width;
			area = Math.PI * Math.pow(radius, 2.0);
			perimeter = Math.PI * 2.0 * radius;
			center1 = ( x + ( width / 2.0));
			center2 = ( y + ( height / 2.0));
			g.drawString("Perimeter is " + perimeter, 5, 468);
			g.drawString("Area is " + area, 5, 478);
			g.drawString("Center is (" + center1 + " ," + center2 + ")", 5, 488);
			g.drawString("Top Left Corner is (" + x + " ," + y + ")", 5, 498);
			//end circle computations

		}
		else
		{
			g.drawRect(x,y,width,height);

			//put computations here for rectangle
			area = height * width;
			perimeter = (2.0 * height) + (2.0 * width);
			center1 = x + (.5 * width);
			center2 = y + (.5 * height);
			g.drawString("Perimeter is " + perimeter, 5, 468);
			g.drawString("Area is " + area, 5, 478);
			g.drawString("Center is (" + center1 + " ," + center2 + ")", 5, 488);
			g.drawString("Top Left Corner is (" + x + " ," + y + ")", 5, 498);
			//end Rectangle computations
		}
	}

}

