-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeGUI.java
More file actions
463 lines (415 loc) · 13.6 KB
/
MazeGUI.java
File metadata and controls
463 lines (415 loc) · 13.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package maze;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JCheckBox;
import javax.swing.WindowConstants;
import graph.GraphAlgorithmObserver;
import graph.MazeGraph;
/**
* <P>The MazeGUI class allows the user to create
* and display mazes. It relies on an underlying
* MazeGraph, which stores the maze as a Graph, and
* can perform various algorithms (DFS, BFS, and
* Dijsktra.)</P>
*/
public class MazeGUI {
private Maze maze;
private MazeGraph theGraph;
/* Containers */
private JPanel contentPane;
private JFrame window;
private JPanel controlsPanel;
private JPanel mazePanel;
/* Widgets */
private JButton drawButton;
private JLabel widthLabel, heightLabel;
private JFormattedTextField widthBox, heightBox;
private JButton DFSButton;
private JButton BFSButton;
private JButton DijkstraButton;
private JSlider speedSlider;
private JSlider densitySlider;
private JCheckBox showWeights;
/* GUI style */
private final Color WALL_COLOR = Color.BLACK;
private static int STROKE_WIDTH;
private final Color[] squareColors = {Color.GRAY, Color.RED, Color.GREEN, Color.BLUE, Color.PINK,
Color.YELLOW, Color.CYAN, Color.MAGENTA, Color.ORANGE, Color.WHITE, Color.BLACK};
/* Used during searches */
private int[][] colorIndex;
private Collection<Juncture> coloredJunctures;
private boolean isDFS;
/** Creates and displays the GUI; constructs
* a maze to start things off.
*/
public MazeGUI() {
createAndDisplayGUI();
makeNewMaze();
}
private void createAndDisplayGUI() {
widthLabel = new JLabel("Width");
heightLabel = new JLabel("Height");
drawButton = new JButton("New Maze");
DijkstraButton = new JButton("Dijkstra");
densitySlider = new JSlider();
DFSButton = new JButton("DFS");
BFSButton = new JButton("BFS");
showWeights = new JCheckBox("Weights");
drawButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
makeNewMaze();
speedSlider.requestFocusInWindow();
}
});
DijkstraButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
changeButtonStatus(false);
showWeights.setSelected(true);
Thread thread = new Thread(new Runnable() {
public void run() {
theGraph.DoDijsktra(new Juncture(0,0), new Juncture(maze.getMazeWidth() - 1, maze.getMazeHeight() - 1));
}
});
thread.start();
}
});
DFSButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
showWeights.setSelected(false);
changeButtonStatus(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
theGraph.DoDFS(new Juncture(0,0),
new Juncture(maze.getMazeWidth() - 1, maze.getMazeHeight() - 1));
}
});
thread.start();
}
});
BFSButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
changeButtonStatus(false);
showWeights.setSelected(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
theGraph.DoBFS(new Juncture(0,0),
new Juncture(maze.getMazeWidth() - 1, maze.getMazeHeight() - 1));
}
});
thread.start();
}
});
showWeights.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mazePanel.updateUI();
}
});
speedSlider = new JSlider();
speedSlider.setValue(40);
NumberFormat numberFormat = NumberFormat.getIntegerInstance();
numberFormat.setMaximumFractionDigits(0);
widthBox = new JFormattedTextField(numberFormat);
widthBox.setColumns(3);
widthBox.setText("60");
widthBox.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent arg0) {
widthBox.setText("");
}
@Override
public void focusLost(FocusEvent e) {
}
});
heightBox = new JFormattedTextField(numberFormat);
heightBox.setColumns(3);
heightBox.setText("30");
heightBox.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent arg0) {
heightBox.setText("");
}
@Override
public void focusLost(FocusEvent e) {
}
});
controlsPanel = new JPanel();
controlsPanel.add(widthLabel);
controlsPanel.add(widthBox);
controlsPanel.add(heightLabel);
controlsPanel.add(heightBox);
controlsPanel.add(new JLabel(" Density"));
densitySlider.setValue(100);
controlsPanel.add(densitySlider);
controlsPanel.add(drawButton);
controlsPanel.add(showWeights);
controlsPanel.add(DFSButton);
controlsPanel.add(BFSButton);
controlsPanel.add(DijkstraButton);
controlsPanel.add(new JLabel(" Speed"));
controlsPanel.add(speedSlider);
mazePanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics gr) {
super.paint(gr);
Graphics2D g = (Graphics2D) gr;
int height = maze.getMazeHeight() + 1;
int width = maze.getMazeWidth() + 1;
int paneWidth = getWidth();
int paneHeight = getHeight();
double widthMultiplier = (double)(paneWidth) / (width + 1);
double heightMultiplier = (double)(paneHeight) / (height + 1);
STROKE_WIDTH = (int)(widthMultiplier / 10);
if (STROKE_WIDTH < 2) {
STROKE_WIDTH = 2;
}
g.setStroke(new BasicStroke(STROKE_WIDTH));
g.setColor(Color.GREEN);
g.fillRect((int)(widthMultiplier), (int)(heightMultiplier),
(int)widthMultiplier, (int)heightMultiplier);
g.setColor(Color.RED);
g.fillRect((int)((width - 1) * widthMultiplier), (int)((height - 1) * heightMultiplier),
(int)widthMultiplier, (int)heightMultiplier);
for (int row = 0; row < height - 1; row++) {
for (int col = 0; col < width - 1; col++) {
g.setColor(squareColors[colorIndex[row][col]]);
g.fillRect((int)((col + 1.32) * widthMultiplier), (int)((row + 1.32) * heightMultiplier),
(int)(widthMultiplier * 0.4), (int)(heightMultiplier * 0.4));
}
}
g.setColor(WALL_COLOR);
for (int row = 0; row < height - 1; row++) {
for (int col = 0; col < width - 1; col++) {
if (maze.isWallAbove(new Juncture(col, row))) {
drawHorizontalLine(g, (int)((col + 1) * widthMultiplier), (int)((row + 1) * heightMultiplier),
(int)((col + 2) * widthMultiplier));
}
if (maze.isWallToLeft(new Juncture(col, row))) {
drawVerticalLine(g, (int)((col + 1) * widthMultiplier), (int)((row + 1) * heightMultiplier),
(int)((row + 2) * heightMultiplier));
}
}
}
for (int x = 1; x < width; x++) {
for (int y = 1; y < height; y++) {
int a = (int)(x * widthMultiplier);
int b = (int)(y * heightMultiplier);
g.drawLine(a, b, a, b);
}
}
for (int row = 0; row < height - 1; row++) {
if (maze.isWallToRight(new Juncture(width - 2, row))) {
int col = width - 1;
drawVerticalLine(g, (int)((col + 1) * widthMultiplier), (int)((row + 1) * heightMultiplier),
(int)((row + 2) * heightMultiplier));
}
}
for (int col = 0; col < width - 1; col++) {
if (maze.isWallBelow(new Juncture(col, height - 2))) {
int row = height - 1;
drawHorizontalLine(g, (int)((col + 1) * widthMultiplier), (int)((row + 1) * heightMultiplier),
(int)((col + 2) * widthMultiplier));
}
}
g.setFont(new Font("Arial", Font.BOLD, (int)(0.5 * heightMultiplier)));
if (showWeights.isSelected()) {
g.setColor(Color.BLACK);
for (int row = 0; row < height - 2; row++) {
for (int col = 0; col < width - 1 ; col++) {
Juncture juncture = new Juncture(col, row);
if (!maze.isWallBelow(juncture)) {
g.drawString(String.valueOf(maze.getWeightBelow(juncture)), (int)((col + 1.38) * widthMultiplier), (int)((row + 2.2) * heightMultiplier));
}
}
}
for (int row = 0; row < height - 1; row++) {
for (int col = 0; col < width - 2; col++) {
Juncture juncture = new Juncture(col, row);
if (!maze.isWallToRight(juncture)) {
g.drawString(String.valueOf(maze.getWeightToRight(juncture)), (int)((col + 1.9) * widthMultiplier), (int)((row + 1.68) * heightMultiplier));
}
}
}
}
}
};
mazePanel.setBackground(Color.GRAY);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(mazePanel, BorderLayout.CENTER);
contentPane.add(controlsPanel, BorderLayout.NORTH);
window = new JFrame();
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setContentPane(contentPane);
window.setExtendedState(JFrame.MAXIMIZED_BOTH);
speedSlider.requestFocusInWindow();
window.setVisible(true);
}
private void changeButtonStatus(boolean setting) {
DFSButton.setEnabled(setting);
BFSButton.setEnabled(setting);
drawButton.setEnabled(setting);
DijkstraButton.setEnabled(setting);
}
private int getSliderDelayMS() {
int v = 109 - speedSlider.getValue();
return (v * v * v) / 1000;
}
private void drawHorizontalLine(Graphics2D g, int x1, int y, int x2) {
g.drawLine(x1, y, x2, y);
}
private void drawVerticalLine(Graphics2D g, int x, int y1, int y2) {
g.drawLine(x, y1, x, y2);
}
private void makeNewMaze() {
int height = Integer.parseInt(heightBox.getText());
int width = Integer.parseInt(widthBox.getText());
maze = new Maze(height, width, 100 - densitySlider.getValue());
colorIndex = new int[height][width];
resetColorsOfSquares();
theGraph = new MazeGraph(maze);
theGraph.addObserver(new GraphAlgorithmObserver<Juncture>() {
@Override
public void notifyBFSHasBegun() {
coloredJunctures = new ArrayList<Juncture>();
resetColorsOfSquares();
isDFS = false;
}
@Override
public void notifyDFSHasBegun() {
coloredJunctures = new Stack<Juncture>();
resetColorsOfSquares();
isDFS = true;
}
@Override
public void notifyDijkstraHasBegun() {
coloredJunctures = new Stack<Juncture>();
resetColorsOfSquares();
}
@Override
public void notifySearchIsOver() {
changeButtonStatus(true);
controlsPanel.updateUI();
}
@Override
public void notifyDijkstraVertexFinished(Juncture p, Integer pathCost) {
colorIndex[p.getY()][p.getX()] = 5;
mazePanel.updateUI();
try {
Thread.sleep(getSliderDelayMS());
} catch(InterruptedException e) {
}
}
@Override
public void notifyDijkstraIsOver(List<Juncture> path) {
for (int i = 0; i < maze.getMazeHeight(); i++) {
for (int j = 0; j < maze.getMazeWidth(); j++) {
colorIndex[i][j] = 0;
}
}
for (Juncture p : path) {
colorIndex[p.getY()][p.getX()] = 3;
}
changeButtonStatus(true);
mazePanel.updateUI();
}
@Override
public void notifyVisit(Juncture juncture) {
if (isDFS) {
/* Go through and remove all of the points from the end
* of the path back to the last point that was adjacent
* to this one
*/
while(!coloredJunctures.isEmpty()) {
Juncture p = ((Stack<Juncture>)coloredJunctures).peek();
if (theGraph.getWeight(p, juncture) != null) {
break;
}
colorIndex[p.getY()][p.getX()] = 0;
mazePanel.updateUI();
try {
Thread.sleep(getSliderDelayMS());
} catch(InterruptedException e) {
}
((Stack<Juncture>)coloredJunctures).pop();
}
colorIndex[juncture.getY()][juncture.getX()] = 3;
mazePanel.updateUI();
try {
Thread.sleep(getSliderDelayMS());
} catch(InterruptedException e) {
}
((Stack<Juncture>)coloredJunctures).push(juncture);
} else { // BFS
int c = 1;
for (int i = coloredJunctures.size() - 1; i >=0; i--) {
Juncture p = ((ArrayList<Juncture>)coloredJunctures).get(i);
if (theGraph.getWeight(p, juncture) != null) {
int old = colorIndex[p.getY()][p.getX()];
c = old + 1;
if (c >= squareColors.length) {
c = 1;
}
break;
}
}
colorIndex[juncture.getY()][juncture.getX()] = c;
mazePanel.updateUI();
try {
Thread.sleep(getSliderDelayMS());
} catch(InterruptedException e) {
}
coloredJunctures.add(juncture);
}
}
});
DFSButton.setEnabled(true);
BFSButton.setEnabled(true);
DFSButton.grabFocus();
controlsPanel.updateUI();
}
private void resetColorsOfSquares() {
for (int row = 0; row < maze.getMazeHeight(); row++) {
for (int col = 0; col < maze.getMazeWidth(); col++) {
colorIndex[row][col] = 0;
}
}
mazePanel.updateUI();
}
/** Request to the Event Dispatching Thread to create and
* display this GUI.
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MazeGUI();
}
});
}
}