//
/* ----------------------------------------------------------------------------------------------
    shiftjis.java	シフトJIS漢字コード						ver. 2.00 (JDK 1.02)
    														ueyama@infonet.co.jp  1999.04.04
    														update: 2007.06.11
---------------------------------------------------------------------------------------------- */

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

public class shiftjis extends Applet implements Runnable
{
	int			X0=32, Y0=27;										// 漢字コード表原点
	int			Jx0=98,Jy0=93, Jx1=287, Jy1=282;					// 漢字コード 2121〜8080 の位置
	int			Sx0=161, Sy0=286, Sx1=539, Sy1=348;					// シフトJIS漢字コード 8140〜A0FC の位置
	int			Sy2=476, Sy3=508;									// シフトJIS漢字コード E0〜F0 の位置
	int			J8x=568, J8y=259;									// JIS コード表原点
	//			Btn:  <|   △   |>  ▽   |>   <<   >>
	int			Bx[]={651, 680, 680, 680, 709, 580, 609};			// ボタン表示 X座標
	int			By[]={491, 465, 491, 517, 491, 491, 491};			// ボタン表示 Y座標

	String		Jis="3021", Sjis="889f", Jis0="0000", Sjis0="0000";
	TextField	Code=new TextField(Jis, 8);
	boolean		Cj=true;											// true: JIS,  false: S-JIS
	Graphics	gr;
	boolean		Run=false;
	int			Wait=256;
	int			Bgc;
	Image		Img, Tbl;
	Thread		th=null;

	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"));		// Applet用 GIF画像ファイル
		mt.addImage(Img, 0);
		Tbl=getImage(getCodeBase(),getParameter("letter"));			// JIS X 0208 文字コード表
		mt.addImage(Tbl, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		gr=getGraphics();
		setLayout(null);
		add(Code);
		Code.reshape(680,117,44,22);								// TextField の表示
	}
	
	public void start()
	{
		if(th==null)
		{
			th=new Thread(this);
			th.start();
		}
	}
	
	public boolean inside(int x, int a, int b)
	{
		return (x<=Math.max(a,b) && x>=Math.min(a,b)) ? true:false;
	}
	
	public void sjis2jis()											// Shift JIS -> JIS 変換
	{
		Integer		c=Integer.valueOf(Sjis, 16);
		int	c1=c.intValue()/256, c2=c.intValue()%256;
		if(c1<0xa0) c1-=0x70;
		else if(c1<0xf0)c1-=0xb0;
		else
		{
			Jis=Integer.toString(c1*256+c2,16);
			return;
		}
		if(c2>=0x80) c2--;c1*=2;
		if(c2>=0x9e) c2-=0x5e; else c1--;
		c2-=0x1f;
		Jis=Integer.toString(c1*256+c2,16);
	}
	
	public void jis2sjis()											// JIS -> Shift JIS 変換
	{
		Integer		c=Integer.valueOf(Jis, 16);
		int	c1=c.intValue()/256, c2=c.intValue()%256;
		if((c1 & 1)==1)
		{
			c2+=0x1f;
			if(c2>=0x7f) c2++;
		}
		else c2+=0x7e;
		c1=(c1-0x20-1)/2+0x81;
		if(c1>=0xa0) c1+=0x40;
		Sjis=Integer.toString(c1*256+c2,16);
	}
	
	public void inc_dec(boolean low, boolean inc)					// Code の Inc, Dec
	{
		String		u=Jis.substring(0,2), l=Jis.substring(2);
		Integer		i=Integer.valueOf((low)?u:l, 16), k;
		int			c=i.intValue(), d=(inc)?1:-1, p;
		
		if(inc && c<0x7e || !inc && c>0x21)	c+=d;
		else
		{
			c=(inc)? 0x21:0x7e;
			if(!low)
			{
				k=Integer.valueOf(Jis.substring(0,2), 16);
				p=k.intValue(); p++;
				u=Integer.toString(p,16);
			}
		}
		if(low)	Jis=Integer.toString(c,16)+l;
		else	Jis=u+Integer.toString(c,16);
		jis2sjis();
	}
	
