//
/* ----------------------------------------------------------------------------------------------
    ch_code.java	キャラクタコード						ver. 1.00 (JDK 1.02)
    														ueyama@infonet.co.jp  2001.11.11
---------------------------------------------------------------------------------------------- */

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


public class ch_code extends Applet
{
	int			Xs =  12,	Ys =   6,	Sn = 0;						// 入力文字表示座標・文字数
	int			Sl[]={0, 0, 1, 2, 0, 3, 4};
	int			Sc[]=new int[26];
	//  一般キー  *************************************************************************
	int			Xt = 225,	Yt =  62;
	int			Xk =  70,	Yk = 313;								// キー表示オフセット
	int	Kx[]={ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,		// 0
			   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,		// 1
			  189, 36, 72,108,144,180,216,252,288,324,387,423,333,396,369,405,		// 2  SP 〜 /
			  360, 36, 72,108,144,180,216,252,288,324,423,387,333,396,369,405,		// 3  0  〜 ?
			  414, 63,225,153,135,126,171,207,243,306,279,315,351,297,261,342,		// 4  @  〜 O
			  378, 54,162, 99,198,270,189, 90,117,234, 81,450,468,459,432,441,		// 5  P  〜 _
			  414, 63,225,153,135,126,171,207,243,306,279,315,351,297,261,342,		// 6    〜 o
			  378, 54,162, 99,198,270,189, 90,117,234, 81,450,468,459,432,441,		// 7  p  〜 _
			   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,		// 8
			   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,		// 9
			   -1,369,450,459,333,405,360,108,126,144,180,216,252,288,324, 81,		// A  、 〜 ッ
			  468,108,126,144,180,216,198,207,243,423,225,117,135,162,378,153,		// B  ー 〜 ソ
			   54, 63, 81, 90, 99,270,306, 36,333,315,171,189, 72,432,396,279,		// C  タ 〜 マ
			  261,459,405,297,252,288,324,342,351,369,387,441,360,234,414,450,-1};	// D  ミ 〜 ゜
	int	Ky[]={ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			  144,  0,  0,  0,  0,  0,  0,  0,  0,  0, 72, 72,108,  0,108,108,
			    0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 72, 72,108,  0,108,108,
			   36, 72,108,108, 72, 36, 72, 72, 72, 36, 72, 72, 72,108,108, 36,
			   36, 36, 36, 72, 36, 36,108, 36,108, 36,108, 36,  0, 72,  0,108,
			   36, 72,108,108, 72, 36, 72, 72, 72, 36, 72, 72, 72,108,108, 36,
			   36, 36, 36, 72, 36, 36,108, 36,108, 36,108, 36,  0, 72,  0,108,
			   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			   -1,108, 36, 72,108,108,  0,  0, 36,  0,  0,  0,  0,  0,  0,108,
			    0,  0, 36,  0,  0,  0, 36, 72, 72, 72,108,108, 72, 36, 36,108,
			   36, 72,108, 36, 72, 36, 36,  0,108, 72, 72,108,  0,  0,  0, 72,
			  108, 72,108,108,  0,  0,  0, 36, 72,108, 72,108,  0, 36, 36, 36,-1};
	int	Kn[][]={{0x21,0x31,0xc7,0},{0x22,0x32,0xcc,0},{0x23,0x33,0xb1,0xa7},		// キー割り当て 1列目
				{0x24,0x34,0xb3,0xa9},{0x25,0x35,0xb4,0xaa},{0x26,0x36,0xb5,0xab},
				{0x27,0x37,0xd4,0xac},{0x28,0x38,0xd5,0xad},{0x29,0x39,0xd6,0xae},
				{0,0x30,0xdc,0xa6},{0x3d,0x2d,0xce,0},{0x7e,0x5e,0xcd,0},{0x7c,0x5c,0xb0,0},
				{0,0x51,0xc0,0},{0,0x57,0xc3,0},{0,0x45,0xb2,0xa8},{0,0x52,0xbd,0},	// 2列目
				{0,0x54,0xb6,0},{0,0x59,0xdd,0},{0,0x55,0xc5,0},{0,0x49,0xc6,0},
				{0,0x4f,0xd7,0},{0,0x50,0xbe,0},{0x60,0x40,0xde,0},{0x7b,0x5b,0xdf,0xa2},
				{0,0x41,0xc1,0},{0,0x53,0xc4,0},{0,0x44,0xbc,0},{0,0x46,0xca,0},	// 3列目
				{0,0x47,0xb7,0},{0,0x48,0xb8,0},{0,0x4a,0xcf,0},{0,0x4b,0xc9,0},
				{0,0x4c,0xd8,0},{0x2b,0x3b,0xda,0},{0x2a,0x3a,0xb9,0},{0x7d,0x5d,0xd1,0xa3},
				{0,0x5a,0xc2,0xaf},{0,0x58,0xbb,0},{0,0x43,0xbf,0},{0,0x56,0xcb,0},	// 4列目
				{0,0x42,0xba,0},{0,0x4e,0xd0,0},{0,0x4d,0xd3,0},{0x3c,0x2c,0xc8,0xa4},
				{0x3e,0x2e,0xd9,0xa1},{0x3f,0x2f,0xd2,0xa5},{0x5f,0,0xdb,0}};
	int			Kox[]={  7,  7, 18, 18};							// キー  文字表示オフセット X
	int			Koy[]={  4, 17, 17,  4};							// キー  文字表示オフセット Y
	//  特殊キー  0: esc  1: tab  2: caps lock  3: shift  4: sp  5: del  6: bs  7: enter  8: カナ/英数
	int			Kw0[]={ 36, 54, 63, 81, 139,  36,  54,  45,  63};	// 幅
	int			Kx0[]={  0,  0,  0,  0, 189, 504, 486, 495, 477};	// 表示オフセット X
	int			Ko[] ={352,369,385,432,   0, 452, 465, 476, 501};	// 文字オフセット X
	int			Kw[] ={ 17, 16, 47, 20,   0,  13,  11,  25,  41};	// 文字幅
	int			Kc[] ={0x1b,0x09,0, 0, 0x20,0x7f,0x08,0x0a, 0};		// コード
	//  特殊キー動作    0: caps lock  1: shift  2: カナ  3: 英数  *************************
	int			Xd[] ={ 12, 72, 484, 506};							// 表示 文字オフセット
	int			Xw[] ={ 47, 20,  15,  19};							// 表示 文字幅
	int			Xo[] ={385,432, 501, 523};
	boolean		Xl[] ={false, false, false, true};					// 状態
	boolean		Cap=false;											// 大文字

