Advertisement
cd62131

AWT Bomb Game

Jan 30th, 2014
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.76 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Button;
  3. import java.awt.Dialog;
  4. import java.awt.Frame;
  5. import java.awt.Label;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10. import java.util.Random;
  11.  
  12. public class Bomb {
  13.   public static final String NAME = "爆弾カウントダウン";
  14.   private Frame window;
  15.   private Dialog dialog;
  16.   private int count;
  17.   private int turn;
  18.  
  19.   public Bomb() {
  20.     init_and_run();
  21.   }
  22.  
  23.   private void init_and_run() {
  24.     Random r = new Random();
  25.     count = r.nextInt(30) + 1;
  26.     turn = 0;
  27.     window = new Frame(NAME);
  28.     window.setIgnoreRepaint(true);
  29.     window.setResizable(true);
  30.     window.add(new Label("カウントを進めてください"));
  31.     Button b;
  32.     window.add(b = new Button("    −1     "), BorderLayout.WEST);
  33.     b.addMouseListener(new MouseAdapter() {
  34.       @Override
  35.       public void mouseClicked(MouseEvent e) {
  36.         turn++;
  37.         window.add(new Label("" + turn + "回"));
  38.         decrease_count();
  39.       }
  40.     });
  41.     window.add(b = new Button("     −2     "), BorderLayout.EAST);
  42.     b.addMouseListener(new MouseAdapter() {
  43.       @Override
  44.       public void mouseClicked(MouseEvent e) {
  45.         turn++;
  46.         decrease_count();
  47.         decrease_count();
  48.       }
  49.     });
  50.     window.add(b = new Button("Exit"), BorderLayout.SOUTH);
  51.     b.addMouseListener(new MouseAdapter() {
  52.       @Override
  53.       public void mouseClicked(MouseEvent e) {
  54.         quit();
  55.       }
  56.     });
  57.     window.addWindowListener(new WindowAdapter() {
  58.       @Override
  59.       public void windowClosing(WindowEvent e) {
  60.         quit();
  61.       }
  62.     });
  63.     window.pack();
  64.     window.setVisible(true);
  65.     window.requestFocus();
  66.   }
  67.  
  68.   private void decrease_count() {
  69.     count--;
  70.     if (count < 0) bomb();
  71.   }
  72.  
  73.   private void bomb() {
  74.     dialog = new Dialog(window, "BOMB!", true);
  75.     dialog.addWindowListener(new WindowAdapter() {
  76.       @Override
  77.       public void windowClosing(WindowEvent e) {
  78.         quit();
  79.         dialog_quit();
  80.       }
  81.     });
  82.     Button b;
  83.     dialog.add(b = new Button("Exit"), BorderLayout.CENTER);
  84.     b.addMouseListener(new MouseAdapter() {
  85.       @Override
  86.       public void mouseClicked(MouseEvent e) {
  87.         quit();
  88.         dialog_quit();
  89.       }
  90.     });
  91.     dialog.add(new Label("BOOOOMB!!!!! " + turn + " 回"), BorderLayout.NORTH);
  92.     dialog.pack();
  93.     dialog.setVisible(true);
  94.   }
  95.  
  96.   public void quit() {
  97.     window.setVisible(false);
  98.     window.dispose();
  99.   }
  100.  
  101.   public void dialog_quit() {
  102.     dialog.setVisible(false);
  103.     dialog.dispose();
  104.   }
  105.  
  106.   public static void main(String[] args) {
  107.     Bomb b = new Bomb();
  108.     b.quit();
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement