import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;


public class TriangleCalculatorPanel extends JPanel implements MouseListener,
															   MouseMotionListener
{
	private Point p1;
	private Point p2;
	private Point p3;
	
	private int pointCount;
	
	private Point mouseLocation;
	
	public TriangleCalculatorPanel()
	{
		this.setBackground( Color.BLACK);
		
		p1 = new Point();
		p2 = new Point();
		p3 = new Point();
		pointCount = 0;
		mouseLocation = new Point();

		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		
	}
	
	
	public void paintComponent( Graphics g )
	{
		super.paintComponent( g );
		g.setFont(new Font("ariel", Font.BOLD, 16));
		g.setColor(Color.green);
		
		g.drawString("(" + mouseLocation.x + " ," + mouseLocation.y + ")", 600, 20);
		
		if(pointCount == 0)
			g.drawString("Please click on point " + (pointCount + 1), 10, 20);
		
		else if(pointCount == 1)
		{
			g.drawString("Please click on point " + (pointCount + 1), 10, 20);
			g.drawLine(p1.x, p1.y, p1.x, p1.y);	// Draw point at location p1
		}
		else if(pointCount == 2)
		{
			g.drawString("Please click on point " + (pointCount + 1), 10, 20);
			g.drawLine(p2.x, p2.y, p2.x, p2.y);	// Draw point at location p2
			
			g.drawLine(p1.x, p1.y, p2.x, p2.y);	// Draw line from point p1 to point p2
		}
		else if(pointCount == 3)
		{
			//g.drawLine(p3.x, p3.y, p3.x, p3.y);	// Draw point at location p3
			g.drawLine(p2.x, p2.y, p3.x, p3.y);	// Draw line from point p2 to p3
			g.drawLine(p1.x, p1.y, p3.x, p3.y);	// Draw line from point p1 to p3
			g.drawLine(p1.x, p1.y, p2.x, p2.y);	// Draw line from point p1 to p2
			
			Triangle triangle = new Triangle(p1, p2, p3);
			g.drawString("Area: " + triangle.getArea(), 10, 20);
			g.drawString("Perimeter: " + triangle.getPerimeter(), 10, 40);
			
		}
		
	}
	
	// Methods for MouseLister
	public void mouseClicked(MouseEvent evt)
	{
		if(pointCount == 0)
		{
			p1 = evt.getPoint();
			pointCount++;
		}
		else if (pointCount == 1)
		{
			p2 = evt.getPoint();
			pointCount++;
		}
		else if (pointCount == 2)
		{
			p3 = evt.getPoint();
			pointCount++;
		}

		repaint();
	}
	
	public void mouseEntered(MouseEvent evt) { /* no action */ }
	public void mouseExited(MouseEvent evt) { /* no action */ }
	public void mousePressed(MouseEvent evt) { /* no action */ }
	public void mouseReleased(MouseEvent evt) { /* no action */ }
	
	// Methods for MouseMotionListener
	public void mouseDragged(MouseEvent e)		{ /* no action */	}
	
	public void mouseMoved(MouseEvent e)
	{
		mouseLocation = e.getPoint();
		repaint();
	}

}
