//
/* ---------------------------------------------------------------------------------------------- signedbin.java 符号つき2進数 ver. 1.00 (JDK 1.02) ueyama@infonet.co.jp 2002.03.14 ---------------------------------------------------------------------------------------------- */ import java.applet.Applet; import java.awt.*; import java.util.*; public class signedbin extends Applet { int Bx=80, By=80, Px=52, Nx=175, Ny=230; // 数値・ボタン表示座標 int Dx=163, Dy=154; // 10進数表示座標 int Num; // 表示数値 int Fs=0; // 0: 符号なし2進数 1: 符号つき2進数 int Mn=0, Mx=0; int Bgc; Image Img; Thread th=null; Graphics gr; public void init() { String bg=getParameter("BgColor"); 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){}; gr=getGraphics(); Random r=new Random(); Num=Math.abs(r.nextInt())%128+128; // 初期値を 128〜255 の範囲で } 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 c, int m, int n) // 画像の表示 { int w=0, h=0, ox=0, oy=0; Graphics gx=gr.create(); switch(c) { case 0: w= 32; h=52; ox= 0; oy= 0; break; // 2進数 case 1: w= 29; h=46; ox= 64; oy= 0; break; // 10進数 case 2: w= 28; h=21; ox= 64; oy=46; break; // 10進数表示 case 3: w= 13; h=17; ox=338; oy=80; break; // "+" case 4: w= 55; h=17; ox=338; oy=46; break; // "2進数", "10進数" case 5: w= 26; h=23; ox= 64; oy=88; break; // +/− case 6: w= 26; h=23; ox=116; oy=88; break; // + case 7: w= 26; h=23; ox=168; oy=88; break; // Dect case 8: w= 26; h=23; ox=220; oy=88; break; // Clear case 9: w= 26; h=23; ox=272; oy=88; break; // Inc case 10: w=119; h=21; ox= 92; oy=67; break; // "符号なし2進数" case 11: w= 68; h=21; ox=211; oy=67; break; // "符号つき" } gx.clipRect(x, y, (c==2 && m==8)?50:w, h); gx.drawImage(Img,x-(ox+m*w),y-(oy+n*h),this); gx.dispose(); } public void dsp_num() // 数値の表示 { int i, m=0, n, x; String bin, dec, c=""; gr.clearRect(0, 0, 476, Ny-1); bin=Integer.toString(Num,2); // 2進数 文字列 for(i=bin.length();i<8;i++) bin="0"+bin; bin+=" "; for(i=7;i>0;i--) // 2進数の表示 { m=Integer.parseInt(bin.substring(i,i+1)); dsp_img(Bx+Px*i, 0, 0, m, 0); dsp_img(Bx+Px*i+3, By, 2, (m==0)?0:8-i, 0); dsp_img(Bx+Px*i-15, By, 3, 0, 0); } dsp_img(Bx, 0, 0, Num/128, Fs); // 2進数 MSB の表示 m=Num/128; dsp_img(Bx-m*20, By, 2, (m==0)?0:8, Fs); dsp_img(0,18,4,0,0); dsp_img(0,Dy+14,4,0,1); // "2進数", "10進数" dsp_img(357, 232, 10, 0, 0); // "符号なし2進数" if(Fs==1) { dsp_img(357, 232, 11, 0, 0); // "符号つき" n=Math.abs(Num%128-(Num/128)*m*128); } else n=Num; dec=Integer.toString(n,10); // 10進数 文字列 x=Dx+(4-dec.length())*20; if(Fs==1 && Num>127) x+=20; for(i=3; i>=0; i--) // 10進数の表示 { if(dec.length()>i) { m=Integer.parseInt(dec.substring(i,i+1)); dsp_img(x+40*i, Dy, 1, m, 0); } else gr.clearRect(x+40*i, Dy, 29, 46); } if(Fs==1 && Num>127) dsp_img(x+40*i, Dy, 1, 10, 0); showStatus("Num="+Num+", dec="+dec+", Fs="+Fs+", c="+c); } public void paint(Graphics g) { dsp_num(); dsp_img(Nx, Ny,5+Fs,0,0); // +/− dsp_img(Nx+ 40,Ny,7,0,0); // Dec dsp_img(Nx+ 70,Ny,8,0,0); // Clear dsp_img(Nx+100,Ny,9,0,0); // Inc } public boolean mouseDown(Event e, int mx, int my) { int m=(mx-Bx)/Px, n=1, i; Mn=0; if((my戻るNy) // ボタン { if(inside(mx,Nx,Nx+26)) // +/− { Mn=5; Mx=Nx; dsp_img(Mx,Ny,Mn+Fs,1,0); if(Fs==0) Fs=1; else Fs=0; } if(inside(mx,Nx+40,Nx+40+26)) // Dec { Mn=7; Mx=Nx+40; Num+=255; Num%=256; } if(inside(mx,Nx+70,Nx+70+26)) // Clear { Mn=8; Mx=Nx+70; Num=0; } if(inside(mx,Nx+100,Nx+100+26)) // Inc { Mn=9; Mx=Nx+100; Num+=1; Num%=256; } dsp_num(); if(Mn>6) dsp_img(Mx,Ny,Mn,1,0); } return true; } public boolean mouseUp(Event e, int mx, int my) { if(Mn>0) { if(Mn==5) dsp_img(Mx,Ny,Mn+Fs,0,0); else dsp_img(Mx,Ny,Mn,0,0); } return true; } } //