/*------------------------------------------------------------------
アプレット版数当てゲーム Applet_kazuate_game
------------------------------------------------------------------*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**/
public class Applet_kazuate_game extends Applet
implements ActionListener {
TextField txt1; // テキストボックスの変数
Label lb1; // 文字ラベルの変数
Button btn1; // ボタンクラスの変数
int n=0;
int kazu; // 入力された数値
int ransu; // 当てたい数(正解)
// -------------- 初期設定 ---------------
public void init() {
txt1=new TextField(10);
this.add(txt1); // テキストボックスの表示
txt1.addActionListener(this); // アクションリスナーを追加
lb1=new Label("数字を入れてください");
this.add(lb1); // 文字ラベルの表示
btn1=new Button("最初から");
this.add(btn1); // ボタンの表示
btn1.addActionListener(this); // ボタンにアクションリスナーを追加
resetGame(); // ゲーム開始
}
// -------------- ゲームをリセット ---------------
public void resetGame() {
ransu=(int)(Math.random()*100+1);
n=0; }
// -------------- 画面描画 ---------------
public void paint(Graphics g) {
g.drawString(Integer.toString(ransu),50,300); // ←これが答え
if (n>0) {
if (kazuransu)
g.drawString("もっと小さいです!",50,100);
else {
g.setColor(Color.red);
g.drawString("***当たり!!*** "+n+"回目",50,100);
}
}
}
// -------------- アクションリスナー ---------------
public void actionPerformed(ActionEvent ev) {
Object s=ev.getSource();
if (s==txt1) {
kazu=Integer.parseInt(txt1.getText());
n++;
txt1.setText(""); // テキストボックスのクリア
}
if (s==btn1)
resetGame();
repaint();
}
}