//
/* ----------------------------------------------------------------------------------------------
    bin2dec.java	2進数から10進数への変換				ver. 2.00 (JDK 1.02)
    														ueyama@infonet.co.jp  1998.11.23
    														update: 2007.06.21
---------------------------------------------------------------------------------------------- */

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


public class bin2dec extends Applet
{
	int			Dw[]={19,19,19,19,38,38,38,50};
	int			Bp=19, Ip=32, Dp=38, Sp=18, Fh=26;					// 数字の表示ピッチおよび高さ
	int			Dec;												// 10進数数値
	int			Dgt;												// 2進数桁数
	String		Bin;												// 2進数数値(文字列)
	int			Stp=0;												// 変換実行ステップ
	Image		Img;
	Button		bt_stp;
	Graphics	gr;
	Checkbox	cb_bin, cb_dec, cb_hex;
	TextField	tf_num;
	CheckboxGroup Cg=new CheckboxGroup();
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void init()
	{
		Random		rnd=new Random();
		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"));		// Applet用 画像ファイル
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		gr=getGraphics();
		
		Dec=Math.abs(rnd.nextInt())%128+128;						// 初期値を乱数で発生させる
		dec_to_bin();
		Panel p=new Panel();
		p.add(cb_bin	=new Checkbox("BIN",Cg,true));
		p.add(cb_dec	=new Checkbox("DEC",Cg,false));
		p.add(cb_hex	=new Checkbox("HEX",Cg,false));
		p.add(tf_num	=new TextField(8));
		p.add(new Label("  "));
		p.add(bt_stp	=new Button("STEP"));
		setLayout(new BorderLayout());
		add("South" ,p);
		tf_num.setText(Bin);
	}
	
	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= 19; h= 24; ox=  0; oy=  0;	break;		// 数字
			case	 1:	w= 32; h= 26; ox=  0; oy= 48;	break;		// 2のべき乗表示
			case	 2:	w= 38; h= 24; ox=  0; oy=100;	break;		// 1, 2, 4, 8, 16, 32 …
			case	 3:	w= 50; h= 24; ox=255; oy= 50;	break;		// 128
			case	 4:	w= 18; h= 24; ox=190; oy=  0;	break;		// +、×、=
			case	 5:	w= 46; h= 11; ox=244; oy=  0;	break;		// "(2進数)"、"(10進数)"
		}
		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 paint(Graphics g)
	{
		int		i,j,m,s,x,y,x0;
		String	dec="";
		
		g.clearRect(0,0,size().width,size().height);
		x0=(8-Dgt)*(Bp+Ip+Sp*2)/2;
		
		x=x0+Sp+3; y=0;
		for(i=0; i0)
		{
			x=x0; y+=50;
			dsp_g(x,y,2,1,4);	x+=Sp+3;							// '=' 記号の表示
			for(i=0; i1)												// 10進数の和の表示
			{
				x=x0; y+=50;
				dsp_g(x,y,2,1,4);	x+=Sp+3;						// '=' 記号の表示
				for(i=0; i2)												// 10進数の表示
			{
				dec=Integer.toString(Dec);
				x=x0; y+=50;
				dsp_g(x,y,2,1,4);	x+=Sp+3;						// '=' 記号の表示
				for(i=0; i=0) k+=m;
			else	return false;
			if(i==0 && s.length()==2) k*=16;
		}
		Dec=k;
		return true;
	}
	
	public boolean action(Event e, Object o)
	{
		int		i,j,k,m,n;
		String	num, d="0123456789";
		boolean	f=true;
		
		if(e.target==bt_stp)										// STEP ボタン
		{
			Stp++;
			if(Stp==3) bt_stp.disable();
			repaint();
		}
		else if(e.target==tf_num)									// 数値入力の処理
		{
			num=tf_num.getText();
			if(num.length()>0)
			{
				if(Cg.getCurrent().getLabel()=="BIN")				// 2進数
				{
					k=num.length();
					if(k>0 && k<9)
					{
						n=0;
						for(j=1,i=k-1; i>=0; i--,j*=2)
						{
							m=Integer.parseInt(num.substring(i,i+1));
							if(m==1 || m==0) n+=m*j;
							else f=false;
						}
						if(f)
						{
							Dec=n; Bin=num; Dgt=k;
						}
					}
				}
				else if(Cg.getCurrent().getLabel()=="DEC")			// 10進数
				{
					for(i=0;i=0 && k<256)
						{
							Dec=k; dec_to_bin();
						}
					}
				}
				else												// 16進数
				{
					k=num.length();
					if(k==1 || k==2)
					{
						if(hex_to_dec(num)) dec_to_bin();
					}
				}
			}
			Stp=0; bt_stp.enable();
			repaint();
		}
		return true;
	}
}
//
戻る