/*------------------------------------------------------------------ アプレット版 数当てゲーム Applet_kazuate_game ------------------------------------------------------------------*/ import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class Applet_kazuate_game extends Applet implements ActionListener { int n=0; //当てるまでの回数 int number; //入力する数字 int ransu; //正解 TextField txt; Label lb; Button btn1; // ---------- アプレットの初期設定 ---------- public void init() { lb=new Label("数字を入れて下さい"); // ラベルの生成 this.add(lb); // ラベルの表示 txt=new TextField(10); // テキストボックスの生成 this.add(txt); // テキストボックスの表示 txt.addActionListener(this); // アクションリスナーを追加 btn1=new Button("最初から"); // ボタンの生成 this.add(btn1); // ボタンの表示 btn1.addActionListener(this); // アクションリスナーを追加 resetGame(); // ゲームのリセット } // ---------- 乱数の生成(ゲームリセット) ---------- public void resetGame() { ransu=(int)(Math.random()*100)+1; // 1-100までの乱数生成 n=0; number=0; } // ---------- 描画メソッド ---------- public void paint(Graphics g) { g.drawString(Integer.toString(ransu),50,300); // ←こたえ if (n>0) { if ( numberransu ) g.drawString("もっと小さいですよ",50,100); else g.drawString("当たり!! "+n+"回目でした",50,100); } } // ---------- アクションリスナー ---------- public void actionPerformed(ActionEvent e) { Object src=e.getSource(); if ( src==txt ) { number=Integer.parseInt(txt.getText()); n++; txt.setText(""); // テキストボックスのクリア } if ( src==btn1 ) { resetGame(); } repaint(); // 描画メソッド呼び出し } }