//
/* ----------------------------------------------------------------------------------------------
add_ws.java 2進数の加算 (演習) ver. 1.01 (JDK 1.02)
ueyama@infonet.co.jp 2007.04.04
---------------------------------------------------------------------------------------------- */
import java.applet.*;
import java.awt.*;
import java.util.*;
public class add_ws extends Applet
{
int Exe[][]=new int[3][9]; // 被加数[0]・加数[1]・和[2] の2進形式データ
int Dgt[]={0,0,0}; // 被加数[0]・加数[1]・和[2] の桁数
int Ans[][]={{2,2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2,2}}; // 回答
int Aug, Add, Sum; // 被加数, 加数, 和
int Dmx; // 桁数の最大値
int Ax[]=new int[3]; // 数値の表示X座標
int Bx; //「○×」「?」の表示X座標
int Dx; // 基準X座標
int Fin=2; // Fin==0: 入力完了
int Xx[]={0, 41, 0, 14, 55, 96, 110, 69, 110, 96, 55, 14, 0}; // '×' 表示x座標データ
int Xy[]={14, 55, 96, 110, 69, 110, 96, 55, 14, 0, 41, 0, 14}; // '×' 表示y座標データ
int X_x[]=new int[13], X_y[]=new int[13]; // 同上
boolean Sc =true; //「○×」「?」ボタンの表示
boolean Ac =true; // true: 音を出す
boolean Ae =false; // 加数 Add の入力・編集モード
boolean Clr=false;
boolean Com=false; // true: 正解
boolean Da=false; // true:「?」ボタンが押された
AudioClip Pinpon, Boouu;
Image Img;
Graphics gr;
public void init()
{
int i,n;
String s,bg,vds;
Random rnd=new Random();
bg=getParameter("BgColor");
n=Integer.valueOf(bg,16).intValue();
setBackground(new Color(n));
vds=getParameter("augend"); // ** 被加数データの読み取り 'Aug'
Aug=Integer.valueOf(vds,16).intValue();
if(Aug==0) Aug=Math.abs(rnd.nextInt())%255+1; // 0 のときは被加数を乱数で発生させる
for(i=0,n=Aug; i<8; i++){Exe[0][i]=n % 2; n/=2;} // 被加数の2進形式データ
for(i=Aug,Dgt[0]=0; i>0; i/=2) Dgt[0]++; // 被加数の桁数
vds=getParameter("addend"); // ** 加数データの読み取り 'Add'
Add=Integer.valueOf(vds,16).intValue();
if(Add==0) Add=Math.abs(rnd.nextInt())%255+1; // 0 のときは加数を乱数で発生させる
if(Add<0)
{
Ae=true; // 加数 Add の入力・編集モード
Add*=-1; // Add は負の数で指定されている
}
for(i=0,n=Add; i<8; i++){Exe[1][i]=n % 2; n/=2;} // 加数の2進形式データ
for(i=Add,Dgt[1]=0; i>0; i/=2) Dgt[1]++; // 加数の桁数
s=getParameter("check"); //「○×」「?」ボタンの表示
if(s.equals("0")) Sc=false;
s=getParameter("sound"); //「○×」ボタンが押されたときの音の有無
if(s.equals("0")) Ac=false;
Sum=Aug+Add;
for(i=0,n=Sum; i<9; i++){Exe[2][i]=n % 2; n/=2;} // 和の2進形式データ
for(i=Sum,Dgt[2]=0; i>0; i/=2) Dgt[2]++; // 和の桁数
Dmx=Math.max(Dgt[0],Dgt[1]); // 桁数の最大値
Dx=(8-Dmx)*20; // 基準X座標
Ax[1]=Dx+(1-(Dgt[1]-Dmx))*40; // 加数の表示X座標
Ax[2]=Dx+(1-(Dgt[2]-Dmx))*40; // 和の表示X座標
Bx=Dx+(Dmx+1)*40+30; // 「○×」「?」ボタンの表示X座標
for(i=0; i<13; i++) {X_x[i]=Xx[i]+125; X_y[i]=Xy[i]+20;} // '×' 表示座標の換算
Pinpon=getAudioClip(getCodeBase(), "pinpon.au");
Boouu=getAudioClip(getCodeBase(), "boouu.au");
MediaTracker mt=new MediaTracker(this);
Img=getImage(getCodeBase(), getParameter("figure"));
mt.addImage(Img, 0);
try {mt.waitForID(0);}
catch(InterruptedException e){};
gr=getGraphics();
}
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 m, int n, int c) // 図や文字を描く
{
Graphics gx=gr.create();
int w=0, h=0, ox=0, oy=0;
switch(c)
{
case 0: w= 20; h= 31; ox= 0; oy= 0; break; // 0 or 1
case 1: w= 27; h= 27; ox=60; oy= 0; break; // + or -
case 2: w= 26; h= 23; ox= 0; oy=124; break; // ボタン
}
gx.clipRect(x,y,w,h);
gx.clearRect(x,y,w,h);
gx.drawImage(Img, x-(ox+m*w), y-(oy+n*h), this);
gx.dispose();
}
public void dsp_exe(int n) // 演習問題の表示
{
int i, k=0;
gr.setColor(new Color(224,215,200)); // 加数入力欄背景色
for(i=0;i108) Ans[2][m]=(Ans[2][m]==0)?1:0;
for(i=0,n=0; i0)?2:0; // Fin==0: 入力完了
dsp_ans(2);
if(Sc) dsp_g(Bx, 94, Fin, 0, 2); // 「!」の表示
if(Sc) dsp_g(Bx,124, Fin, 1, 2); // 「?」の表示
}
if(inside(x, Bx, Bx+26) && Fin==0 && Sc) // ボタンを押す
{
if(Ae) for(i=0, n=0; i戻る