//
/* ----------------------------------------------------------------------------------------------
    relay.java	リレー										ver. 1.00 (JDK 1.02)
    														ueyama@infonet.co.jp  2007.07.30
---------------------------------------------------------------------------------------------- */

import java.applet.Applet;
import java.awt.*;


public class relay extends Applet implements Runnable
{
	int			Sw=0;												// リレーコイル  0: Off  1: On
	long		Lstc=0;												// 最後にクリックした時間
	int			Dclk=0;												// ダブルクリック回数
	int			Wait=2000;
	boolean		Run=true;
	Thread		th=null;
	Image		Img;
	
	public void init()
	{
		MediaTracker mt=new MediaTracker(this);
		Img=getImage(getCodeBase(), getParameter("figure"));
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
	}
	
	public void start()
	{
		if(th==null)
		{
			th=new Thread(this);
			th.start();
		}
	}
	
	public void dsp_g()
	{
			Graphics gx=getGraphics();
			gx.drawImage(Img,0-size().width*Sw,0,this);
			gx.dispose();
	}
	
	public void run()
	{
		while(th!=null)
		{
			try
			{
				th.sleep(Wait);
			}
			catch (InterruptedException e){};
			if(Run) Sw=(Sw==0)?1:0;
			dsp_g();
		}
	}
	
	public void paint(Graphics g)
	{
		g.clearRect(0, 0, size().width, size().height);
		g.drawImage(Img, 0, 0, this);
		dsp_g();
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		if((e.when-Lstc > 180) || (Dclk==0)) Dclk=1;
		else Dclk++;
		Lstc=e.when;
		if(Dclk==1)														// シングルクリック
		{
			Run=false; Wait=50;
			Sw=(Sw==0)?1:0;
			dsp_g();
		}
		else															// ダブルクリック
		{
			Run=true; Wait=2000;
		}
		return true;
	}
	
	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
}
//
戻る