package pythagoras;

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

class arabCanvas extends PlotCanvas {
	double A, B, C;
	PlotNode cn;
	
	public arabCanvas(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);
		double W = 8, H = W*h/w;
		setCorners(-W, -H, W, H);
		// set the origin at the centre
		translate(-(A+B)/2.0, -(A+B)/2.0);
		cn = new PlotNode(this, new RealPoint(B, A+B));
	}
	
	PlotPath region;
	PlotPath csquare;
	PlotPath bsquare;
	PlotPath asquare;
	
	boolean ready = true;
	
	public void draw() {
		ready = false;
		csquare = new PlotPath(this);
		csquare.moveto(A, 0);
		csquare.lineto(A+B, A);
		csquare.lineto(B, (A+B));
		csquare.lineto(0, B);
		csquare.close();
		csquare.stroke(Color.black);
		
		bsquare = new PlotPath(this);
		bsquare.moveto(0, 0);
		bsquare.rlineto(B, 0);
		bsquare.rlineto(0, B);
		bsquare.rlineto(-B, 0);
		bsquare.close();
		bsquare.stroke(Color.black);
		
		asquare = new PlotPath(this);
		asquare.moveto(B, 0);
		asquare.rlineto(A, 0);
		asquare.rlineto(0, A);
		asquare.rlineto(-A, 0);
		asquare.close();
		asquare.stroke(Color.black);
		
		region = new PlotPath(this);
		region.moveto(0, B);
		region.rlineto(B, 0);
		region.rlineto(0, A);
		region.close();
		region.fill(Color.pink);
		region.stroke(Color.black);
		
		double DA = A*(B-A)/B;
		double DB = B-A;
		
		region = new PlotPath(this);
		region.moveto(A, 0);
		region.lineto(0, B);
		region.rlineto(B, 0);
		region.lineto(B, DA);
		region.close();
		region.fill(Color.pink);
		region.stroke(Color.black);
		
		region = new PlotPath(this);
		region.moveto(B, A+B);
		region.rlineto(0, -DB);
		region.rlineto(DA, 0);
		region.close();
		region.fill(Color.pink);
		region.stroke(Color.black);
		
		region = new PlotPath(this);
		region.moveto(B, DA);
		region.lineto(B, A);
		region.rlineto(A, 0);
		region.close();
		region.fill(Color.red);
		region.stroke(Color.black);
		
		region = new PlotPath(this);
		region.moveto(A+B, A);
		region.rlineto(-A, 0);
		region.rlineto(0, A);
		region.rlineto(DA, 0);
		region.close();
		region.fill(Color.red);
		region.stroke(Color.black);
		
		cn.draw();
		ready = false;
	}
	
	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);
		// System.out.println(P);
		if (cn.contains(P)) {
			// System.out.println("# " + P);
			active = true;
		}
		return true;
	}

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

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

public class arabApplet extends Applet {

	double a = 3/2.0, b = 4/2.0;
	arabCanvas baby;
	
	public void init() {
		setBackground(Color.white);
		// System.out.println("***");
		baby = new arabCanvas(this, bounds().width, bounds().height,
				 a, b);
		add(baby);
		show();
	}

}