Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JMine - JMine.java

JMine.java

Caricato da:
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * JMine.java
  8.  *
  9.  * Created on 24-set-2010, 16.04.17
  10.  */
  11. package jmine.graphics;
  12.  
  13. import java.awt.Color;
  14. import java.awt.Font;
  15. import java.awt.GridLayout;
  16. import java.awt.Toolkit;
  17. import java.awt.event.MouseAdapter;
  18. import java.awt.event.MouseEvent;
  19. import java.awt.event.MouseListener;
  20. import javax.swing.ImageIcon;
  21. import javax.swing.JLabel;
  22. import javax.swing.JOptionPane;
  23. import javax.swing.JPanel;
  24. import javax.swing.border.LineBorder;
  25. import javax.swing.border.SoftBevelBorder;
  26. import jmine.Box;
  27. import jmine.Game;
  28. import jmine.exceptions.IllegalNumOfMine;
  29.  
  30. /**
  31.  *
  32.  * @author Paolo
  33.  */
  34. public class JMine extends javax.swing.JFrame {
  35.  
  36.     private Game game;
  37.     private JLabel[][] box;
  38.     private JPanel panelGround;
  39.     private boolean startTimer;
  40.     private Timer timer;
  41.  
  42.     private class Mouse extends MouseAdapter {
  43.  
  44.         private int w;
  45.         private int h;
  46.  
  47.         public Mouse(int pH, int pW) {
  48.             this.h = pH;
  49.             this.w = pW;
  50.         }
  51.  
  52.         @Override
  53.         public void mouseClicked(MouseEvent e) {
  54.             if (e.getButton() == MouseEvent.BUTTON1) {
  55.                 game.unconver(this.w, this.h);
  56.                 if (!startTimer) {
  57.                     timer.start();
  58.                     startTimer = true;
  59.                 }
  60.             } else if (e.getButton() == MouseEvent.BUTTON3) {
  61.                 game.flag(this.w, this.h);
  62.                 if (!startTimer) {
  63.                     timer.start();
  64.                     startTimer = true;
  65.                 }
  66.             }
  67.             repaintGround();
  68.             if (game.isWinner()) {
  69.                 JOptionPane.showMessageDialog(panelGround, "Hai vinto!");
  70.                 timer.stop();
  71.                 removeMouseListeners();
  72.             }
  73.         }
  74.     }
  75.  
  76.     private class Timer implements Runnable {
  77.  
  78.         private int sec;
  79.         private Thread start;
  80.  
  81.         public void start() {
  82.             this.start = new Thread(this);
  83.             this.start.start();
  84.         }
  85.  
  86.         public void stop() {
  87.             this.start.stop();
  88.             this.sec = 0;
  89.         }
  90.  
  91.         public void run() {
  92.             while (true) {
  93.                 this.sec++;
  94.                 seconds.setText("Secondi: " + this.sec);
  95.                 try {
  96.                     Thread.sleep(1000);
  97.                 } catch (InterruptedException ex) {
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     public JMine(int pWidth, int pHeight, int pNumOfMine) throws IllegalNumOfMine {
  104.         this.initComponents();
  105.         this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/jmine/graphics/images/mina_piccola.png")));
  106.         this.timer = new Timer();
  107.         this.box = new JLabel[pHeight][pWidth];
  108.         this.game = new Game(pWidth, pHeight, pNumOfMine);
  109.         this.panelGround = new JPanel();
  110.         this.panelGround.setLayout(new GridLayout(pHeight, pWidth));
  111.         this.panelGround.setSize(pWidth * 30, pHeight * 30);
  112.         this.panelGround.setLocation(25, 30);
  113.         this.panel.add(this.panelGround);
  114.         this.initBox();
  115.     }
  116.  
  117.     public void addMouseListeners() {
  118.         for (int i = 0; i < this.game.getHeight(); i++) {
  119.             for (int j = 0; j < this.game.getWidth(); j++) {
  120.                 MouseListener[] ml = this.box[i][j].getMouseListeners();
  121.                 for (int x = 0; x < ml.length; x++) {
  122.                     this.box[i][j].removeMouseListener(ml[x]);
  123.                 }
  124.                 this.box[i][j].addMouseListener(new Mouse(j, i));
  125.             }
  126.         }
  127.     }
  128.  
  129.     public void removeMouseListeners(){
  130.         for (int i = 0; i < this.game.getHeight(); i++) {
  131.             for (int j = 0; j < this.game.getWidth(); j++) {
  132.                 MouseListener[] ml = this.box[i][j].getMouseListeners();
  133.                 for (int x = 0; x < ml.length; x++) {
  134.                     this.box[i][j].removeMouseListener(ml[x]);
  135.                 }
  136.             }
  137.         }
  138.     }
  139.  
  140.     private void repaintGround() {
  141.         Box[][] ground = this.game.getGround();
  142.         for (int i = 0; i < this.game.getHeight(); i++) {
  143.             for (int j = 0; j < this.game.getWidth(); j++) {
  144.                 this.box[i][j].setText("");
  145.                 this.box[i][j].setBackground(new Color(214, 217, 223));
  146.                 this.box[i][j].setIcon(null);
  147.                 this.box[i][j].setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
  148.                 if (this.game.getGameOver()) {
  149.                     this.box[i][j].removeMouseListener(this.box[i][j].getMouseListeners()[0]);
  150.                     if (ground[i][j].isMine()) {
  151.                         this.box[i][j].setIcon(new ImageIcon(getClass().getResource("/jmine/graphics/images/mina_piccola.png")));
  152.                         this.box[i][j].setBackground(Color.RED);
  153.                     }
  154.                     this.timer.stop();
  155.                     this.repaint();
  156.                 }
  157.                 if (!ground[i][j].isCover()) {
  158.                     if (ground[i][j].getNumOfMine() != 0) {
  159.                         this.box[i][j].setText(ground[i][j].getNumOfMine() + "");
  160.                     }
  161.                     this.box[i][j].setBorder(new LineBorder(Color.WHITE));
  162.                 } else if (ground[i][j].isFlagged()) {
  163.                     this.box[i][j].setIcon(new ImageIcon(getClass().getResource("/jmine/graphics/images/flag.png")));
  164.                 } else if(ground[i][j].isFlagged() && !ground[i][j].isMine()){
  165.                     this.box[i][j].setIcon(null);
  166.                     this.box[i][j].setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
  167.                 }
  168.  
  169.             }
  170.         }
  171.     }
  172.  
  173.     private void initBox() {
  174.         for (int i = 0; i < this.game.getHeight(); i++) {
  175.             for (int j = 0; j < this.game.getWidth(); j++) {
  176.                 this.box[i][j] = new JLabel();
  177.                 this.box[i][j].setSize(30, 30);
  178.                 this.box[i][j].setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
  179.                 this.box[i][j].addMouseListener(new Mouse(j, i));
  180.                 this.box[i][j].setHorizontalAlignment(JLabel.CENTER);
  181.                 this.box[i][j].setFont(new Font("Tempus Sans ITC", Font.BOLD, 15));
  182.                 this.box[i][j].setOpaque(true);
  183.                 this.panelGround.add(this.box[i][j]);
  184.             }
  185.         }
  186.     }
  187.  
  188.     /** This method is called from within the constructor to
  189.      * initialize the form.
  190.      * WARNING: Do NOT modify this code. The content of this method is
  191.      * always regenerated by the Form Editor.
  192.      */
  193.     @SuppressWarnings("unchecked")
  194.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  195.     private void initComponents() {
  196.  
  197.         panel = new javax.swing.JPanel();
  198.         seconds = new javax.swing.JLabel();
  199.         newGameButton = new javax.swing.JButton();
  200.  
  201.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  202.         setTitle("JMine - by Paolo Ricciuti (C)");
  203.         setResizable(false);
  204.  
  205.         seconds.setFont(new java.awt.Font("Tempus Sans ITC", 0, 24)); // NOI18N
  206.         seconds.setText("Secondi: 0");
  207.  
  208.         newGameButton.setFont(new java.awt.Font("Tempus Sans ITC", 0, 14)); // NOI18N
  209.         newGameButton.setText("Nuovo gioco");
  210.         newGameButton.addActionListener(new java.awt.event.ActionListener() {
  211.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  212.                 newGameButtonActionPerformed(evt);
  213.             }
  214.         });
  215.  
  216.         javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
  217.         panel.setLayout(panelLayout);
  218.         panelLayout.setHorizontalGroup(
  219.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  220.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelLayout.createSequentialGroup()
  221.                 .addContainerGap()
  222.                 .addComponent(newGameButton)
  223.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
  224.                 .addComponent(seconds)
  225.                 .addContainerGap())
  226.         );
  227.         panelLayout.setVerticalGroup(
  228.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  229.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelLayout.createSequentialGroup()
  230.                 .addContainerGap(256, Short.MAX_VALUE)
  231.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  232.                     .addComponent(seconds)
  233.                     .addComponent(newGameButton))
  234.                 .addContainerGap())
  235.         );
  236.  
  237.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  238.         getContentPane().setLayout(layout);
  239.         layout.setHorizontalGroup(
  240.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  241.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  242.         );
  243.         layout.setVerticalGroup(
  244.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  245.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  246.         );
  247.  
  248.         pack();
  249.     }// </editor-fold>//GEN-END:initComponents
  250.  
  251.     private void newGameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newGameButtonActionPerformed
  252.         try {
  253.             this.timer.stop();
  254.         } catch (Exception e) {
  255.         }
  256.         this.startTimer = false;
  257.         this.game.newGame();
  258.         this.repaintGround();
  259.         this.addMouseListeners();
  260.         this.seconds.setText("Secondi: 0");
  261.     }//GEN-LAST:event_newGameButtonActionPerformed
  262.     // Variables declaration - do not modify//GEN-BEGIN:variables
  263.     private javax.swing.JButton newGameButton;
  264.     private javax.swing.JPanel panel;
  265.     private javax.swing.JLabel seconds;
  266.     // End of variables declaration//GEN-END:variables
  267. }