-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutionPanel.java
289 lines (259 loc) · 12.1 KB
/
SolutionPanel.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
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
public class SolutionPanel extends JPanel implements KeyListener{
private ArrowButton upArrowButton;
private ArrowButton downArrowButton;
private ArrowButton leftArrowButton;
private ArrowButton rightArrowButton;
private JButton prevButton;
private JButton nextButton;
private JButton viewSolutionButton;
private LinkedList<Directions> path;
private World world;
private String message;
private StatsPanel statsPanel;
private int currentIndex;
private Deque<State> stateStore;
public SolutionPanel(World world, LinkedList<Directions> path, long time, int nodesGenerated){
// Actual parameters needed by the solutionPanel
this.world = world;
this.path = path;
this.currentIndex = 0;
// stateStore
Player clonedPlayer = Clone.clonePlayer(world.getPlayer());
String[][] clonedArray2D = Clone.cloneArray2D(world.getWorldArray());
LinkedList<Directions> empty = new LinkedList<Directions>();
this.stateStore = new ArrayDeque<State>();
this.stateStore.offerLast(new State(clonedArray2D, clonedPlayer, world.getInitialEndPointCount(), empty));
// Default operations
this.setLayout(null);
this.setBounds(0,0,SolutionWindow.SOLUTION_FRAME_WIDTH, SolutionWindow.SOLUTION_FRAME_HEIGHT);
this.setPreferredSize(new Dimension(SolutionWindow.SOLUTION_FRAME_WIDTH, SolutionWindow.SOLUTION_FRAME_HEIGHT));
// Instantiate normal JButtons
this.prevButton = new JButton("<- previous");
this.nextButton = new JButton("next ->");
/* --------------------------------------------------------------
statsPanel will be receiving the receiving the:
Solution:
path -> for the actual path generated by BFS (LinkedList)
Stats:
time -> for the actual time it performed BFS method
nodesGenerated -> the nodes it enqueued in ArrayDeque
-----------------------------------------------------------------*/
this.statsPanel = new StatsPanel(path, time, nodesGenerated);
// Instantiate arrow buttons
// -> they have enabled and disabled properties. See defined ArrowButton class
this.upArrowButton = new ArrowButton("up_arrowkey.png", "up_arrowkey_blur.png");
this.downArrowButton = new ArrowButton("down_arrowkey.png", "down_arrowkey_blur.png");
this.leftArrowButton = new ArrowButton("left_arrowkey.png", "left_arrowkey_blur.png");
this.rightArrowButton = new ArrowButton("right_arrowkey.png", "right_arrowkey_blur.png");
// Add all of the buttons and panels to this main panel
this.add(upArrowButton);
this.add(downArrowButton);
this.add(leftArrowButton);
this.add(prevButton);
this.add(rightArrowButton);
this.add(nextButton);
this.add(statsPanel);
this.message = "Status: Puzzle is still not solved. Follow the highlighted arrowkeys.";
// Set the locations of each button
upArrowButton.setBounds(275,75,64,64);
downArrowButton.setBounds(275,139,64,64);
leftArrowButton.setBounds(211,139,64,64);
rightArrowButton.setBounds(339,139,64,64);
prevButton.setBounds(100,225,100, 40);
nextButton.setBounds(428,225,100, 40);
// So that it will not lose focus in the frame
// and still accept keyboard inputs
upArrowButton.setFocusable(false);
downArrowButton.setFocusable(false);
leftArrowButton.setFocusable(false);
rightArrowButton.setFocusable(false);
prevButton.setFocusable(false);
nextButton.setFocusable(false);
// // Which button will be highlighted first
if(this.path.get(currentIndex) == Directions.UP){
this.upArrowButton.unsetIcon();
} else if (this.path.get(currentIndex) == Directions.DOWN){
this.downArrowButton.unsetIcon();
} else if (this.path.get(currentIndex) == Directions.LEFT){
this.leftArrowButton.unsetIcon();
} else if (this.path.get(currentIndex) == Directions.RIGHT){
this.rightArrowButton.unsetIcon();
}
// Set each action that corresponds to each interface
// arrow buttons.
upArrowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!upArrowButton.isDisabled()){
Player clonedPlayer = Clone.clonePlayer(world.getPlayer());
String[][] clonedArray2D = Clone.cloneArray2D(world.getWorldArray());
LinkedList<Directions> empty = new LinkedList<Directions>();
stateStore.offerLast(new State(clonedArray2D, clonedPlayer, world.getInitialEndPointCount(), empty));
world.passDirectionToWorld(Directions.UP);
setNextIcon(Directions.UP);
}
}
});
downArrowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!downArrowButton.isDisabled()){
Player clonedPlayer = Clone.clonePlayer(world.getPlayer());
String[][] clonedArray2D = Clone.cloneArray2D(world.getWorldArray());
LinkedList<Directions> empty = new LinkedList<Directions>();
stateStore.offerLast(new State(clonedArray2D, clonedPlayer, world.getInitialEndPointCount(), empty));
world.passDirectionToWorld(Directions.DOWN);
setNextIcon(Directions.DOWN);
}
}
});
leftArrowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!leftArrowButton.isDisabled()){
Player clonedPlayer = Clone.clonePlayer(world.getPlayer());
String[][] clonedArray2D = Clone.cloneArray2D(world.getWorldArray());
LinkedList<Directions> empty = new LinkedList<Directions>();
stateStore.offerLast(new State(clonedArray2D, clonedPlayer, world.getInitialEndPointCount(), empty));
world.passDirectionToWorld(Directions.LEFT);
setNextIcon(Directions.LEFT);
}
}
});
rightArrowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!rightArrowButton.isDisabled()){
Player clonedPlayer = Clone.clonePlayer(world.getPlayer());
String[][] clonedArray2D = Clone.cloneArray2D(world.getWorldArray());
LinkedList<Directions> empty = new LinkedList<Directions>();
stateStore.offerLast(new State(clonedArray2D, clonedPlayer, world.getInitialEndPointCount(), empty));
world.passDirectionToWorld(Directions.RIGHT);
setNextIcon(Directions.RIGHT);
}
}
});
// Enable up, down, left, right keyboard buttons
this.addKeyListener(this);
// ask focus for the panel
this.setFocusable(true);
this.requestFocusInWindow();
prevButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(currentIndex <= 0) return;
updateIcons(path.get(currentIndex));
currentIndex--;
Directions nextDir = path.get(currentIndex);
updateIcons(nextDir);
State prevState = stateStore.pollLast();
world.setWorldArray(prevState.getWorldArray());
world.setPlayer(prevState.getPlayer());
world.repaint();
}
});
nextButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(currentIndex >= path.size()) return;
Directions currDir = path.get(currentIndex);
setNextIcon(currDir);
Player clonedPlayer = Clone.clonePlayer(world.getPlayer());
String[][] clonedArray2D = Clone.cloneArray2D(world.getWorldArray());
LinkedList<Directions> empty = new LinkedList<Directions>();
stateStore.offerLast(new State(clonedArray2D, clonedPlayer, world.getInitialEndPointCount(), empty));
world.passDirectionToWorld(currDir);
}
});
}
private void updateIcons(Directions dir){
// updateIcons is a function that will determine
// given a direction, what should be the proper
// highlighted buttons
switch(dir){
case UP:
this.upArrowButton.unsetIcon();
break;
case DOWN:
this.downArrowButton.unsetIcon();
break;
case LEFT:
this.leftArrowButton.unsetIcon();
break;
case RIGHT:
this.rightArrowButton.unsetIcon();
break;
}
}
private void setNextIcon(Directions dir){
// setNextIcon is a function that will update the icons for
// next instructions
if(this.currentIndex >= this.path.size() - 1){
updateIcons(dir);
this.removeKeyListener(this);
this.message = "Status: Puzzle solved. You may exit this window now.";
this.repaint();
return;
}
else{
if(dir.equals(this.path.get(currentIndex))){
updateIcons(dir);
currentIndex++;
Directions nextDir = this.path.get(currentIndex);
updateIcons(nextDir);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){
// For keyListener to enable UDLR keyboard buttons
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
if(this.path.get(currentIndex) == Directions.UP){
world.passDirectionToWorld(Directions.UP);
this.setNextIcon(Directions.UP);
}
break;
case KeyEvent.VK_DOWN:
if(this.path.get(currentIndex) == Directions.DOWN){
world.passDirectionToWorld(Directions.DOWN);
this.setNextIcon(Directions.DOWN);
}
break;
case KeyEvent.VK_LEFT:
if(this.path.get(currentIndex) == Directions.LEFT){
world.passDirectionToWorld(Directions.LEFT);
this.setNextIcon(Directions.LEFT);
}
break;
case KeyEvent.VK_RIGHT:
if(this.path.get(currentIndex) == Directions.RIGHT){
world.passDirectionToWorld(Directions.RIGHT);
this.setNextIcon(Directions.RIGHT);
}
break;
}
}
public void keyReleased(KeyEvent e){}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Draw the background of the solutionPanel
g2d.drawImage(Main.textureLoader.getTexture(Texture.DEFAULT_PATH, "solutionsBack.jpg").getImage(),0,0,null);
// Change to "somehow" black for background of status bar
g2d.setColor(new Color(0,0,0,180));
// Render status bar
g2d.fillRect(0,0,600,25);
// Change font color to white
g2d.setColor(Color.WHITE);
// Render status string
g2d.drawString(this.message, 0, 20);
}
}