//
/* ----------------------------------------------------------------------------------------------
cipher.java 暗号 ver. 1.10 (JDK 1.02)
ueyama@infonet.co.jp 2000.02.27
update: 2007.08.16
---------------------------------------------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;
import java.lang.Math;
import java.util.*;
public class cipher extends Applet
{
int Cyp[]= new int[6]; // 暗号鍵データ
Image Img;
Random Rnd=new Random();
public void init()
{
String bg=getParameter("bgcolor"); // 背景色の読み込み
int c=Integer.valueOf(bg,16).intValue();
setBackground(new Color(c));
MediaTracker mt=new MediaTracker(this);
Img=getImage(getCodeBase(), getParameter("figure")); // gif ファイルの読み込み
mt.addImage(Img, 0);
try
{
mt.waitForID(0);
}
catch(InterruptedException e){};
rnd_key();
}
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 hol, int n) // 画像の表示
{
int w=0,h=0, ox=0,oy=0;
switch(n)
{
case 0: w= 32; h= 17; ox= 0; oy= 0; break; // スイッチ
case 1: w= 21; h= 21; ox= 64; oy= 0; break; // 電球
case 2: w= 37; h= 24; ox=106; oy= 0; break; // 電池
case 3: w= 14; h= 13; ox=143; oy= 0; break; // 文字
case 4: w= 5; h= 5; ox= 0; oy=17; break; // 黒丸
case 6: w= 26; h= 23; ox=227; oy= 0; break; // ボタン
case 7: w= 79; h= 11; ox=143; oy=13; break; // "スクランブラー"
}
Graphics gx=getGraphics();
gx.clearRect(x,y,w,h);
gx.clipRect(x,y,w,h);
gx.drawImage(Img,x-(ox+w*hol),y-oy,this);
gx.dispose();
}
public void rnd_key() // ランダムな鍵を発生
{
int i,j,k=0,d=0, ex[]=new int[6];;
for(i=0; i<6; i++) ex[i]=Math.abs(Rnd.nextInt()); // ex[i] に乱数を入れる
for(i=0; i<6; i++)
{
for(j=0; j<6; j++) {if(ex[j]>d) {d=ex[j]; k=j;}}
ex[k]=0; Cyp[i]=k; d=0; // Cyp[] は ex[] の大きさの順位データ
}
}
public void dsp_w() // 暗号化部配線の表示
{
Graphics gr=getGraphics();
gr.setColor(new Color(224,224,240));
gr.fillRect(109,17,133,153); // 長方形を描く
gr.setColor(new Color(0));
for(int y=0;y<6;y++) gr.drawLine(242,y*25+31,109,Cyp[y]*25+31); // 線を引く
}
public void paint(Graphics g)
{
int i, x, y;
for(y=31;y<177;y+=25) g.drawLine(2,y, 364,y); // 水平線を引く
g.drawLine(2,201,364,201);
for(x=0;x<2;x++) g.drawLine(x*362+2,31,x*362+2,201); // 垂直線を引く
dsp_w(); // 暗号化部の表示
dsp_g(136,0,0,7); // "スクランブラー" の表示
for(y=0;y<6;y++)
{
dsp_g(298,y*25+19,0,0); // スイッチの表示
dsp_g(56,y*25+21,0,1); // 電球の表示
dsp_g(32,y*25+15,y,3); // 文字の表示
dsp_g(274,y*25+15,y,3); // 文字の表示
if(y<5) dsp_g(0,y*25+54,0,4); // 黒丸の表示
if(y<5) dsp_g(362,y*25+54,0,4); // 黒丸の表示
}
dsp_g(155,191,0,2); // 電池の表示
dsp_g(261,215,0,6); // ランダムボタンの表示
dsp_g(295,215,4,6); // シーザーボタンの表示
dsp_g(329,215,2,6); // クリアボタンの表示
}
public boolean mouseDown(Event e, int mx, int my)
{
int i,y;
if(inside(mx,298,330) && my<161) // スイッチをクリック
{
dsp_g(298,my/25*25+19,1,0); // スイッチの表示
dsp_g(56,Cyp[my/25]*25+21,1,1); // 電球の表示
}
if(inside(mx,261,355) && inside(my,215,238)) // ボタン
{
if(inside(mx,261,302)) // ランダムボタン
{
dsp_g(261,215,1,6); // ランダムボタンの表示
rnd_key(); // 鍵の生成
}
if(inside(mx,295,321)) // シーザーボタン
{
dsp_g(295,215,5,6); // シーザーボタンの表示
y=Math.abs(Rnd.nextInt())%5+1; // 鍵の生成
for(i=0;i<6;i++) Cyp[i]=(i+y)%6;
}
if(inside(mx,329,355)) // クリアボタン
{
dsp_g(329,215,3,6); // クリアボタンの表示
for(y=0;y<6;y++) Cyp[y]=y;
}
dsp_w(); // 暗号化部の表示
}
return true;
}
public boolean mouseUp(Event e, int mx, int my)
{
if(inside(mx,298,330) && my<161)
{
dsp_g(298,my/25*25+19,0,0); // スイッチの表示
dsp_g(56,Cyp[my/25]*25+21,0,1); // 電球の表示
}
if(inside(mx,261,355) && inside(my,215,238))
{
if(inside(mx,261,287)) dsp_g(261,215,0,6); // ランダムボタンの表示
if(inside(mx,295,321)) dsp_g(295,215,4,6); // シーザーボタンの表示
if(inside(mx,329,355)) dsp_g(329,215,2,6); // クリアボタンの表示
}
return true;
}
}
//戻る