	public void dsp_g(int x, int y, int f, int v, int c)			// 図や文字を描く
	{
		Graphics	gx=gr.create();
		int			w=0, h=0, ox=0, oy=0;
		switch(c)
		{
			case	 0:	w=545; h=540; ox=  0; oy=  0;	break;		// 漢字コード表
			case	 1:	w=205; h=218; ox=545; oy=  0;	break;		// コード表 (JIS X 0201)
			case	 2:	w= 13; h= 14; ox=545; oy=218;	break;		// 16進数(文字コード用)
			case	 3:	w= 26; h= 23; ox=545; oy=274;	break;		// ボタン(移動用)
			case	 4:	w= 26; h= 23; ox=623; oy=274;	break;		// ボタン(RUN, STOP用)
			case	 5:	w=109; h= 11; ox=545; oy=366;	break;		// 文字(JIS漢字コード、他)
			case	 6:	w= 43; h= 11; ox=545; oy=410;	break;		// 文字(JIS, S-JIS)
		}
		gx.clipRect(x,y,w,h);
		gx.clearRect(x,y,w,h);
		gx.drawImage(Img, x-(ox+f*w), y-(oy+v*h), this);
		gx.dispose();
	}

	public void dsp_i()		
	{
		int			i, m, x, y;
		Integer		c=Integer.valueOf(Jis, 16);
		Graphics	gx=gr.create();
		x=c.intValue()%256-33; y=c.intValue()/256-33;
		gr.clearRect(680,198,16,16);
		gx.clipRect(680,198,16,16);
		gx.drawImage(Tbl,680-x*16,198-y*16,this);					// 文字の表示
		gx.dispose();
		del_dot();
		dsp_jdot(Jis);												// 漢字コード表の位置表示
		dsp_jdot(Sjis);
		dsp_r(Jis);													// JISコード表に色をつける
		dsp_r(Sjis);
		gr.clearRect(680,151,52,38);
		for(i=0; i<4; i++)											// JIS コードの表示
		{
			m=Integer.parseInt(Jis.substring(i,i+1),16);
			dsp_g(680+i*14,151,m,i/2,2);
		}
		for(i=0; i<4; i++)											// Shift JIS コードの表示
		{
			m=Integer.parseInt(Sjis.substring(i,i+1),16);
			dsp_g(680+i*14,175,m,i/2+2,2);
		}
		Jis0=Jis; Sjis0=Sjis;
	}

	public void dsp_r(String s)										// JISコード表に色をつける
	{
		int		x,y, u,l;
		Integer	c=Integer.valueOf(s, 16);
		u=(c.intValue()/256); l=c.intValue()%256;
		x=u%16*12+J8x; y=u/16*12+J8y;
		gr.setXORMode(new Color(Bgc));
		if(u<128)	gr.setColor(new Color(51,102,255));
		else		gr.setColor(new Color(255,51,153));
		gr.fillRect(x,y,11,11);
		x=l%16*12+J8x; y=l/16*12+J8y;
		if(u<128)	gr.setColor(new Color(204,204,255));
		else		gr.setColor(new Color(255,204,230));
		gr.fillRect(x,y,11,11);
		gr.setPaintMode();
	}
	
	public void dsp_jdot(String s)									// 漢字の位置表示
	{
		Integer	i,j;
		i=Integer.valueOf(s.substring(0,2),16); j=Integer.valueOf(s.substring(2),16);
		gr.setXORMode(new Color(Bgc));
		gr.setColor((i.intValue()<128)? new Color(51,51,255): new Color(255,51,51));
		gr.drawOval(j.intValue()*2+X0-4, i.intValue()*2+Y0-4, 10, 10);
		gr.fillRect(j.intValue()*2+X0, i.intValue()*2+Y0, 3, 3);
		gr.setPaintMode();
	}
	
	public void del_dot()		
	{
		if(Jis0!="0000")
		{
			dsp_jdot(Jis0);											// 漢字コード表の位置表示
			dsp_jdot(Sjis0);
			dsp_r(Jis0);											// JISコード表に色をつける
			dsp_r(Sjis0);
		}
	}

	public void paint(Graphics g)
	{
		g.clearRect(0,0,size().width,size().height);
		dsp_g(0,0,0,0,0);											// 漢字コード表
		dsp_g(555,233,0,0,1);										// コード表 (JIS X 0201)
		dsp_g(555,123,0,0,5);										// "文字コード"
		dsp_g(555,152,0,1,5);										// "JIS 漢字コード"
		dsp_g(555,176,0,2,5);										// "シフトJIS 漢字コード"
		dsp_g(555,200,0,3,5);										// "文字データ"
		dsp_g(Bx[0],By[0],0,1,3);									// ボタン  <|  の表示
		dsp_g(Bx[1],By[1],0,3,3);									// ボタン  △  の表示
		dsp_g(Bx[2],By[2],0,(Run)?3:2,4);							// ボタン  |> の表示
		dsp_g(Bx[3],By[3],0,2,3);									// ボタン  ▽  の表示
		dsp_g(Bx[4],By[4],0,0,3);									// ボタン  |>  の表示
		dsp_g(Bx[5],By[5],0,1,4);									// ボタン  <<  の表示
		dsp_g(Bx[6],By[6],0,0,4);									// ボタン  >>  の表示
		del_dot();
		dsp_i();
	}
	
