finish studying java event processing ...... The teacher asked to write a beggar version of mine sweeping ...... As a front end dog You can't be decisive , therefore ......
I wrote it hard , Refer to the following three codes :
Post the code first , Everything that needs attention is written in the notes , Write out the tutorial when you have time
package sixth;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MineClearance {
static private int midtime = 3600,mineNum = 0;/* Countdown time and number of flags available */
private static ImageIcon face = new
ImageIcon("E:\\project\\JAVA\\test\\demo\\face.jpg");/* Little yellow face icon */
static private JLabel label1,label2;/* Prompt text */
static private GamePanel gp;/* Minefield */
MineClearance(){
/* Draw window */
JFrame f = new JFrame(" mine clearance ");
f.setBounds(600,200,500,600);
f.setDefaultCloseOperation(3);
f.setLayout(null);
label1 = new JLabel(" Remaining time :" +(midtime / 60 / 60 % 60) + ":"+ (midtime / 60 %
60)+ ":" +(midtime % 60));
label1.setBounds(10,20,120,20);
f.add(label1);
/* Number of flags displayed */
label2 = new JLabel(" surplus :"+mineNum);
label2.setBounds(400,20,120,20);
f.add(label2);
/* Reset button */
JButton bt = new JButton(face);
bt.setBounds(230, 15,30,30);
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.dispose();
midtime = 3600;
new MineClearance();
}
});
f.add(bt);
/* Mapping minefields */
gp = new GamePanel(20,20);
gp.setBounds(40,100,400,400);
f.add(gp);
/* Display interface */
f.setVisible(true);
}
/* Countdown thread */
static class CountDown extends Thread{
public void run(){
while (midtime > 0){
try{
-- midtime;
label1.setText(" Remaining time :" +(midtime / 60 / 60 % 60) + ":"+ (midtime / 60 % 60)+
":" +(midtime % 60));
this.sleep(1000);
}catch (Exception e){
System.out.println(" error :" + e.toString());
}
}
if(midtime == 0) {
gp.showBomb();
JOptionPane.showMessageDialog(null," Time is up. "," game over ",JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String[] args){
new MineClearance();
/* count down */
CountDown cd = new CountDown();
cd.start();
}
/* Modify the number of flags */
public static void setMineNum(int i){
mineNum = i;
label2.setText(" surplus :"+mineNum);
}
}
class GamePanel extends JPanel {
private int rows, cols, bombCount,flagNum;
private final int BLOCKWIDTH = 20;
private final int BLOCKHEIGHT = 20;
private JLabel[][] label;
private boolean[][] state;
private Btn[][] btns;
private byte[][] click;
private static ImageIcon flag = new
ImageIcon("E:\\project\\JAVA\\test\\demo\\flag.jpg");
private static ImageIcon bomb = new
ImageIcon("E:\\project\\JAVA\\test\\demo\\bomb.jpg");
private static ImageIcon lucency = new
ImageIcon("E:\\project\\JAVA\\test\\demo\\lucency.png");
/* Tectonic minefield */
public GamePanel(int row, int col) {
rows = row;/* Number of rows */
cols = col;/* Number of columns */
bombCount = rows * cols / 10; /* Number of Mines */
flagNum = bombCount;/* Number of tags ( For flag insertion ) */
label = new JLabel[rows][cols];
state = new boolean[rows][cols];/* Used to store whether there are mines */
btns = new Btn[rows][cols];
click = new byte[rows][cols];/* Used to store button click status (0- Not clicked ,1- Clicked ,2- Not clicked but surrounded by thunder ,3- Flag insertion ) */
MineClearance.setMineNum(flagNum);
setLayout(null);
initLable();
randomBomb();
writeNumber();
randomBtn();
}
public void initLable() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JLabel l = new JLabel("", JLabel.CENTER);
// Set the boundary of each small square
l.setBounds(j * BLOCKWIDTH, i * BLOCKHEIGHT, BLOCKWIDTH, BLOCKHEIGHT);
// Draw a grid border
l.setBorder(BorderFactory.createLineBorder(Color.GRAY));
// Set the grid to transparent , Easy for us to fill color
l.setOpaque(true);
// The background is filled with yellow
l.setBackground(Color.lightGray);
// Add the grid to the container ( Panel JPanel)
this.add(l);
// Save the square in the class variable , Convenient for public use
label[i][j] = l;
label[i][j].setVisible(false);
}
}
}
/* Mapping mines */
private void randomBomb() {
for (int i = 0; i < bombCount; i++) {
int rRow = (int) (Math.random() * rows);
int rCol = (int) (Math.random() * cols);
label[rRow][rCol].setIcon(bomb);
state[rRow][rCol] = true;/* A lattice with mines state For true */
}
}
/* Draw numbers */
private void writeNumber() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (state[i][j]) {
continue;
}
int bombCount = 0;
/* Find the number of mines in nine self centered grids */
for (int r = -1; (r + i < rows) && (r < 2); ++r) {
if (r + i < 0) continue;
for (int c = -1; (c + j < cols) && (c < 2); ++c) {
if (c + j < 0) continue;
if (state[r + i][c + j]) ++bombCount;
}
}
if (bombCount > 0) {
click[i][j] = 2;
label[i][j].setText(String.valueOf(bombCount));
}
}
}
}
/* Draw button */
private void randomBtn() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
Btn btn = new Btn();
btn.i = i;
btn.j = j;
btn.setBounds(j * BLOCKWIDTH, i * BLOCKHEIGHT, BLOCKWIDTH, BLOCKHEIGHT);
this.add(btn);
btns[i][j] = btn;
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
/* Left click */
if(e.getButton() == MouseEvent.BUTTON1) open(btn);
/* Right click */
if(e.getButton() == MouseEvent.BUTTON3) placeFlag(btn);
}
}
);
}
}
}
/* Open this minefield */
private void open(Btn b){
/* Step on thunder */
if(state[b.i][b.j]){
for (int r = 0;r < rows;++r){
for(int c = 0;c < cols; ++c){
btns[r][c].setVisible(false);/* hide label */
label[r][c].setVisible(true);/* Display button ( Here, you can only display the button below the button if the button is hidden label) */
}
}
JOptionPane.showMessageDialog(null," You failed "," game over ",JOptionPane.PLAIN_MESSAGE);
}else /* No thunder */{
subopen(b);
}
}
/* Recursive opening of surrounding minefields */
private void subopen(Btn b){
/* Have thunder , Cannot open */
if(state[b.i][b.j]) return;
/* Opened and flagged , Don't open it */
if(click[b.i][b.j] == 1 || click[b.i][b.j] == 4) return;
/* There's thunder around , Just open it */
if(click[b.i][b.j] == 2) {
b.setVisible(false);
label[b.i][b.j].setVisible(true);
click[b.i][b.j] = 1;
return;
}
/* Open the current button */
b.setVisible(false);
label[b.i][b.j].setVisible(true);
click[b.i][b.j] = 1;
/* Recursive detection of eight surrounding buttons */
for (int r = -1; (r + b.i < rows) && (r < 2); ++r) {
if (r + b.i < 0) continue;
for (int c = -1; (c + b.j < cols) && (c < 2); ++c) {
if (c + b.j < 0) continue;
if (r==0 && c==0) continue;
Btn newbtn = btns[r + b.i][c + b.j];
subopen(newbtn);
}
}
}
/* Flag insertion */
private void placeFlag(Btn b){
/* Only the same number of flags as the number of mines can be inserted */
if(flagNum>0){
/* Flagged , Click again to cancel */
if(click[b.i][b.j] == 3){
if(label[b.i][b.j].getText() == "[0-9]") click[b.i][b.j] = 2;
else click[b.i][b.j] = 0;
b.setIcon(lucency);
++ flagNum;
MineClearance.setMineNum(flagNum);
}else /* UN flagged , Flag insertion */{
b.setIcon(flag);
click[b.i][b.j] = 3;
-- flagNum;
MineClearance.setMineNum(flagNum);
}
/* Put all the flags in , Is the test successful */
if(flagNum == 0){
boolean flagstate = true;
for(int i = 0;i < rows; ++i){
for(int j = 0;j < cols; ++j){
if (click[i][j] != 3 && state[i][j]) flagstate = false;
}
}
if(flagstate)
JOptionPane.showMessageDialog(null," You succeeded "," game over ",JOptionPane.PLAIN_MESSAGE);
}
}else /* We've run out of flags , Can't plug in */{
JOptionPane.showMessageDialog(null," Tag exhausted "," Wrong operation ",JOptionPane.PLAIN_MESSAGE);
}
}
/* Display minefield */
public void showBomb(){
for (int r = 0;r < rows;++r){
for(int c = 0;c < cols; ++c){
btns[r][c].setVisible(false);/* hide label */
label[r][c].setVisible(true);/* Display button ( Here, you can only display the button below the button if the button is hidden label) */
}
}
}
}
class Btn extends JButton{
public int i,j;
}
Materials needed :
bomb.jpg -- For bomb display
face.jpg -- Cool reset button in the middle of the top
flag.jpg -- Flag inserted
luncecy.png -- You can't see anything ? That's right , This is a pure transparent picture , Used to replace the flag with transparent when right clicking the button with a flag again
Technology