//
/* ----------------------------------------------------------------------------------------------
adder_r.java 半加算器 (リレー) ver. 2.10 (JDK 1.02)
ueyama@infonet.co.jp 2003.10.27
update: 2007.10.18
---------------------------------------------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;
public class adder_r extends Applet
{
int A=0, B=0, S=0, C=0; // 入出力値
int Tt[][]={{0,0,0,0},{0,1,0,1},{1,0,0,1},{1,1,1,0}}; // 真理値表
int Ax[]={22, 22, 22, 22, 33, 33, 34, 33,163}; // スイッチ・文字の表示 X座標 (for A)
int Ay[]={20,132,192,237,115,168,164,220, 29}; // スイッチ・文字の表示 Y座標
int At[]={0,1,2,1,4,4,5,4,4}; // 表示パーツ
int Bx[]={80, 80, 80, 80, 91, 92, 91, 91,163}; // スイッチ・文字の表示 X座標 (for B)
int By[]={73,139,185,237,115,111,168,220, 82}; // スイッチ・文字の表示 Y座標
int Bt[]={0,2,1,1,4,5,4,4,4}; // 表示パーツ
int Xt[]={315,339,372,396}; // 真理値表 数値表示 X座標
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){};
}
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) // gif 画像の部分表示
{
int w=0, h=0, ox=0, oy=0;
switch(c)
{
case 0: w= 31; h=21; ox=420; oy= 0; break; // 押しボタンスイッチ の表示
case 1: w= 31; h=16; ox=420; oy= 21; break; // a接点 の表示
case 2: w= 31; h=16; ox=420; oy= 37; break; // b接点 の表示
case 3: w= 25; h=25; ox=420; oy= 53; break; // 電球 の表示
case 4: w= 12; h=14; ox=420; oy= 78; break; // A の表示
case 5: w= 9; h= 2; ox=420; oy=106; break; //  ̄ の表示
case 6: w= 12; h=16; ox=420; oy=110; break; // 数字 の表示
}
Graphics gx=getGraphics();
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_p() // 画面の表示
{
int i,j;
for(i=0;i<9;i++) dsp_g(Ax[i],Ay[i],A,0,At[i]); // 接点と文字 A の表示
for(i=0;i<9;i++) dsp_g(Bx[i],By[i],B,(i<4)?0:1,Bt[i]); // 接点と文字 B の表示
dsp_g(156,157,S,0,3); // 電球 S
dsp_g(156,236,C,0,3); // 電球 C
for(i=0;i<4;i++) for(j=0;j<4;j++) dsp_g(Xt[j],131+i*25,Tt[i][j],(i==A*2+B)?1:0,6); // 真理値表
}
public void paint(Graphics g)
{
int i;
g.clearRect(0,0,size().width,size().height);
g.drawImage(Img,0,0,this);
dsp_p();
}
public boolean mouseDown(Event e, int x, int y)
{
int m;
if(inside(x,14,64) && inside(y, 2, 50)) A=(A==0)?1:0; // スイッチ A をクリック
if(inside(x,70,120) && inside(y,54,103)) B=(B==0)?1:0; // スイッチ B をクリック
if(x>Xt[0]-10 && inside(y,127,227)) // 真理値表をクリック
{
m=(y-127)/25;
A=m/2; B=m%2;
}
S=Tt[A*2+B][3];
C=Tt[A*2+B][2];
dsp_p();
return true;
}
}
//戻る