//
/* ----------------------------------------------------------------------------------------------
    shiftregister.java	シフトレジスタ						ver. 1.01 (JDK 1.02)
    														ueyama@infonet.co.jp  1999.05.11
    														update: 2008.05.30
---------------------------------------------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;
import java.util.*;


public class shiftregister extends Applet
{
	int			Dec;													// レジスタの値 (10進数)
	int			Sh=0, Sl=0;												// シリアルデータ
	Image		Img;
	
	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"));
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		Random		rnd=new Random();
		Dec=Math.abs(rnd.nextInt())%256;								// 初期値を乱数で発生させる
	}
	
	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_p(int x, int y, int m, int c)						// 画像の表示
	{
		Graphics gx=getGraphics();
		int      w=0, h=0, ox=0, oy=0;
		switch(c)
		{
			case	 0:	w=15; h=22; ox=  0; oy=30;	break;				// 2進数
			case	 1:	w=10; h=14; ox= 30; oy=30;	break;				// 10進数
			case	 2:	w=28; h=12; ox=150; oy=30;	break;				// 矢印
			case	 3:	w=26; h=23; ox=  0; oy=52;	break;				// ボタン
			case	 4:	w=47; h=11; ox=156; oy=55;	break;				// (10進数), (2進数)
			case	 5:	w=299;h=30; ox=  0; oy= 0;	break;				// シフトレジスタ 枠
			case	 6:	w=100;h=13; ox=156; oy=42;	break;				// "シフトレジスタ"
		}
		gx.clipRect(x,y,w,h);
		gx.clearRect(x,y,w,h);
		gx.drawImage(Img,x-(ox+m*w),y-oy,this);							// 画像の表示
		gx.dispose();
	}
	
	public void dsp_s()													// 数値の表示
	{
		int	i,j,m,n;
		dsp_p(4,24,Sh,0); dsp_p(280,24,Sl,0);							// シリアルデータ
		for(j=128, i=0; i<8; i++,j/=2)									// シフトレジスタの値表示
		{
			m=((Dec & j)==0)?0:1;
			dsp_p(22*i+65,24,m,0);										// シフトレジスタ内部
			dsp_p(13*i+30,70,m,1);										// 下部
		}
		m=Dec/100; dsp_p(198,70,(m>0)?m:11,1);							// 10進数の表示
		n=Dec/10; m=n%10; dsp_p(211,70,(n>0)?m:11,1);
		m=Dec%10; dsp_p(224,70,m,1);
	}
	
	public void paint(Graphics g)
	{
		g.clearRect(0,0,size().width,size().height);
		dsp_p(0,20,0,5);												// "シフトレジスタ" (文字) の表示
		dsp_p(100,0,0,6);												// シフトレジスタ (枠) の表示
		dsp_p(134,73,1,4);												// "(2進数)" の表示
		dsp_p(183,70,10,1);												// = の表示
		dsp_p(237,73,0,4);												// "(10進数)" の表示
		dsp_p(120,100,0,3);												// 左シフトボタンの表示
		dsp_p(160,100,3,3);												// 右シフトボタンの表示
		dsp_s();														// 数値の表示
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		int	m,i;
		dsp_p(28,29,2,2); dsp_p(243,29,2,2);							// 矢印を消す
		if(inside(y,20,50))												// レジスタ部をクリック
		{
			if(inside(x,  0,22)) Sh=(Sh==0)?1:0;						// シリアルデータ (H)
			if(inside(x,277,299)) Sl=(Sl==0)?1:0;						// シリアルデータ (L)
			if(inside(x, 61,237))										// レジスタ
			{
				m=7-(x-61)/22;											// クリックされた桁
				double n=Math.pow(2,m);									// 2 の冪乗
				if((Dec & (int)n) ==0) Dec+=n;							// 数値の更新
				else Dec-=n;
			}
		}
		else if(inside(y,100,123))
		{
			if(inside(x,120,146))										// 左シフトボタン
			{
				Sh=Dec/128;
				Dec*=2; Dec%=256; Dec+=Sl;
				dsp_p(28,29,1,2); dsp_p(243,29,1,2);					// 矢印の表示
				dsp_p(120,100,1,3);										// ボタンの色を変える
			}
			if(inside(x,160,186))										// 右シフトボタン
			{
				Sl=Dec%2;
				Dec/=2; Dec+=Sh*128;
				dsp_p(28,29,0,2); dsp_p(243,29,0,2);					// 矢印の表示
				dsp_p(160,100,4,3);										// ボタンの色を変える
			}
		}
		dsp_s();														// 数値の表示
		return true;
	}
	
	public boolean mouseUp(Event e, int x, int y)
	{
		if(inside(y,100,123) && inside(x,120,186))
		{
			dsp_p(120,100,0,3);											// ボタンの色を元に戻す
			dsp_p(160,100,3,3);
		}
		return true;
	}
}
//
戻る