	public void run()
	{
		while(th!=null)
		{
			if(Run)
			{
				inc_dec(false, true);
				dsp_i();
			}
			try
			{
				th.sleep(Wait);
			}
			catch (InterruptedException e){};
		}
	}
	
	public boolean mouseDown(Event e, int mx, int my)
	{
		int  i,m, x=(mx-X0+1)/2, y=(my-Y0+1)/2;
		boolean	f=false;
		
		if(inside(mx,Jx0,Jx1) && inside(my,Jy0,Jy1))				// JIS コード
		{
			f=true;
			Jis=Integer.toString(y*256+x,16);
			jis2sjis();
		}
		if(inside(mx,Sx0,Sx1) && (inside(my,Sy0,Sy1) || inside(my,Sy2,Sy3)))	// Shift JIS コード
		{
			f=true;
			Sjis=Integer.toString(y*256+x,16);
			sjis2jis();
		}
		if(inside(mx,566,749) && my>=465)							// ボタンをクリック
		{
			f=true;
			for(i=0; i<7; i++) if(inside(mx,Bx[i],Bx[i]+26) && inside(my,By[i],By[i]+23)) break;
			switch(i)
			{
				case 0:	dsp_g(Bx[0],By[0],1,1,3); inc_dec(false, false); f=true;	break;	// 左へ
				case 1:	dsp_g(Bx[1],By[1],1,3,3); inc_dec(true, false); f=true;		break;	// 上へ
				case 2:	dsp_g(Bx[2],By[2],1,(Run)?3:2,4); Run=!Run;					break;	// RUN
				case 3:	dsp_g(Bx[3],By[3],1,2,3); inc_dec(true, true); f=true;		break;	// 下へ
				case 4:	dsp_g(Bx[4],By[4],1,0,3); inc_dec(false, true); f=true; 	break;	// 右へ
				case 5:	dsp_g(Bx[5],By[5],1,1,4); Wait*=2;							break;	// 減速
				case 6:	if(Wait>64){dsp_g(Bx[6],By[6],1,0,4); if(Wait>64) Wait/=2;}	break;	// 加速
			}
		}
		if(f) dsp_i();
		return true;
	}
	
	public boolean mouseUp(Event e, int mx, int my)
	{
		int		i,m, x=(mx-X0+1)/2, y=(my-Y0+1)/2;
		if(inside(mx,566,749) && my>=465)							// ボタンをクリック
		{
			for(i=0; i<7; i++) if(inside(mx,Bx[i],Bx[i]+26) && inside(my,By[i],By[i]+23)) break;
			switch(i)
			{
				case 0:	dsp_g(Bx[0],By[0],0,1,3); break;			// 左へ
				case 1:	dsp_g(Bx[1],By[1],0,3,3); break;			// 上へ
				case 2:	dsp_g(Bx[2],By[2],0,(Run)?3:2,4); break;	// RUN
				case 3:	dsp_g(Bx[3],By[3],0,2,3); break;			// 下へ
				case 4:	dsp_g(Bx[4],By[4],0,0,3); break;			// 右へ
				case 5:	dsp_g(Bx[5],By[5],0,1,4); dsp_g(Bx[6],By[6],0,0,4); break;	// 減速
				case 6:	dsp_g(Bx[6],By[6],(Wait>64)?0:2,0,4); break;	// 加速
			}
		}
		return true;
	}

	public boolean action(Event e, Object o)
	{
		if(e.target instanceof TextField)
		{
			String c=Code.getText();
			Integer	u=Integer.valueOf(c.substring(0,2),16);
			Integer	l=Integer.valueOf(c.substring(2),16);
			Integer	n=Integer.valueOf(c, 16);
			int	i=u.intValue(), j=l.intValue(), m=n.intValue();
			if(inside(m, 8481, 32382))								// 0x2121 〜 0x7e7e (JIS)
			{
				if(inside(i, 33,126) && inside(j, 33,126))
				{
					Cj=true;
					Jis=c;
					jis2sjis();
				}
			}
			if(inside(m, 33088, 61436))								// 0x8140 〜 0xeffc (S-JIS)
			{
				if((inside(i,129,159) || inside(i,224,239)) && inside(j, 64,252) && j!=127)
				{
					Cj=false;
					Sjis=c;
					sjis2jis();
				}
			}
			dsp_i();
		}
		return true;
	}

	public void stop()
	{
		if(th!=null)
		{
			th.stop();
			th=null;
		}
	}
}
//
戻る