	AudioClip	Ac;													// 効果音
	Graphics	gr;
	int			Bgc;
	Image		Img;
	
	public void init()
	{
		String	Bg=getParameter("BgColor");
		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){};
		gr=getGraphics();
		Ac=getAudioClip(getCodeBase(), getParameter("audioclip"));
	}
	
	public boolean inside(int x, int a, int b)
	{
		return (x<=Math.max(a,b) && x>=Math.min(a,b)) ? true:false;
	}
	
	public boolean inner(int x, int x1, int x2, int y, int y1, int y2)
	{
		return (x<=Math.max(x1,x2) && x>=Math.min(x1,x2) && y<=Math.max(y1,y2) && y>=Math.min(y1,y2)) ? true:false;
	}

	public void dsp_img(int x, int y, int n, int m)
	{
		Graphics	gx=gr.create();
		int			w=0, h=0, ox=0, oy=0;
		switch(n)
		{
			case	 0:	w= 19; h=19; ox=  0; oy=135;	break;		// 文字  0x20 〜 0x3f
			case	 1:	w= 19; h=19; ox=  0; oy=154;	break;		// 文字  0x40 〜 0x5f
			case	 2:	w= 19; h=19; ox=  0; oy=173;	break;		// 文字  0x60 〜 0x7f
			case	 3:	w= 19; h=19; ox=  0; oy=192;	break;		// 文字  0xa0 〜 0xbf
			case	 4:	w= 19; h=19; ox=  0; oy=211;	break;		// 文字  0xc0 〜 0xdf
			case	 5:	w= 11; h=11; ox=352; oy= 47;	break;		// 16進数
			case	 6:	w= 48; h= 8; ox=532; oy=  0;	break;		// 上位4ビット
			case	 7:	w=  8; h=50; ox=590; oy=  0;	break;		// 下位4ビット
			case	 8:	w= 26; h=23; ox=512; oy= 10;	break;		// クリアボタン
			case	 9:	w= 36; h=36; ox=  0; oy=  0;	break;		// キートップ
		}
		gx.clipRect(x,y,w,h);
		gx.drawImage(Img,x-(ox+m*w),y-oy,this);
		gx.dispose();
	}
		
	public void dsp_str(int n, int c)								// 入力文字の表示
	{
		dsp_img(Xs+n*26, Ys, Sl[c/32], c%32);
		dsp_img(Xs+n*26, Ys+22, 5, c/16);
		dsp_img(Xs+n*26+10, Ys+22, 5, c%16);
	}
	
	public void dsp_key(int n, boolean f)							// キー & 文字表示 n: キーNo.
	{
		Graphics	gx;
		int			i,m;
		int			c = Kn[n][2];
		int			x = Kx[c],  y = Ky[c];

		dsp_img(Xk+x, Yk+y, 9, (f)?1:0);
		for(i=0;i<4;i++)
		{
			c = Kn[n][i];
			if(c>0)
			{
				gx=gr.create();
				m=inside(c, 0xa0, 0xe0)? 2: 0;
				gx.clipRect(Xk+Kox[i]+x, Yk+Koy[i]+y, 11, 11);
				gx.drawImage(Img, Xk+Kox[i]+x-(c%32)*11, Yk+Koy[i]+y-((c/32+m)*11+36), this);
				gx.dispose();
			}
		}
	}
	
	public void dsp_key0(int n, boolean f)							// 特殊キーの表示
	{
		Graphics	gx=gr.create();
		int			x = Xk + Kx0[n],   y = Yk + n%5 * 36,   ox = (f)? 36: 0;
		if(inside(n,1,3) || inside(n,6,8)) ox=(f)?153:72;
		else if(n==4) ox=(f)?373:234;
		if(n==0 || n==4 || n==5)									// キートップの表示
		{
			gx.clipRect(x, y, Kw0[n], 36);
			gx.drawImage(Img, x-ox, y, this);
		}
		else
		{
			gx.clipRect(x, y, Kw0[n]-18, 36);
			gx.drawImage(Img, x-ox, y, this);
			gx.dispose();
			ox=((f)?234:153)-Kw0[n]/2;
			gx=gr.create();
			gx.clipRect(x+Kw0[n]-Kw0[n]/2, y, Kw0[n]/2, 36);
			gx.drawImage(Img, x+Kw0[n]-Kw0[n]/2-ox, y, this);
		}
		gx.dispose();
		gx=gr.create();												// 文字の表示
		gx.clipRect(x+7, y+16, Kw[n], 11);
		gx.drawImage(Img, x-Ko[n]+7, y+16-36, this);
		gx.dispose();
	}

	public void dsp_exk(int n)										// 特殊キーの動作表示
	{
		Graphics	gx=gr.create();
		gx.clipRect(Xk+Xd[n],Yk+168,Xw[n],11);
		gx.drawImage(Img,Xk+Xd[n]-Xo[n],Yk+168-36,this);
		gx.dispose();
		gx=gr.create();
		gx.clipRect(Xk+Xd[n] +Xw[n]/2-5,Yk+156,10,10);
		gx.drawImage(Img,Xk+Xd[n]+Xw[n]/2-5 -(512+((Xl[n])?10:0)), Yk+156, this);
		gx.dispose();
	}
	
	public void dsp_chr(int n)										// キャラクタコード表  文字表示
	{
		Graphics	gx=gr.create();
		int			x = (n/32)*28 + ((n%32)/16 + 1)*14, y = (n%16 + 1)*14;
		gx.clipRect(Xt+x+2, Yt+y+2, 11, 11);
		gx.drawImage(Img, Xt+2+x-(n%32)*11, Yt+2+y-(36+(n/32)*11), this);
		gx.dispose();
	}

	public void dsp_cct(int c, boolean f)							// クリック時の文字コード表表示
	{
		int i, tx=(c/16+1)*14+Xt+1, ty=(c%16+1)*14+Yt+1;
		
		gr.setColor((f)?new Color(255,255,51):new Color(Bgc));
		gr.fillRect(tx, ty, 13, 13);
		dsp_chr(c);
		gr.fillRect(Xt+1, ty, 13, 13);
		gr.fillRect(tx, Yt+1, 13, 13);
		gr.setColor((f)?new Color(255,255,184):new Color(Bgc));
		for(i=0;i0) m=0;							// 英記号
						if(inside(Kn[n][1], 0x41,0x5b)) Cap=true;	// 大文字
					}
					else Cap=false;
					if(Xl[0]) Cap=true;
				}
				if(Xl[1] && Xl[2] && Kn[n][3]>0) m=3;				// カナ記号
				
				c=Kn[n][m];
				if(!Cap && inside(c, 0x41,0x5a)) c+=0x20;
				if(inside(c, 0x20, 0xdf))
				{
					dsp_key(n, f);
					dsp_cct(c, f);
				}
				if(f && Sn<26 && c>0)
				{
					gr.setColor(new Color(244,255,51));
					gr.fillRect(Xs+Sn*26-3, 3, 26, 38);
					dsp_str(Sn, c);
					Sc[Sn++]=c;
				}
				if(!f && Sn<27 && c>0)
				{
					gr.setColor(new Color(255,255,255));
					gr.fillRect(Xs+(Sn-1)*26-3, 3, 26, 38);
					dsp_str(Sn-1, Sc[Sn-1]);
				}
			}
			else
			{
				for(n=0; n<9; n++) if(inner(x,Kx0[n],Kx0[n]+Kw0[n], y,(n%5)*36,(n%5+1)*36)) break;
				if(n<9)												// 特殊キー
				{
					dsp_key0(n, f);
					c=Kc[n];
					if(c>0) dsp_cct(c, f);
					if(f)
					{
						switch(n)
						{
							case 2:	Xl[0]=!Xl[0]; if(Xl[0]) Cap=true;	break;	// caps lock
							case 3:	Xl[1]=!Xl[1];						break;	// shift
							case 8:	Xl[3]=!Xl[3]; Xl[2]=!Xl[3];			break;	// カナ/英数
						}
						if(c==0x20 && Sn<26)
						{
							dsp_str(Sn, c);
							Sc[Sn++]=c;
						}
						if(c==0x08 && Sn>0)
						{
							Sn--;
							gr.setColor(new Color(255,255,255));
							gr.fillRect(Xs+Sn*26-3, 1, 26, 42);
						}
					}
					for(i=0; i<4; i++) dsp_exk(i);
				}
			}
			if(f && Sn>=26) Ac.play();
		}
		m=(Sn==0)? 2:0;
		if(f && Sn>0 && inner(x,560,560+26, y,156,156+23))
		{
			gr.setColor(new Color(255,255,255));
			gr.fillRect(1, 1, 698, 42);
			Sn=0; m=1;
		}
		dsp_img(Xk+560, Yk+156, 8, m);
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		mouse_clik(x,y, true);
		return true;
	}
	
	public boolean mouseUp(Event e, int x, int y)
	{
		mouse_clik(x,y, false);
		return true;
	}
}
//
戻る