-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineSweeper.java
More file actions
135 lines (100 loc) · 3.6 KB
/
MineSweeper.java
File metadata and controls
135 lines (100 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MineSweeper extends Frame implements MouseListener, ActionListener {
private static JFrame appFrame;
public static MineSweeperJComponent mineSweeperJComponent;
public static Timer timer;
public static int count;
MineSweeper() {}
public static void main(String[] args) {
//creating the graphics frames
appFrame = new JFrame();
//creating the button
JButton loseButton = new JButton("Play Again");
loseButton.setBounds(400,400,95,30);
appFrame.add(loseButton);
//class where I can change the image
MineSweeper m = new MineSweeper();
appFrame.addMouseListener(m);
mineSweeperJComponent = new MineSweeperJComponent();
//action a lose button
loseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(m, loseButton.getText());
MineSweeperJComponent.setBoard();
appFrame.repaint();
}
});
//Timer for flags
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
timer.setRepeats(false);
//using clicking things
mineSweeperJComponent.setBoard();
//size, title, close on close
appFrame.setSize(600,500);
appFrame.setTitle("MineSweeper");
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//adding from my mineSweeperJComponent class
appFrame.add(mineSweeperJComponent);
appFrame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int row = (y-40)/20;
int col = (x-17)/20;
System.out.print("("+ x +", ");
System.out.println( y + ")");
System.out.println("Row: " + row + " Col: " + col);
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if((x >= 17 && x < 157) && (y >= 40 && y < 180)){
appFrame.repaint(); //It is updating!! YAY
MineSweeperJComponent.updateBoard(row, col);
//if you click a bomb aka a 10 you lose.
if(Board.getIndex(row,col) == 10){
appFrame.repaint();
}
}
}
});
timer.setRepeats(false);
timer.restart();
}
@Override
public void mousePressed(MouseEvent e) {
int clicks = e.getClickCount();
int x = e.getX();
int y = e.getY();
int row = (y-40)/20;
int col = (x-17)/20;
if(clicks == 2)
if ((x >= 17 && x < 157) && (y >= 40 && y < 180)) {
appFrame.repaint(); //It is updating!! YAY
MineSweeperJComponent.board.putFlag(row, col);
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
}
}