/* applet appears in proof.htm*/

import java.awt.*;
import primitives.frames.Frames;
import primitives.machines.*;
import java.awt.*;
import primitives.geomtry.*; 
public class TwoArmsSpaceDemo extends Frames implements MachineUpdate{
  	public Image sphere;
	TwoArmsMachine machine = null;
//	Line line	;
	Dimension d;
	int r;
	int k;
	double ik;
	double dk;
	final static double dalpha = 0.05;
	double ir;
	int sign;
	int l;
/*	public void destroy(){
		machine = null;
		sphere.flush();
		sphere = null;
		d = null;
		tracker = null;
		super.destroy();
	}*/
	MediaTracker tracker ;
/*	public void init(){
		super.init();
  	}	*/
 	public void start(){
			run = true;
		sign = 1;
		tracker = new MediaTracker(frames[0].drawArea);
    	sphere = getImage(getCodeBase(),getParameter("image"));
		tracker.addImage(sphere,0);
		machine = new TwoArmsMachine(frames[0].drawArea.getSize(),1.3,1);
		d = frames[1].drawArea.getSize();
		frames[1].drawArea.setCurrentObject(this);
	   	frames[0].drawArea.setCurrentObject(machine);
		r = Math.min(d.width,d.height)/2-5	;
		dk = r*dalpha/Math.abs(machine.angles[1]-machine.angles[0]);
		ik = -r;
		ir = r;
		try {    //Start downloading the images. Wait until they're loaded.
			tracker.waitForID(0);
		} catch (InterruptedException e) {
			throw new RuntimeException(e.getMessage());
		}
		bg = frames[1].drawArea.getBackground();
		super.start();
		//startThread();
	}
	public void stop(){
		super.stop();
		machine = null;
		sphere.flush();
		sphere = null;
		d = null;
		tracker = null;
		bg = null;
	}

	public void changeFrames(){
		try{
			for(int i=0;i<2;i++)
				frames[i].drawArea.repaint();
			ik = ik+dk*sign;
			k = (int)ik;
			ir = Math.rint(Math.sqrt((r-Math.abs(k))*(2*r-(r-Math.abs(k)))));
			l = (int)((r-k)*ir*10/(r*r))+3;
			machine.rotateJoint(1,sign*dalpha);
	   	}catch(MachineException e){
			 sign = sign*(-1);
		}
	}

	public void redraw(Graphics g){
		g.setColor(Color.red);
		g.drawArc(d.width/2-(int)(ir),d.height/2+k-l,2*(int)ir,l*2,0,-180);
	}
	Color bg ;
	public void update(Graphics g){
		if (!tracker.checkID(0,true)) {
		if(d!=null){	g.clearRect(0, 0, d.width, d.height);
			g.drawString("Please wait...", 0, d.height/2);
		}
		}else
	//		if (tracker.isErrorID(0)) throw new RuntimeException("gif not loaded");
		if(d!=null)g.drawImage(sphere,d.width/2-(r+5),d.height/2-(r+5),r*2+10,r*2+10,
			bg,frames[1].drawArea); 
	} 
		
}		
/*class Line implements MachineUpdate{
	TwoArmsSpaceDemo controller;
	Point state ;
	TwoArmsMachine machine;
	int up,down;
	Dimension d;
	public Line(TwoArmsSpaceDemo controller){
		this.controller = controller;
		machine = controller.machine;
		state = new Point(0,0);
		int up =(int)(Geomtry.getPointByVector(machine.anchors[1].toCoordinate(),machine.barLength,machine.angles[0])).y;
		int down = (int)(Geomtry.getPointByVector(machine.anchors[1].toCoordinate(),machine.barLength,machine.angles[1])).y;
		d = controller.frames[1].drawArea.getSize();
	}
	public void update(Graphics g){		
		g.clearRect(0,0,d.width,d.height);
		g.setColor(Color.black);
		g.drawLine(d.width/2,0,d.width/2,d.height);
	}
	public void redraw(Graphics g){
			state.move(d.width/2,machine.joints[1].y);
			g.setColor(Color.magenta);
			Geomtry.drawJoint(g,state);
	}
	public void finalize() throws Throwable{
		controller = null;
		state = null;
		d = null;
		machine = null;
	}
}				 */

