import primitives.machines.*;
import primitives.spaces.*;
import primitives.frames.*;
import java.awt.event.*;
import java.awt.*;
import primitives.geomtry.Coordinate;
public class FourArmsDemo extends Frames implements MouseMotionListener{
	NarmsMachine machine;
	ArmsConSpace space;
	int clicked = 0;
	boolean move;
	public void init(){
		super.init();
		frames[0].drawArea.addMouseMotionListener(this);
	}
	public void start(){
			run = false;
	  	machine = new NarmsMachine(frames[0].drawArea.getSize(),4,1.3);
		try{
		space = new ArmsConSpace(frames[0].drawArea.getSize(),machine,this);
		}catch(MachineException e){}
		frames[0].drawArea.setCurrentObject(machine);
		frames[1].drawArea.setCurrentObject(space);
		frames[1].drawArea.addMouseMotionListener(space);
		machine.switchBend(2);
		space.changeState();
		tx = new Coordinate();
	}
	public void stop(){
		super.stop();
		frames[1].drawArea.removeMouseMotionListener(space);
		machine = null;
		space = null;
		tx = null;
	}
	Coordinate tx;
	public void mouseMoved(MouseEvent e){
		if (move){
			Point t = e.getPoint();
			try{
				machine.moveCenter(t.x-machine.center.x,t.y-machine.center.y);
			}catch(MachineException ex){
				tx.move(t.x,t.y);
				machine.moveToMaxCoordinate(tx);
			}
			space.changeState();
			frames[0].drawArea.repaint();
			frames[1].drawArea.repaint();
		}
		move = !move;
	}
	public void mouseDragged(MouseEvent e){}
	public void mouseClicked(MouseEvent e){
		double d = getSize().width;
		int j=0;
		while((d>=100)&&(j<4))
			d = Math.abs(machine.joints[j].x-e.getPoint().x)+Math.abs(machine.joints[j++].y-e.getPoint().y);
		if (d<100){
			machine.switchBend(j-1);
			space.changeState();
			frames[0].drawArea.repaint();
			frames[1].drawArea.repaint();
		}
	}

}
