-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineFrame.java
More file actions
331 lines (282 loc) · 7.67 KB
/
MineFrame.java
File metadata and controls
331 lines (282 loc) · 7.67 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package mine;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.RepaintManager;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import mine.gfx.MineGraphics;
import mine.gfx.MineGraphics.Emoticon;
import mine.gfx.MineGraphics.Picture;
public class MineFrame extends JFrame {
public static boolean DEBUG = false;
private static MineFrame INSTANCE;
private int rows;
private int columns;
private int numberOfBombs;
private Mode mode;
private boolean gameOver = false;
private boolean marker = true;
private boolean colored = true;
private boolean firstMove = true;
private boolean wrapField = false;
private final MineLabel bombsLabel;
private final MineLabel timeLabel;
private final JButton button;
private MineCustomStarter dialog;
private MineStatistics stats;
private MineField field;
private final Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getTimeLabel().incValue();
}
});
public static MineFrame getInstance() {
if (INSTANCE == null) {
INSTANCE = new MineFrame("MineSweeper");
}
return INSTANCE;
}
public final void setColored(boolean colored) {
this.colored = colored;
repaint();
}
private MineFrame(String title) {
super(title);
final MineMenu menu = new MineMenu();
setJMenuBar(menu);
final JPanel contentPane = new JPanel(new BorderLayout()) {
private BufferedImage image;
@Override
public void paint(Graphics g) {
if (colored)
super.paint(g);
else {
if (image == null || image.getWidth() != getWidth() || image.getHeight() != getHeight()) {
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_BYTE_GRAY);
}
Graphics2D imgGraphics = image.createGraphics();
super.paint(imgGraphics);
imgGraphics.dispose();
g.drawImage(image, 0, 0, null);
}
}
};
final JComponent headerCenter = Box.createHorizontalBox();
headerCenter.add(Box.createHorizontalStrut(5));
headerCenter.add(bombsLabel = new MineLabel(numberOfBombs));
headerCenter.add(Box.createHorizontalGlue());
button = new JButton();
button.setPreferredSize(new Dimension(MineGraphics.Emoticon.DIMENSION, MineGraphics.Emoticon.DIMENSION));
button.setPressedIcon(MineGraphics.get(Emoticon.WANT));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newGame();
}
});
headerCenter.add(button);
headerCenter.add(Box.createHorizontalGlue());
headerCenter.add(timeLabel = new MineLabel(0));
headerCenter.add(Box.createHorizontalStrut(5));
final JComponent header = Box.createVerticalBox();
header.add(Box.createVerticalStrut(5));
header.add(headerCenter);
header.add(Box.createVerticalStrut(5));
header.setBorder(BorderFactory.createLoweredBevelBorder());
contentPane.add(header, BorderLayout.NORTH);
contentPane.setBorder(BorderFactory.createRaisedBevelBorder());
setContentPane(contentPane);
setResizable(false);
setIconImage(MineGraphics.get(Picture.BOMB));
newGame(9, 9, 10);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@Override
public void dispose() {
stopTimer();
super.dispose();
}
public void newGame() {
newGame(columns, rows, numberOfBombs);
}
public void newGame(Mode mode) {
setMode(mode);
switch (mode) {
case BEGINNER:
newGame(9, 9, 10);
break;
case ADVANCED:
newGame(16, 16, 40);
break;
case PROS:
newGame(30, 16, 99);
break;
}
}
public void newGame(final int width, final int height, final int bombCount) {
final MineField newField = new MineField(columns = width, rows = height, numberOfBombs = bombCount);
if (field != null)
getContentPane().remove(field);
stopTimer();
setFirstMove(true);
setGameOver(false);
getContentPane().add(field = newField, BorderLayout.CENTER);
getBombsLabel().setValue(bombCount);
getTimeLabel().setValue(0);
getButton().setIcon(MineGraphics.get(Emoticon.SMILE));
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
getStatistics().refresh(field);
}
});
if (DEBUG) {
System.out.println(field.toString());
System.out.println();
}
validate();
pack();
newField.getComponent(0).requestFocusInWindow();
}
@Override
public void pack() {
super.pack();
relocate();
}
private void relocate() {
final Point location = getLocation();
final Dimension size = getSize();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final Insets insets = toolkit.getScreenInsets(getGraphicsConfiguration());
final int x;
final int diffX = screenSize.width - size.width - insets.left - insets.right;
if (location.x > diffX)
x = Math.max(diffX, 0);
else
x = location.x;
final int y;
final int diffY = screenSize.height - size.height - insets.top - insets.bottom;
if (location.y > diffY)
y = Math.max(diffY, 0);
else
y = location.y;
setLocation(x, y);
}
public final int getRows() {
return rows;
}
public final int getColumns() {
return columns;
}
public final int getNumberOfBombs() {
return numberOfBombs;
}
public final MineCustomStarter getDialog() {
if (dialog == null) {
dialog = new MineCustomStarter();
}
return dialog;
}
public final MineStatistics getStatistics() {
if (stats == null) {
stats = new MineStatistics();
}
return stats;
}
public final JButton getButton() {
return button;
}
public final MineLabel getBombsLabel() {
return bombsLabel;
}
public final MineLabel getTimeLabel() {
return timeLabel;
}
@Override
public MineMenu getJMenuBar() {
return (MineMenu) super.getJMenuBar();
}
public boolean isGameOver() {
return gameOver;
}
public final void setGameOver(boolean gameOver) {
this.gameOver = gameOver;
}
public final boolean hasMarker() {
return marker;
}
public final void setMarker(boolean marker) {
this.marker = marker;
}
public final boolean isFirstMove() {
return firstMove;
}
public final void setFirstMove(boolean firstMove) {
this.firstMove = firstMove;
}
public final boolean isWrapField() {
return wrapField;
}
public final void setWrapField(boolean wrapField) {
this.wrapField = wrapField;
}
public final Mode getMode() {
return mode;
}
public final void setMode(Mode mode) {
this.mode = mode;
}
public enum Mode {
BEGINNER, ADVANCED, PROS, CUSTOM;
}
public void stopTimer() {
timer.stop();
}
public void startTimer() {
if (!timer.isRunning())
timer.start();
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
RepaintManager.setCurrentManager(new RepaintManager() {
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
JComponent r = c.getRootPane();
if (r != null) {
super.addDirtyRegion(r, 0, 0, r.getWidth(), r.getHeight());
} else {
super.addDirtyRegion(c, 0, 0, w, h);
}
}
});
final MineFrame frame = getInstance();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}