//
/* ----------------------------------------------------------------------------------------------
    bin2hex.java	16進数への変換								ver. 2.00 (JDK 1.02)
    															ueyama@infonet.co.jp  2000.03.20
    															update: 2007.06.03
---------------------------------------------------------------------------------------------- */

import java.applet.*;
import java.awt.*;
import java.util.*;


public class bin2hex extends Applet implements Runnable
{
	int			Bx=0, By=138, Qx=24, Qy=13;
	int			Qn,Qq;												// 出題、出題数
	int			T,Te;
	AudioClip	Pinpon, Boouu;
	int			Bgc;
	Color		Bs[]=new Color[5];
	Image		Img;
	Graphics	gr;
	Random		Rnd=new Random();
	Thread		th=null;
	
	public void start()
	{
		if(th==null)
		{
			th=new Thread(this);
			th.start();
		}
	}
	
	public void init()
	{
		String bg=getParameter("BgColor");
		Bgc=Integer.valueOf(bg,16).intValue();
		setBackground(new Color(Bgc));
		Pinpon=getAudioClip(getCodeBase(), "pinpon.au");
		Pinpon.play(); Pinpon.stop();
		Boouu=getAudioClip(getCodeBase(), "boouu.au");
		Boouu.play(); Boouu.stop();
		MediaTracker mt=new MediaTracker(this);
		Img=getImage(getCodeBase(), getParameter("figure"));
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		gr=getGraphics();
		Bs[0]=new Color(255,255,255);
		Bs[1]=new Color(232,232,232);
		Bs[2]=new Color(102,102,102);
		Bs[3]=new Color( 51, 51, 51);
		Bs[4]=new Color(180,180,204);
		Qn=Math.abs(Rnd.nextInt())%16;
		Qq=0; T=0; Te=0;
	}
	
	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_b(int x, int y, int n, int c)					// 図、文字の表示
	{
		Graphics	gx=gr.create();
		int			w=12, h=16, ox=632, oy=0;
		switch(n)
		{
			case  0: w=36; h=37; ox=  0; oy=  0;	break;			// 白16進数
			case  1: w=36; h=37; ox=  0; oy= 37;	break;			// 緑16進数
			case  2: w=37; h=57; ox=576; oy=  0;	break;			// 数字 0 or 1
			case  3: w=18; h=16; ox=  0; oy= 74;	break;			// 白数字 ボタン用
			case  4: w=11; h=16; ox=576; oy= 57;	break;			// 灰数字
			case  5: w=26; h=24; ox=650; oy=  0;	break;			// RUN ボタン
			case  6: w=33; h=12; ox=576; oy= 73;	break;			// "Start"
			case  7: w=32; h=12; ox=609; oy= 73;	break;			// "Time"
			case  8: w=29; h=12; ox=642; oy= 73;	break;			// "Sec."
		}
		gx.clipRect(x,y,w,h);
		gx.clearRect(x,y,w,h);
		gx.drawImage(Img,x-(ox+c*w),y-oy,this);
		gx.dispose();
	}
	
	public void dsp_q()												// 問題の表示
	{
		int i,j=8,m=0;
		for(i=0; i<4; i++,j/=2)
		{
			m=((Qn&j)==0)? 0:1;
			dsp_b(Qx+i*50,Qy,2,m);
		}
	}
	
	public void button(int n, boolean s)							// 16進キーの表示
	{
		int x=(n%4)*60+Bx, y=((15-n)/4)*60+By;
		gr.setColor(Bs[(s)?3:0]);
		gr.drawLine(x,y,x+54,y); gr.drawLine(x,y,x,y+54);
		gr.setColor(Bs[(s)?2:1]);
		gr.drawLine(x+1,y+1,x+53,y+1); gr.drawLine(x+1,y+1,x+1,y+53);
		gr.setColor(Bs[(s)?1:2]);
		gr.drawLine(x+2,y+53,x+53,y+53); gr.drawLine(x+53,y+2,x+53,y+53);
		gr.setColor(Bs[(s)?0:3]);
		gr.drawLine(x+1,y+54,x+54,y+54); gr.drawLine(x+54,y+1,x+54,y+54);
		gr.setColor(Bs[4]);
		gr.fillRect(x+2,y+2,51,51);
		dsp_b(x+9,y+9,(s)?1:0,n);
	}
	
	public void dsp_t()												// 時間の表示
	{
		int m, x=18;
		if(inside(Te,1,9999))
		{
			dsp_b(x,97,7,0); x+=38;
			m=Te/1000; if(m>0) {dsp_b(x,93,4,m); x+=13;}
			m=Te/100; m%=10; dsp_b(x,93,4,m); x+=13;
			m=Te/10;  m%=10; dsp_b(x,93,4,m); x+=12;
			dsp_b(x,93,4,10); x+=8;
			m=Te%10; dsp_b(x,93,4,m); x+=18;
			dsp_b(x,97,8,0);
		}
	}
	
	public void paint(Graphics g)
	{
		int i;
		g.clearRect(0,0,size().width,size().height);
		g.setColor(new Color(204,204,204));
		g.fillRoundRect(4,0,227,83,20,20);							// 出題 '枠'
		g.setColor(new Color(Bgc));
		g.fillRoundRect(8,4,219,75,16,16);
		for(i=0; i<16; i++) button(i,false);
		dsp_b(201,91,5,(Qq==0)?0:1);
		if(Qq>0)
		{
			dsp_b(206,95,3,Qq-1);
			dsp_q();
		}
		else dsp_b(161,97,6,0);
		dsp_t();
	}
	
	public void run()
	{
		while(true)
		{
			try
			{
				Thread.sleep(100);
			}
			catch (InterruptedException e){}
			T++;
			if(Te>0 && T-Te==5) Pinpon.play();
		}
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		int mx=(x-Bx)/60, nx=(x-Bx)%60, my=(y-By)/60, ny=(y-By)%60, n=(3-my)*4+mx;
		if(inside(x,201,227) && inside(y,91,115) && Qq==0)			// Start ボタン
		{
			Qq=1;
			dsp_b(201,91,5,1);
			Qn=Math.abs(Rnd.nextInt())%16;
			dsp_q();
			gr.clearRect(16,93,180,17);								// 時間表示の消去
			T=0; Te=0;
		}
		if(inside(nx,0,55) && inside(ny,0,55) && Qq>0)				// 16進キーをクリック
		{
			button(n,true);
			if(n==Qn)												// 正解
			{
				Pinpon.play();
				Qq++;
				dsp_b(206,95,3,Qq-1);								// 出題 No. の表示
				if(Qq>10)											// 終了
				{
					Qq=0; Te=T;
					dsp_b(201,91,5,0);
					gr.clearRect(24,13,187,57);
					dsp_b(161,97,6,0);
				}
				else
				{
					while(Qn==n) Qn=Math.abs(Rnd.nextInt())%16;		// 問題(16進数)を乱数で作成
					dsp_q();										// 問題の表示
				}
			}
			else Boouu.play();
		}
		dsp_t();
		return true;
	}
	
	public boolean mouseUp(Event e, int x, int y)
	{
		int mx=(x-Bx)/60, nx=(x-Bx)%60, my=(y-By)/60, ny=(y-By)%60, n=(3-my)*4+mx;
		if(inside(x,201,227) && inside(y,91,115) && Qq==1)	dsp_b(206,95,3,0);
		if(inside(nx,0,55) && inside(ny,0,55))				button(n,false);
		return true;
	}
	
	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
}
//
戻る