//
/* ----------------------------------------------------------------------------------------------
    focusing.java		CD のフォーカシング制御				ver. 1.10 (JDK 1.02)
    														ueyama@infonet.co.jp  2001.05.05
    														update: 2007.08.23
---------------------------------------------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;


public class focusing extends Applet implements Runnable
{
	int			Fy=184, Fh=165, Fl=203;									// センサの高さ、上限、下限
	int			Mf=-1;													// ボタンのクリック 0: △  1: ─  2: ▽
	Image		Img;
	Thread		th;
	
	public void init()
	{
		String bg=getParameter("bgcolor");								// 背景色の設定
		int bgc=Integer.valueOf(bg,16).intValue();
		setBackground(new Color(bgc)); 
		MediaTracker mt=new MediaTracker(this);
		Img=getImage(getCodeBase(), getParameter("figure"));			// GIF 画像の取得
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
	}
	
	public void start()
	{
		th=new Thread(this);
		th.start();
	}
	
	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
	
	public boolean inside(int x, int a, int b)
	{
		return (x<=Math.max(a,b) && x>=Math.min(a,b)) ? true:false;
	}
	
	public void dsp_g(int x, int y, int m, int n, int c)				// 画像の表示
	{
		int ox=0,oy=0,w=0,h=0;
		switch(c)
		{
			case  0: w= 26; h= 23; ox=  0; oy=361; break;				// ボタン
			case  1: w=163; h=  1; ox= 10; oy=360; break;				// 直線
			case  2: w= 59; h= 59; ox= 62; oy=290; break;				// センサ背景
		}
		if(c==1 && n>0) oy=y;											// 直線消去処理のため
		Graphics gx=getGraphics();
		gx.clipRect(x,y,w,h);
		if(c<2) gx.clearRect(x,y,w,h);
		gx.drawImage(Img,x-(ox+w*m),y-(oy+h*n),this);
		gx.dispose();
	}

	public void dsp_btn()												// ボタンの表示
	{
		dsp_g(194,147,(Fy==Fh)?2:0,0,0);								// △ボタンの表示
		dsp_g(194,173,0,1,0);											// ─ボタンの表示
		dsp_g(194,199,(Fy==Fl)?2:0,2,0);								// ▽ボタンの表示
	}

	public void dsp_ls()												// センサ部の表示
	{
		int w=Fy-Fh+13, h=Fl-Fy+13;
		int x=(81-w)/2+50, y=(81-h)/2+280;
		Graphics gx=getGraphics();
		gx.setColor(new Color(255,255,204));							// センサ背景色
		gx.fillRect(62,290,59,59);										// 中心部をクリア
		gx.setColor(new Color(255,153,153));
		gx.fillOval(x,y,w,h);											// レーザビームを描く
		dsp_g( 62,290,0,0,2);											// センサ斜線を表示
	}
	
	public void paint(Graphics g)
	{
		g.drawImage(Img,0,0,this);										// 背景の表示
		dsp_ls();														// 光センサの表示
		dsp_btn();														// ボタンの表示
		dsp_g( 10, Fy,0,0,1);											// 直線の表示
	}

	public void run()
	{
		while(true)
		{
			try
			{
				Thread.sleep(64);
			}
			catch (InterruptedException e){};
			if(inside(Mf,0,2))											// ボタンがクリックされていたら、
			{
				dsp_g( 10, Fy,0,1,1);									// 直線の消去
				if(Mf==0 && Fy>Fh) Fy--;								// 直線の位置データの更新
				if(Mf==1) Fy=184;
				if(Mf==2 && Fy194 && inside(y,147,222))									// ボタンをクリック
		{
			Mf=(y-147)/26;												// 0: △  1: ─  2: ▽
			if(!(Mf==0 && Fy==Fh || Mf==2 && Fy==Fl)) dsp_g(194,147+Mf*26,1,Mf,0);	// ボタンを緑色で表示
		}
		return true;
	}
	
	public boolean mouseUp(Event e, int x, int y)						// クリックが終わったときの処理
	{
		if(x>194 && inside(y,147,222))									// ボタンがクリックされていた
		{
			Mf=-1;
			dsp_btn();													// ボタンの表示
		}
		return true;
	}
}
//
戻る