-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeGUI-2.java
159 lines (135 loc) · 3.64 KB
/
MazeGUI-2.java
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
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.io.FileNotFoundException;
public class MazeGUI extends JPanel
{
GridBagConstraints gbc;
private GridLocation [][] squares;
private GridLocation startLocation;
private GridLocation goalLocation;
public MazeGUI(Maze maze)
{
int rows = maze.getNumRows();
int cols = maze.getNumCols();
squares = new GridLocation[rows][cols];
// Initializes the GUI
gbc = new GridBagConstraints();
setLayout(new GridBagLayout());
/* Sets up the grid */
for (int row = 0; row < rows; ++row)
{
for (int col = 0; col < cols; ++col)
{
gbc.gridx = col;
gbc.gridy = row;
//Square sqr = new Square(600/cols, 600/rows);
GridLocation loc = new GridLocation(row, col);
char symbol = maze.getSquare(loc);
if (symbol == '#')
loc.setBackgroundColor(Color.BLACK);
else if (symbol == 'o')
{
loc.setBackgroundColor(Color.GREEN);
startLocation = new GridLocation(row, col);
}
else if (symbol == '*')
{
loc.setBackgroundColor(Color.RED);
goalLocation = new GridLocation(row, col);
}
add(loc, gbc);
squares[row][col] = loc;
}
}
}
/*
* Called when a location added to agenda in solveMaze
*/
public void addLocToAgenda(GridLocation loc)
{
if ((!loc.equals(startLocation)) && (!loc.equals(goalLocation)))
squares[loc.getRow()][loc.getColumn()].setBackgroundColor(Color.LIGHT_GRAY);
}
/*
* Called after a location has been visited in solveMaze
*/
public void visitLoc(GridLocation loc)
{
if ((!loc.equals(startLocation)) && (!loc.equals(goalLocation)))
squares[loc.getRow()][loc.getColumn()].setBackgroundColor(Color.DARK_GRAY);
}
/*
* Called as solution path constructed in solveMaze
*/
public void addLocToPath(GridLocation loc)
{
if ((!loc.equals(startLocation)) && (!loc.equals(goalLocation)))
squares[loc.getRow()][loc.getColumn()].setBackgroundColor(Color.YELLOW);
}
public void pause(int delay)
{
try
{
Thread.sleep(delay);
}
catch (InterruptedException ie)
{
}
}
/*
* Print out a usage message instead of erroring
* with incorrect number or values for arguments to main
*/
public static void printUsage()
{
System.out.println("Usage: java MazeGUI <maze file> <agenda type>");
System.out.println("\tWhere agenda type should be s (for stack) or q (for queue)");
}
public static void main(String [] args)
{
if (args.length != 2)
{
printUsage();
System.exit(1);
}
JFrame frame = new JFrame("Maze Solver");
// Create a new maze based on maze file given
Maze maze = null;
try
{
maze = new Maze(args[0]);
}
catch(FileNotFoundException fnfe)
{
System.out.println("Cannot find maze file: " + args[0]);
System.exit(1);
}
// Now setup the GUI
MazeGUI mazeGraphics = new MazeGUI(maze);
frame.getContentPane().add("Center", mazeGraphics);
mazeGraphics.setFocusable(true);
mazeGraphics.requestFocusInWindow();
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Create an appropriate type of Agenda and a MazeSolver
// Then solve the maze
Agenda agenda = null;
if (args[1].equalsIgnoreCase("s"))
agenda = new StackAgenda();
else if (args[1].equalsIgnoreCase("q"))
agenda = new QueueAgenda();
else
{
printUsage();
System.exit(1);
}
MazeSolver solver = new MazeSolver(agenda);
solver.solveMaze(maze, mazeGraphics);
}
}