package shears;

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

import psplot.*;
import real.*;

abstract class ShearCanvas extends PlotCanvas {

	double b, ht, s;
	PlotNode cn;
	double leftx, rightx;
	
	public ShearCanvas(Applet e, int w, int h,
			double B, double Ht, double S) {
		super(e, w, h);
		b = B; ht = Ht; s = S;
		double W = 5, H = W*(double)h/(double) w;;
		setCorners(-W, -H, W, H);
		translate(-b/2.0, -ht/2.0);
		leftx = -W + b/2.0;
		rightx = W + b/2.0;
		RealPoint P = new RealPoint(s, ht);
		cn = new PlotNode(this, P);
		repaint();
	}
	
	// draw nodes 1 & 2

	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;
	}
	
	boolean ready = true;

	public boolean mouseDrag(Event evt, int x, int y) {
		RealPoint P = toRealPoint(x, y);
		if (active & ready) {
			P.y = ht;
			double delta = 0.10;
			if (P.x < leftx + delta) P.x = leftx + delta;
			else if (P.x > rightx - delta) P.x = rightx - delta;
			cn.locate(P);
			s = P.x;
			repaint();
		}
		return true;
	}

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

