/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* JMine.java
*
* Created on 24-set-2010, 16.04.17
*/
package jmine.graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import jmine.Box;
import jmine.Game;
import jmine.exceptions.IllegalNumOfMine;
/**
*
* @author Paolo
*/
public class JMine
extends javax.
swing.
JFrame {
private Game game;
private boolean startTimer;
private int w;
private int h;
public Mouse(int pH, int pW) {
this.h = pH;
this.w = pW;
}
@Override
game.unconver(this.w, this.h);
if (!startTimer) {
timer.start();
startTimer = true;
}
game.flag(this.w, this.h);
if (!startTimer) {
timer.start();
startTimer = true;
}
}
repaintGround();
if (game.isWinner()) {
JOptionPane.
showMessageDialog(panelGround,
"Hai vinto!");
timer.stop();
removeMouseListeners();
}
}
}
private int sec;
public void start() {
this.
start = new Thread(this);
this.start.start();
}
public void stop() {
this.start.stop();
this.sec = 0;
}
public void run() {
while (true) {
this.sec++;
seconds.setText("Secondi: " + this.sec);
try {
}
}
}
}
public JMine(int pWidth, int pHeight, int pNumOfMine) throws IllegalNumOfMine {
this.initComponents();
this.
setIconImage(Toolkit.
getDefaultToolkit().
getImage(getClass
().
getResource("/jmine/graphics/images/mina_piccola.png")));
this.
timer = new Timer();
this.
box = new JLabel[pHeight
][pWidth
];
this.game = new Game(pWidth, pHeight, pNumOfMine);
this.
panelGround = new JPanel();
this.
panelGround.
setLayout(new GridLayout(pHeight, pWidth
));
this.panelGround.setSize(pWidth * 30, pHeight * 30);
this.panelGround.setLocation(25, 30);
this.panel.add(this.panelGround);
this.initBox();
}
public void addMouseListeners() {
for (int i = 0; i < this.game.getHeight(); i++) {
for (int j = 0; j < this.game.getWidth(); j++) {
for (int x = 0; x < ml.length; x++) {
this.box[i][j].removeMouseListener(ml[x]);
}
this.box[i][j].addMouseListener(new Mouse(j, i));
}
}
}
public void removeMouseListeners(){
for (int i = 0; i < this.game.getHeight(); i++) {
for (int j = 0; j < this.game.getWidth(); j++) {
for (int x = 0; x < ml.length; x++) {
this.box[i][j].removeMouseListener(ml[x]);
}
}
}
}
private void repaintGround() {
Box[][] ground
= this.
game.
getGround();
for (int i = 0; i < this.game.getHeight(); i++) {
for (int j = 0; j < this.game.getWidth(); j++) {
this.box[i][j].setText("");
this.
box[i
][j
].
setBackground(new Color(214, 217, 223
));
this.box[i][j].setIcon(null);
if (this.game.getGameOver()) {
this.box[i][j].removeMouseListener(this.box[i][j].getMouseListeners()[0]);
if (ground[i][j].isMine()) {
this.
box[i
][j
].
setIcon(new ImageIcon(getClass
().
getResource("/jmine/graphics/images/mina_piccola.png")));
this.
box[i
][j
].
setBackground(Color.
RED);
}
this.timer.stop();
this.repaint();
}
if (!ground[i][j].isCover()) {
if (ground[i][j].getNumOfMine() != 0) {
this.box[i][j].setText(ground[i][j].getNumOfMine() + "");
}
} else if (ground[i][j].isFlagged()) {
this.
box[i
][j
].
setIcon(new ImageIcon(getClass
().
getResource("/jmine/graphics/images/flag.png")));
} else if(ground[i][j].isFlagged() && !ground[i][j].isMine()){
this.box[i][j].setIcon(null);
}
}
}
}
private void initBox() {
for (int i = 0; i < this.game.getHeight(); i++) {
for (int j = 0; j < this.game.getWidth(); j++) {
this.
box[i
][j
] = new JLabel();
this.box[i][j].setSize(30, 30);
this.box[i][j].addMouseListener(new Mouse(j, i));
this.
box[i
][j
].
setHorizontalAlignment(JLabel.
CENTER);
this.
box[i
][j
].
setFont(new Font("Tempus Sans ITC",
Font.
BOLD, 15
));
this.box[i][j].setOpaque(true);
this.panelGround.add(this.box[i][j]);
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
panel
= new javax.
swing.
JPanel();
seconds
= new javax.
swing.
JLabel();
newGameButton
= new javax.
swing.
JButton();
setTitle("JMine - by Paolo Ricciuti (C)");
setResizable(false);
seconds.
setFont(new java.
awt.
Font("Tempus Sans ITC", 0, 24
)); // NOI18N
seconds.setText("Secondi: 0");
newGameButton.
setFont(new java.
awt.
Font("Tempus Sans ITC", 0, 14
)); // NOI18N
newGameButton.setText("Nuovo gioco");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
newGameButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(newGameButton)
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED, 163,
Short.
MAX_VALUE)
.addComponent(seconds)
.addContainerGap())
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelLayout.createSequentialGroup()
.
addContainerGap(256,
Short.
MAX_VALUE)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(seconds)
.addComponent(newGameButton))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addComponent(panel, javax.
swing.
GroupLayout.
DEFAULT_SIZE, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addComponent(panel, javax.
swing.
GroupLayout.
DEFAULT_SIZE, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void newGameButtonActionPerformed
(java.
awt.
event.
ActionEvent evt
) {//GEN-FIRST:event_newGameButtonActionPerformed
try {
this.timer.stop();
}
this.startTimer = false;
this.game.newGame();
this.repaintGround();
this.addMouseListeners();
this.seconds.setText("Secondi: 0");
}//GEN-LAST:event_newGameButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.
swing.
JButton newGameButton
;
private javax.
swing.
JPanel panel
;
private javax.
swing.
JLabel seconds
;
// End of variables declaration//GEN-END:variables
}