package primitives.machines;
import java.awt.*;
import primitives.geomtry.*;
/**
* Used by <a href="SmallMachineProof.html>SmallMachineProof</a> applet draw a small machine 
*with 2 copies of its right joint.
*@see SmallMachine
*@author Dori Eldar
*/
public class SmallMachineDemo extends SmallMachine{
	/**
	*Stores the 2 locations of the right joint.
	*/
	private Coordinate[] danotherJoints;
	/**
	*integeral representation of the 2 locations of the right joint.
	*/
	public ExtPoint[]    anotherJoint;
	/**
	*
	*@param d the size of the rectangle to draw the machine in.
	*/
	public SmallMachineDemo(Dimension d){
		super(d);
		anotherJoint = new ExtPoint[2];
		danotherJoints = new Coordinate[2];
		danotherJoints = Geomtry.getTriPoint(anchors[1].toCoordinate(),barLength,dJoints[0],barLength);
		updatePoints(danotherJoints,anotherJoint);
	}
	Coordinate t3= new Coordinate();
	/**
	*rotates the right joint with respect to the left joint.
	*@param alpha the angle in radians to rotate.
	*@exception MachineException if rotation is not physicaly possible.
	*@see SmallMachine#rotateJoint
	*/
	public void rotateJoint(double alpha)throws MachineException{
		super.rotateJoint(alpha);
		t3.move(anchors[1].x,anchors[1].y);
		danotherJoints = Geomtry.getTriPoint(t3,barLength,dJoints[0],barLength);
	}
	/**
	*streches the right joint.
	*@see SmallMachine#reachEnd
	*/
	public void reachEnd(){
		   super.reachEnd();
		   danotherJoints[0]=dJoints[1];
		   danotherJoints[1]=dJoints[1];
	}
	/**
	*Redraws the machine.
	*@param g the graphic context to draw to.
	*/
	public void redraw(Graphics g){
		super.redraw(g);
	   	g.setColor(Color.red);
		drawLine(g,anchors[1],joints[0]);
		g.setColor(Color.blue);
		for(int i=0;i<2;i++){
			anotherJoint[i].move((int)Math.rint(danotherJoints[i].x),(int)Math.rint(danotherJoints[i].y));
			drawLine(g,anchors[1],anotherJoint[i]);
			drawLine(g,joints[0],anotherJoint[i]);
		}
		if (anotherJoint[0].y<anotherJoint[1].y) g.setColor(Color.magenta);
		else g.setColor(Color.yellow);
		Geomtry.drawJoint(g,anotherJoint[0]);
		if (anotherJoint[0].y>anotherJoint[1].y) g.setColor(Color.magenta);
		else g.setColor(Color.yellow);
		Geomtry.drawJoint(g,anotherJoint[1]);
	}
	/**
	*Sets object fields to null
	*@exception java.lang.Throwable .
	*/
	public void finalize() throws Throwable{
		danotherJoints = null;
		anotherJoint = null;
		super.finalize();
	}
} 
