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

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

public class dec2bin extends Applet
{
	int			Step=0;												// 2進化演算の進行ステップ数
	int			Dec;												// 2進数に変換される10進数
	int			DecDgt=0;											// Dec の桁数
	int			BinDgt=0;											// Dec を2進数にしたときの桁数
	int			Ww,Wh,Wx;											// Applet 幅, 高さ, 表示位置
	String		DecStr;												// 入力された10進数を文字列にしたもの
	Image		Img;
	TextField	Tf_dec=new TextField("",8);
	Random		rnd=new Random();
	Graphics	gr;
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void init()
	{
		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();
		
		Ww=size().width; Wh=size().height;
		Dec=Math.abs(rnd.nextInt())%128+128;						// 初期値を乱数で発生させる
		dec_set();
		setLayout(null);
		add(Tf_dec);
		Tf_dec.setText(""+Dec);										// text field に初期値を表示
		Tf_dec.reshape(110,384,52,24);								// TextField の表示
	}
	
	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_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=  6; h= 33; ox= 78; oy= 24;	break;		// ')'
			case	 2:	w= 26; h= 23; ox=  0; oy= 24;	break;		// ボタン
		}
		gx.clipRect(x,y,w,h);
		gx.drawImage(Img, x-(ox+f*w), y-(oy+v*h), this);
		gx.dispose();
	}

	public void dec_set()
	{
		DecStr=Integer.toString(Dec,10);
		DecDgt=DecStr.length();
		String s=Integer.toString(Dec,2);
		BinDgt=s.length();
		Wx=(Ww-(DecDgt+BinDgt+2)*24)/2;
		Step=0;
	}

	public void dsp_op()											// 画面の表示
	{
		int		i,k,m,r,s;
		int		x0=130, y0=300;
		int		dy=40, pitch=37;
		String	bin, d, dec="";

		if(Dec>0)
		{
			if(Step==BinDgt) gr.setColor(new Color(204,204,153));
			else gr.setColor(new Color(224,224,176));
			gr.fillRect(Wx-12,y0+dy-6,(DecDgt+BinDgt+3)*24,pitch);	// 長方形の表示
			gr.setColor(Color.black);
			gr.drawRect(Wx-12,y0+dy-6,(DecDgt+BinDgt+3)*24,pitch);
			for(i=0;i0)												// 余りの表示
				{
					for(i=0;i<5;i++) dsp_g(x0+DecDgt*24+i*18+14,y0-k*pitch, 11, 0, 0);
					dsp_g(x0+(DecDgt+4)*24+8,y0-k*pitch, r, 0 ,0);
					dsp_g(Wx+(DecDgt+BinDgt+2-k)*24,y0+dy, r, 0, 0);
					if(Step==BinDgt) dsp_g(Wx+(DecDgt*2+1)*12,y0+dy, 10, 0, 0);
					else for(i=0;i<(BinDgt-Step+1)*2;i++) dsp_g(Wx+(DecDgt*2+i+1)*12,y0+dy,11,0,0);
				}
				if(k'
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		if(inside(x,270,296) && y>=385 && Step' ボタンをクリック
		{
			dsp_g(270, 385, 1, 0, 2);
			Step++;
		}
		if(inside(x,238,264) && y>=385 && Step>0)					// '<|' ボタンをクリック
		{
			dsp_g(238, 385, 1, 1, 2);
			Step--;
		}
		if(inside(x,194,220) && y>=385 && Step>0)					// 'レ' ボタンをクリック
		{
			dsp_g(194, 385, 1, 2, 2);
			Step=0;
		}
		dsp_op();
		return true;
	}

	public boolean mouseUp(Event e, int x, int y)
	{
		if(y>=385 && inside(x,194,296))								// 押されたボタンを離す
		{
			dsp_g(270, 385, (Step==BinDgt)?2:0, 0, 2);
			dsp_g(238, 385, (Step==0)?2:0, 1, 2);
			dsp_g(194,385,(Step==0)?2:0,2,2);
			if(inside(x,194,264)) {gr.clearRect(0,0,Ww,330); dsp_op();}	// 'レ' or '<|' ボタン
		}
		return true;
	}

	public boolean action(Event e, Object o)						// 数値の入力処理
	{
		String d=Tf_dec.getText();
		if(d.length()>0)
		{
			if(inside(Integer.parseInt(d),1,255))
			{
				Dec=Integer.parseInt(d); dec_set();
			}
		}
		repaint();
		return true;
	}
}
//
戻る