//
/* ----------------------------------------------------------------------------------------------
binary.java 2進数 ver. 3.00 (JDK 1.02)
ueyama@infonet.co.jp 1999.04.25
update: 2008.09.05
---------------------------------------------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;
public class binary extends Applet implements Runnable
{
int Nx=64, Ny=30, Bc=-1; // 数値表示座標
int Bin=0; // 表示数値
int Def=1; // 1: increment -1: decrement
int Wait=1024; // 表示待ち時間
boolean Run=false; // true: Run false: Stop
int Bx[]={126, 156, 186, 226, 256, 286}, By=260; // ボタンの表示座標値
Image Img;
Thread th=null;
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){};
}
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 dsp_img(int x, int y, int m, int c) // 画像の表示
{
int w=0, h=0, ox=0, oy=0;
Graphics gx=getGraphics();
switch(c)
{
case 0: w= 32; h=78; ox= 0; oy= 0; break; // 2進数 数字
case 1: w= 37; h=46; ox= 64; oy= 0; break; // 16進数 数字
case 2: w= 58; h=17; ox= 64; oy=69; break; // "2, 10, 16進数"
case 3: w= 26; h=23; ox= 64; oy=46; break; // ボタン
case 5: w=181; h=12; ox=238; oy=69; break; // "Slow Run Fast Dec Clear Inc"
case 6: w= 22; h=12; ox=418; oy=69; break; // "Run" or "Stop"
}
gx.clearRect(x, y, w, h);
gx.clipRect(x, y, w, h);
gx.drawImage(Img,x-(ox+m*w),y-oy,this);
gx.dispose();
}
public void dsp_num() // 数値の表示
{
int i, j, k, m;
for(i=0,j=128; i<8; i++,j/=2) dsp_img(39*i+Nx, Ny, ((Bin & j)==0)?0:1, 0); // 2進数の表示
m=Bin; j=16;
for(i=0; i<2; i++)
{
dsp_img(39*i+Nx+24, Ny+140, m/j, 1); // 16進数の表示
m-=(m/j*j); j/=16;
}
m=Bin; j=100;
for(i=0; i<3; i++)
{
dsp_img(39*i+Nx+174, Ny+140, m/j, 1); // 10進数の表示
m-=(m/j*j); j/=10;
}
}
public void paint(Graphics g)
{
dsp_num();
dsp_img(Nx,0,0,2); // '2進数'
dsp_img(Nx+24,110+Ny,2,2); // '16進数'
dsp_img(Nx+174,110+Ny,1,2); // '10進数'
dsp_img(Nx+120,140+Ny,16,1); // '=' の表示
dsp_img(Bx[0],By,(Wait<8192)?0:2,3); // Slow
dsp_img(Bx[1],By,(Run)?18:3,3); // Run/Stop
dsp_img(Bx[2],By,(Wait>64)?6:8,3); // Fast
dsp_img(Bx[3],By,9,3); // Dec
dsp_img(Bx[4],By,12,3); // Clear
dsp_img(Bx[5],By,15,3); // Inc
dsp_img(127,By+28,0,5); // "Slow Run Fast Dec Clear Inc"
if(Run) dsp_img(Bx[1]+2,By+28,0,6);
}
public void run()
{
while(true)
{
try
{
th.sleep((Run)?Wait:256);
}
catch (InterruptedException e){}
if(Run)
{
Bin+=(256+Def); // 数値の自動更新
Bin%=256;
dsp_num(); // 数値の表示
}
}
}
public void stop()
{
if(th!=null)
{
th.stop();
th=null;
}
}
public boolean mouseDown(Event e, int mx, int my)
{
int m,n,i;
Bc=-1;
if(inside(my,Ny,Ny+78) && inside(mx,Nx,Nx+312)) // 2進数 数値編集
{
m=7-(mx-Nx)/39;
n=(int)Math.pow(2,m); // p = 2 ^ m
if((Bin & n) ==0) Bin+=n;
else Bin-=n;
dsp_num(); // 数値の表示
}
if(inside(my,By,By+23))
{
for(i=0; i<6; i++)
{
if(inside(mx,Bx[i],Bx[i]+26)) // ボタンがクリックされた
{
switch(i)
{
case 0: if(Wait<8192) {Wait*=2; dsp_img(Bx[0],By,1,3);} break; // Slow
case 1: Run=!Run; dsp_img(Bx[i], By, (Run)?4:19, 3); break; // Stop
case 2: if(Wait>64) {Wait/=2; dsp_img(Bx[2],By,7,3);} break; // Fast
case 3: Def=-1; Bin+=255; Bin%=256; break; // Dec
case 4: Bin=0; break; // Clear
case 5: Def=1; Bin+=Def; Bin%=256; break; // Inc
}
dsp_num(); // 数値の表示
if(i>2) dsp_img(Bx[i], By, i*3+1, 3); // ボタンの表示(緑)
Bc=i;
}
}
}
return true;
}
public boolean mouseUp(Event e, int mx, int my)
{
if(Bc>=0)
{
switch(Bc)
{
case 0: dsp_img(Bx[0],By,(Wait<8192)?0:2,3); // Slow
dsp_img(Bx[2],By,6,3); break;
case 1: dsp_img(Bx[1], By, (Run)?18:3, 3); // Run or Stop
dsp_img(Bx[1]+2,By+28,(Run)?1:0,6); break;
case 2: dsp_img(Bx[2],By,(Wait>64)?6:8,3); // Fast
dsp_img(Bx[0],By,0,3); break;
default: dsp_img(Bx[Bc],By,Bc*3,3); // others
}
}
return true;
}
}
//戻る