import primitives.machines.*;
import primitives.spaces.*;
import primitives.frames.*;
import primitives.geomtry.*;
import java.awt.*;
import java.awt.event.*;
public class NarmsSpace extends Frames implements MouseMotionListener,MachineListener{
	NarmsMachine machine;
	NarmsPatch space;
	boolean move;
	public void init(){
		super.init();
		frames[0].drawArea.addMouseMotionListener(this);
	}
	public void start(){
			run = false;
		machine = new NarmsMachine(frames[0].drawArea.getSize(),getParam("arms",5),1.3);
		space = new NarmsPatch(this,frames[1].drawArea.getSize(),machine);
		frames[1].drawArea.addMouseMotionListener(space);
		for(int i=0;i<2;i++) machine.switchBend(i*2);
		frames[0].drawArea.setCurrentObject(this);
		frames[1].drawArea.setCurrentObject(space);
		tx = new Coordinate();
	}
	public void stop(){
		super.start();
		frames[1].drawArea.removeMouseMotionListener(space);
		machine = null;
		space = null;
		tx = null;
	}
	Coordinate tx;
	public void mouseMoved(MouseEvent m){
		if(move){
			Point t = m.getPoint();
			try{
				machine.moveCenter(t.x-machine.center.x,t.y-machine.center.y);
			}catch(MachineException e){
				tx.move(t.x,t.y);
				machine.moveToMaxCoordinate(tx);
			}finally{
				space.changeState();
				frames[0].drawArea.repaint();//paint(space.g[0]); 
				frames[1].drawArea.repaint();//(space.g[1]); 
			}
		}
		move=!move;
	}
	 public void mouseDragged(MouseEvent e){
	 mouseMoved(e);
	 }
	 
	 public void redraw(Graphics g){
		 if (machine!=null){
			 machine.redraw(g);
			g.setColor(Color.black);
			g.drawChars(NarmsPatch.letters,0,1,machine.anchors[0].x-2,machine.anchors[0].y-5);
			g.drawChars(NarmsPatch.letters,1,1,machine.anchors[1].x+10,machine.anchors[1].y-1);
			g.drawChars(NarmsPatch.letters,2,1,machine.anchors[2].x,machine.anchors[2].y-5);
			g.drawChars(NarmsPatch.letters,3,1,machine.anchors[3].x+10,machine.anchors[3].y+10);
		 }
	 }

}
