//
/* ----------------------------------------------------------------------------------------------
    seven_seg_led.java	7セグメント LED						ver. 1.10 (JDK 1.02)
    															ueyama@infonet.co.jp  2000.04.07
    															update: 2009.11.13
---------------------------------------------------------------------------------------------- */
import java.applet.*;
import java.awt.*;
import java.util.*;


public class led7seg extends Applet implements Runnable
{
	//			Segment	a	  b		c	  d		e	  f		g			   LED 点灯パターンデータ
	boolean		L[][]={{true, true, true, true, true, true, false},		// 0
					   {false,true, true, false,false,false,false},		// 1               a
					   {true, true, false,true, true, false,true },		// 2           -------
					   {true, true, true, true, false,false,true },		// 3        f /      /  b
					   {false,true, true, false,false,true, true },		// 4         /  g   /
					   {true, false,true, true, false,true, true },		// 5        -------
					   {true, false,true, true, true, true, true },		// 6     e /      / c
					   {true, true, true, false,false,false,false},		// 7      /  d   /
					   {true, true, true, true, true, true, true },		// 8     -------
					   {true, true, true, true, false,true, true }};	// 9
	int			X[]={30,59,52,16,10,18,25};								// セグメント表示 X座標
	int			Y[]={10,16,60,95,60,16,53};								// セグメント表示 Y座標
	int			N;														// 表示数値
	int			T=0;													// タイマー用カウンタ
	Image		Img;
	Random		Rnd=new Random();
	Thread		th=null;
	
	public void start()
	{
		if(th==null)
		{
			th=new Thread(this);
			th.start();
		}
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	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"));
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		N=Math.abs(Rnd.nextInt())%10;
	}
	
	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)				// LED、数字の表示
	{
		Graphics gx=getGraphics();
		int w=0, h=0, ox=0, oy=0;
		switch(c)
		{
			case  0: w=41; h=11; ox= 0; oy= 0;	break;					// segment a
			case  1: w=16; h=40; ox=48; oy=33;	break;					// segment b
			case  2: w=15; h=41; ox=33; oy=33;	break;					// segment c
			case  3: w=41; h=11; ox= 0; oy=22;	break;					// segment d
			case  4: w=17; h=41; ox=16; oy=33;	break;					// segment e
			case  5: w=16; h=40; ox= 0; oy=33;	break;					// segment f
			case  6: w=36; h=11; ox= 0; oy=11;	break;					// segment g
			case  7: w= 5; h= 7; ox= 0; oy=74;	break;					// 数字
		}
		gx.clipRect(x,y,w,h);
		gx.drawImage(Img,x-(ox+w*m),y-(oy+h*n),this);
		gx.dispose();
	}
	
	
	public void paint(Graphics g)
	{
		int i;
		g.clearRect(0,0,size().width,size().height);
		g.setColor(new Color(0,0,0));
		g.fillRect(0,0,85,116);											// 7seg LED 本体
		for(i=0;i<7;i++) if(L[N][i]) dsp_g(X[i],Y[i],0,0,i);			// 7seg LED セグメントの表示
		for(i=0;i<10;i++) dsp_g(i*8+3,120,i,(i==N)?1:0,7);				// 数字の表示
	}
	
	public void run()
	{
		while(true)
		{
			try
			{
				Thread.sleep(100);
			}
			catch (InterruptedException e){}
			T++;
			if(T%25==0)
			{
				N=Math.abs(Rnd.nextInt())%10;							// 表示数値を乱数で決める
				repaint();
			}
		}
	}
	
	public boolean mouseDown(Event e, int x, int y)						// マウスをクリック
	{
		if(y>116 && inside(x,2,81))										// 数字をクリック
		{
			N=(x-2)/8; T=0; repaint();									// クリックされた数字を表示
		}
		return true;
	}
	
	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
}
//
戻る