//
/* ----------------------------------------------------------------------------------------------
    charcode.java	キャラクターコード						ver. 1.20  2000.02.10 (JDK 1.02)
    														ueyama@infonet.co.jp  1998.09.26
---------------------------------------------------------------------------------------------- */

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


public class charcode extends Applet
{
	int			Ofsx= 38,	Ofsy= 38;								// コード表の左上隅の座標
	int			Maxx=391,	Maxy=391;								// コード表の右下隅の座標
	int			Minx= 16,	Miny= 16;								// コード表の見出しの欄の左上隅の座標
	int			Tp=22;												// コード表の1マスのサイズ
	int			Mx  = 99,	My  = 99;								// マウスクリック値 (0 〜 15)
	int			F_no=0;												// 0: JIS 8 単位コード  1: EBCDIC コード
	int			Ctl[][]={{3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,131},					// JIS 8
						 {7,7,15,3,15,15,15,15,2,3,7,15,11,15,7,15}};			// EBCDIC
	int			Blk[][]={{50944,49920,49920,49920,49920,49920,49920,49920,
						  49920,49920,49920,49920,49920,49920,49920,49920},		// JIS 8
						 {32648,18616, 2288, 2300, 2288, 2288, 2288, 2288,
						   2301, 2300,65288,65280,65284,65280,65288,65280}};	// EBCDIC
	Checkbox	cb_jis8, cb_ebcdic;
	Button		bt_clear;
	Image		fig[]= new Image[2];
	Graphics	gr;
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void init()
	{
		String	bg=getParameter("BgColor");
		int		bc=Integer.valueOf(bg,16).intValue();
		setBackground(new Color(bc));
		gr=getGraphics();
		
		MediaTracker mt=new MediaTracker(this);
		for(int i=0;i<2;i++)
		{
			fig[i]=getImage(getCodeBase(), getParameter("gif"+i));
			mt.addImage(fig[i], 0);
		}
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		CheckboxGroup cg=new CheckboxGroup();
		Panel p=new Panel();
		p.add(cb_jis8	=new Checkbox("JIS 8", cg,true));
		p.add(cb_ebcdic	=new Checkbox("EBCDIC",cg,false));
		p.add(bt_clear	=new Button("CLEAR"));
		setLayout(new BorderLayout());
		add("South" ,p);
	}
	
	public boolean inside(int n, int a, int b)
	{
		return (n<=Math.max(a,b) && n>=Math.min(a,b)) ? true:false;
	}
	
	public boolean is_control(int c, int x, int y)					// 制御コードなら true を返す
	{
		int m=1 << x;
		return ((Ctl[c][y] & m)==0)? false:true;
	}
	
	public boolean is_blank(int c, int x, int y)					// 未定義コードなら true を返す
	{
		int m=1 << x;
		return ((Blk[c][y] & m)==0)? false:true;
	}
	
	public void dsp_code(int x, int y)								// コードの表示
	{
		int		i;
		String	bx,by, hx,hy;

		bx=Integer.toString(x,2);
		by=Integer.toString(y,2);
		for(i=bx.length(); i<4; i++) bx="0"+bx;
		for(i=by.length(); i<4; i++) by="0"+by;
		hx=Integer.toString(x,16);	hx=hx.toUpperCase();
		hy=Integer.toString(y,16);	hy=hy.toUpperCase();
		if(Mx==99) gr.setColor(new Color(153,153,153));				// Clear 時は灰色
		else if(is_control(F_no, x, y)) gr.setColor(new Color(51,102,153));	// 制御コードは青く
		else if(is_blank(F_no, x, y))	gr.setColor(new Color(153,0,102));	// 未定義は赤く表示
		else gr.setColor(new Color(102,102,51));
		gr.clearRect(Ofsx,Maxy+1,Maxx-Ofsx,32);
		gr.setFont(new Font("Helvetica",Font.BOLD,22));
		gr.drawString("CODE = "+hx+hy+"   ( = "+bx+" "+by+" )",Ofsx,Maxy+28);
	}
	
	public void paint(Graphics g)
	{
		g.clearRect(0,0,size().width,size().height);
		g.setColor(new Color(247,247,208));							// 表の見出しの色を変える
		g.fillRect(Ofsx,Miny,Maxx-Ofsx,Tp);
		g.fillRect(Minx,Ofsy,Tp,Maxy-Ofsy);
		
		if(Mx<16 && My<16)
		{
			g.setColor(new Color(240,240,192));						// クリックされた行・列の色を変える
			g.fillRect(Minx,Ofsy+My*Tp,Maxx-Minx,Tp);
			g.fillRect(Ofsx+Mx*Tp,Miny,Tp,Maxy-Miny);
			g.setColor(new Color(224,224,176));						// 見出しの色を変える
			g.fillRect(Minx,Ofsy+My*Tp,Tp,Tp);
			g.fillRect(Ofsx+Mx*Tp,Miny,Tp,Tp);
			g.setColor(new Color(208,208,144));						// クリックされた行・列の色を変える
			g.fillRect(Ofsx+Mx*Tp,Ofsy+My*Tp,Tp,Tp);
			dsp_code(Mx,My);
		}
		g.drawImage(fig[F_no],0,0,this);
	}
	
	public boolean mouseDown(Event e, int mx, int my)
	{
		if(inside(mx,Ofsx,Maxx) && inside(my,Ofsy,Maxy))
		{
			Mx=(mx-Ofsx)/Tp;
			My=(my-Ofsy)/Tp;
		}
		repaint();
		return true;
	}
	
	public boolean mouseMove(Event e, int mx, int my)
	{
		if(inside(mx,Ofsx,Maxx-1) && inside(my,Ofsy,Maxy-1) && Mx==99)
			dsp_code((mx-Ofsx)/Tp,(my-Ofsy)/Tp);
		return true;
	}
	
	public boolean action(Event e, Object o)
	{
		if(e.target==cb_jis8)			F_no=0;
		else if(e.target==cb_ebcdic)	F_no=1;
		else if(e.target==bt_clear)		Mx=My=99;
		repaint();
		return true;
	}
}
//
戻る