package pythagoras;

import java.applet.*;
import java.awt.*;
import psplot.*;
import real.*;

class ThreeTrianglesAppletCanvas extends PlotCanvas {
	double A, B, C;
	double dx, dy;
	PlotNode cn;
	
	public ThreeTrianglesAppletCanvas(Applet a, int w, int h, double x, double y) {
		super(a, w, h);
		A = x; B = y;
		C = Math.sqrt(A*A + B*B);
		initd();
		
		double W = 12, H = W*h/w;
		setCorners(-W, -H, W, H);
		// set the origin at the centre
		translate(-A+0.5, -A/2.0);
		cn = new PlotNode(this, new RealPoint(0, A));
		Font SymbolFont = new Font("Helvetica", Font.ITALIC, 16);
		setFont(SymbolFont);
		repaint();
	}
	
	void initd() {
		double d = A*B/C;
		dx = A*d/C;
		dy = B*d/C;
	}
	
	PlotPath triangle;
	PlotPath trianglea;
	PlotPath triangleb;
	PlotPath trianglec;
	PlotPath drop;
	PlotPath asquare;
	PlotPath bsquare;
	PlotPath csquare;
	
	public void draw() {
	
		asquare = new PlotPath(this);
		asquare.moveto(0, 0);
		asquare.lineto(0, A);
		asquare.lineto(-A, A);
		asquare.lineto(-A, 0);
		asquare.close();
		asquare.stroke(Color.black);
		
		bsquare = new PlotPath(this);
		bsquare.moveto(0, 0);
		bsquare.lineto(B, 0);
		bsquare.lineto(B, -B);
		bsquare.lineto(0, -B);
		bsquare.close();
		bsquare.stroke(Color.black);
		
		csquare = new PlotPath(this);
		csquare.moveto(0, A);
		csquare.lineto(B, 0);
		csquare.lineto(B+A, B);
		csquare.lineto(A, A+B);
		csquare.close();
		csquare.stroke(Color.black);
		
		triangle = new PlotPath(this);
		triangle.moveto(0, 0);
		triangle.lineto(B, 0);
		triangle.lineto(0, A);
		triangle.close();
		triangle.fill(Color.red);
		triangle.stroke(Color.black);
		
		drop = new PlotPath(this);
		drop.moveto(0, 0);
		drop.lineto(dx, dy);
		drop.stroke(Color.black);
		
		trianglea = new PlotPath(this);
		trianglea.moveto(0, 0);
		trianglea.lineto(-dx, dy);
		trianglea.lineto(0, A);
		trianglea.close();
		trianglea.fill(Color.pink);
		trianglea.stroke(Color.black);
		
		triangleb = new PlotPath(this);
		triangleb.moveto(0, 0);
		triangleb.lineto(dx, -dy);
		triangleb.lineto(B, 0);
		triangleb.close();
		triangleb.fill(Color.pink);
		triangleb.stroke(Color.black);
		
		trianglec = new PlotPath(this);
		trianglec.moveto(0, A);
		trianglec.lineto(dx+dx, dy+dy);
		trianglec.lineto(B, 0);
		trianglec.close();
		trianglec.fill(Color.pink);
		trianglec.stroke(Color.black);
		
		cn.draw();
	}
	
	boolean active = false;

	public boolean mouseDown(Event evt, int x, int y) {
		// if (x, y) inside a node set mouseState = MouseActive
		RealPoint P = toRealPoint(x, y);
		if (cn.contains(P)) {
			active = true;
		}
		return true;
	}

	public boolean mouseDrag(Event evt, int x, int y) {
		if (active) {
			RealPoint q = toRealPoint(x, y);
			q.x = 0;
			if (q.y > C) q.y = C;
			if (q.y < 0.0) q.y = 0.0;
			cn.locate(q);
			A = q.y;
			B = Math.sqrt(C*C - A*A);
			initd();
			repaint();
		}
		return true;
	}

	public boolean mouseUp(Event evt, int x, int y) {
		// set nodes inactive
		active = false;
		return true;
	}
}

public class ThreeTrianglesApplet extends Applet {

	double a = 3, b = 4;
	ThreeTrianglesAppletCanvas baby;
	
	public void init() {
		setLayout(new BorderLayout(0, 0));
		setBackground(Color.white);
		baby = new ThreeTrianglesAppletCanvas(this, bounds().width, bounds().height, a, b);
		add(baby);
		show();
	}

}