前回回答してくださった皆様、ありがとうございました。 現在、Javaで簡単なモグラ叩きゲームを作っており、リトライボタンを追加 しようと思っているのですが、なぜか上手くボタンが表示されません・・・何故で しょうか? また、マウスのクリック待ち時間(クリック制限時間)を作りたいのですが、どのようにすれば よいでしょうか? ソースは以下の通りです。(※字数制限に引っかかった、未使用のイベントなどは省略していますがご了承ください※) import java.awt.*; import java.awt.event.*; import java.util.Random; class mole_hit extends Frame implements MouseListener, ActionListener, MouseMotionListener { int mole_point;//土竜の出現データ int fore_mole_point;//土竜の前の出現データ int mole_x, mole_y;//土竜の出現座標 int mouse_x, mouse_y;//マウスの座標 int life = 10; long t = 1000; MyCanvas mc = new MyCanvas(); public static void main(String[] args) { new mole_hit(); } mole_hit() { super("土竜叩きゲーム");//フレーム名 setSize(700, 700);//フレームサイズ setLayout(null); if (life <= 0)//ライフが0以下になったら、 {//リトライボタンを表示 Button b = new Button("RETRY"); b.addActionListener(this); b.setSize(50, 25); b.setLocation(620, 620); add(b); } mc.setSize(600, 600);//キャンバスサイズ mc.setLocation(0, 0);//キャンバス配置座標 mc.addMouseListener(this); mc.addMouseMotionListener(this); add(mc); setVisible(true); addWindowListener(new Close()); //ウィンドウクローズ } /*** ボタン等のイベント ***/ public void actionPerformed(ActionEvent e) { life = 10; mc.repaint(); } /*** マウスボタンが押されたとき ***/ public void mousePressed(MouseEvent e) { mouse_x = e.getX(); mouse_y = e.getY(); System.out.println("(" + mouse_x + "," + mouse_y + ")");//マウス座標確認用 mc.repaint(); } /*** キャンバス ***/ public class MyCanvas extends Canvas { /*** 描画ステータス ***/ public void paint(Graphics g) { super.paint(g); int line_x, line_y;//ライン描画用変数 int hall_x, hall_y;//ホール描画 int i, j;//ループ用変数 Dimension size = getSize();//ウインドウのサイズを取得 Sleep s = new Sleep();//スリープ用メソッド Random rnd = new Random(); Color brown = new Color(160, 0, 0);//茶色 /*** ライフがあるとき ***/ if (life > 0) { /***** 背景色を青にする *****/ g.setColor(Color.blue); g.fillRect(0, 0, size.width, size.height); /*** ボックスの背景色を設定 ***/ g.setColor(Color.green); g.fill3DRect(100, 100, 300, 300, true); /***** 縦軸・横軸を描画*****/ line_x = 100; line_y = 100; g.setColor(Color.red); /*** 縦軸を描画 ***/ for (i = 0; i < 4; i++) { g.drawLine(line_x, 100, line_x, 400); line_x += 100; } for (i = 0; i < 4; i++) { g.drawLine(100, line_y, 400, line_y); line_y += 100; } /*** 土竜の出現するボックスを描画 ***/ g.setColor(Color.black); hall_y = 130; for (i = 0; i < 3; i++) { hall_x = 115; for (j = 0; j < 3; j++) { g.fillOval(hall_x, hall_y, 75, 40); hall_x += 100; } hall_y += 100; } do { fore_mole_point = rnd.nextInt(9); } while(mole_point == fore_mole_point); mole_point = fore_mole_point; switch(mole_point) { case 0: mole_x = 125; mole_y = 135; break; case 1: mole_x = 225; mole_y = 135; break; case 2: mole_x = 325; mole_y = 135; case 3: mole_x = 125; mole_y = 235; break; case 4: mole_x = 225; mole_y = 235; break; case 5: mole_x = 325; mole_y = 235; break; case 6: mole_x = 125; mole_y = 335; break; case 7: mole_x = 225; mole_y = 335; break; case 8: mole_x = 325; mole_y = 335; break; default: break; } g.setColor(brown); g.fillOval(mole_x, mole_y, 50, 25); if (mole_x <= mouse_x && mole_y <= mouse_y && mole_x + 50 >= mouse_x && mole_y + 25 >= mouse_y) { /*** 成功時アクション ***/ g.setColor(Color.red); g.fillOval(mole_x, mole_y, 50, 25); } s.Sleep(500); life--; } /*** ライフが無いとき ***/ if (life <= 0) { g.setColor(Color.red); g.fillRect(0, 0, size.width, size.height); g.setColor(Color.blue); g.drawString("GAME-OVER(´・ω・`)", 250, 300); } } } }
↧