Skip to content

Commit e693474

Browse files
committed
added stop/resume
1 parent 3690eb0 commit e693474

File tree

4 files changed

+98
-22
lines changed

4 files changed

+98
-22
lines changed

Path Finding Visualizer.jar

1.4 KB
Binary file not shown.

Path-Finding_Visualizer/src/ControlsPanel.java

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import java.awt.Graphics2D;
55
import java.awt.event.ActionEvent;
66
import java.awt.event.ActionListener;
7+
import java.awt.event.FocusEvent;
8+
import java.awt.event.FocusListener;
9+
import java.awt.event.KeyAdapter;
10+
import java.awt.event.KeyEvent;
711
import java.util.Random;
812

913
import javax.swing.JButton;
@@ -16,14 +20,14 @@
1620
import javax.swing.event.ChangeEvent;
1721
import javax.swing.event.ChangeListener;
1822

19-
public class ControlsPanel extends JPanel {
23+
public class ControlsPanel extends JPanel implements ActionListener {
2024

2125
private JButton search;
2226
private JButton reset;
2327
private JButton resetPath;
2428
private JButton generate;
2529
private JComboBox<String> algorithms;
26-
// private JComboBox<String> mazes;
30+
2731
private JSlider size;
2832
private JSlider delay;
2933
private JLabel sizeLabel;
@@ -40,31 +44,60 @@ public class ControlsPanel extends JPanel {
4044
private JLabel path;
4145
private JLabel wall;
4246
private JButton help;
47+
private GridPanel gridPanel;
48+
SearchAlgorithms algo = null;
4349

4450
public ControlsPanel(int width, int height, GridPanel gridPanel) {
51+
this.gridPanel = gridPanel;
52+
4553
this.setPreferredSize(new Dimension(width, height));
4654
this.setFocusable(true);
4755
this.setLayout(null);
56+
this.requestFocus();
57+
this.addFocusListener(new FocusListener() {
4858

49-
search = new JButton("Visualize");
50-
search.setBounds(25, 10, 150, 30);
51-
search.addActionListener(new ActionListener() {
59+
@Override
60+
public void focusLost(FocusEvent e) {
61+
ControlsPanel p = (ControlsPanel) e.getSource();
62+
p.requestFocus();
63+
}
5264

5365
@Override
54-
public void actionPerformed(ActionEvent e) {
55-
if (MyUtils.solving) {
56-
return;
57-
}
58-
gridPanel.resetPath();
66+
public void focusGained(FocusEvent e) {
5967

60-
MyUtils.solving = true;
61-
SearchAlgorithms algo = new SearchAlgorithms(gridPanel.getGrid(), gridPanel);
62-
algo.start();
6368
}
6469
});
70+
this.addKeyListener(new KeyAdapter() {
71+
@SuppressWarnings("removal")
72+
@Override
73+
public void keyPressed(KeyEvent e) {
74+
75+
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
76+
if (algo == null || !MyUtils.solving) {
77+
return;
78+
}
79+
80+
if (MyUtils.stopped) {
81+
MyUtils.stopped = false;
82+
algo.resume();
83+
return;
84+
}
85+
86+
MyUtils.stopped = true;
87+
algo.suspend();
88+
89+
}
90+
}
91+
});
92+
93+
search = new JButton("Visualize");
94+
search.setBounds(25, 10, 150, 30);
95+
search.addActionListener(this);
96+
search.setFocusable(false);
6597

6698
generate = new JButton("Generate maze");
6799
generate.setBounds(25, 50, 150, 30);
100+
generate.setFocusable(false);
68101
generate.addActionListener(new ActionListener() {
69102

70103
@Override
@@ -91,6 +124,7 @@ public void actionPerformed(ActionEvent e) {
91124

92125
reset = new JButton("Reset grid");
93126
reset.setBounds(25, 90, 150, 30);
127+
reset.setFocusable(false);
94128
reset.addActionListener(new ActionListener() {
95129
@Override
96130
public void actionPerformed(ActionEvent e) {
@@ -104,6 +138,7 @@ public void actionPerformed(ActionEvent e) {
104138

105139
resetPath = new JButton("Reset path");
106140
resetPath.setBounds(25, 130, 150, 30);
141+
resetPath.setFocusable(false);
107142
resetPath.addActionListener(new ActionListener() {
108143
@Override
109144
public void actionPerformed(ActionEvent e) {
@@ -118,7 +153,7 @@ public void actionPerformed(ActionEvent e) {
118153
algorithms = new JComboBox<>(algorithmsName);
119154
algorithms.setSelectedIndex(0);
120155
algorithms.setBounds(25, 175, 150, 30);
121-
156+
algorithms.setFocusable(false);
122157
algorithms.addActionListener(new ActionListener() {
123158
@Override
124159
public void actionPerformed(ActionEvent e) {
@@ -132,6 +167,7 @@ public void actionPerformed(ActionEvent e) {
132167

133168
allowDiagonials = new JCheckBox("Allow diagonial moves");
134169
allowDiagonials.setBounds(20, 215, 200, 20);
170+
allowDiagonials.setFocusable(false);
135171
allowDiagonials.addChangeListener(new ChangeListener() {
136172
@Override
137173
public void stateChanged(ChangeEvent e) {
@@ -149,12 +185,13 @@ public void stateChanged(ChangeEvent e) {
149185

150186
sizeLabel = new JLabel("Size: 20x20");
151187
sizeLabel.setBounds(24, 245, 150, 20);
188+
sizeLabel.setFocusable(false);
152189

153190
size = new JSlider(20, 60, Node.size);
154191
size.setMajorTickSpacing(10);
155192
size.setMinorTickSpacing(10);
156193
size.setBounds(18, 270, 150, 20);
157-
194+
size.setFocusable(false);
158195
size.addChangeListener(new ChangeListener() {
159196
@Override
160197
public void stateChanged(ChangeEvent e) {
@@ -173,11 +210,13 @@ public void stateChanged(ChangeEvent e) {
173210

174211
delayLabel = new JLabel("Delay: 30ms");
175212
delayLabel.setBounds(24, 295, 150, 20);
213+
delayLabel.setFocusable(false);
176214

177215
delay = new JSlider(0, 100, MyUtils.delay);
178216
delay.setMajorTickSpacing(10);
179217
delay.setMinorTickSpacing(10);
180218
delay.setBounds(18, 320, 150, 20);
219+
delay.setFocusable(false);
181220

182221
delay.addChangeListener(new ChangeListener() {
183222
@Override
@@ -189,40 +228,50 @@ public void stateChanged(ChangeEvent e) {
189228

190229
copyright = new JLabel("© 2021 Leonard Pepa ics20033");
191230
copyright.setBounds(10, (int) (this.getPreferredSize().getHeight() - 30), 200, 20);
231+
copyright.setFocusable(false);
192232

193233
start = new JLabel("Starting node");
194234
start.setBounds(50, 350, 100, 15);
235+
start.setFocusable(false);
195236

196237
target = new JLabel("Target node");
197238
target.setBounds(50, 375, 100, 15);
239+
target.setFocusable(false);
198240

199241
current = new JLabel("Current node");
200242
current.setBounds(50, 400, 100, 15);
243+
current.setFocusable(false);
201244

202245
visited = new JLabel("Visited node");
203246
visited.setBounds(50, 425, 100, 15);
247+
visited.setFocusable(false);
204248

205249
frontier = new JLabel("Frontier node");
206250
frontier.setBounds(50, 450, 100, 15);
251+
frontier.setFocusable(false);
207252

208253
path = new JLabel("Path node");
209254
path.setBounds(50, 475, 100, 15);
255+
path.setFocusable(false);
210256

211257
wall = new JLabel("Wall node");
212258
wall.setBounds(50, 500, 100, 15);
259+
wall.setFocusable(false);
213260

214261
help = new JButton("** Help **");
215262
help.setBounds(25, (int) (this.getPreferredSize().getHeight() - 70), 150, 30);
263+
help.setFocusable(false);
264+
216265
help.addActionListener(new ActionListener() {
217266
@Override
218267
public void actionPerformed(ActionEvent e) {
219-
JOptionPane.showMessageDialog(null,
220-
"Move Starting Node: drag the starting node with left click pressed" + System.lineSeparator()
221-
+ System.lineSeparator()
222-
+ "Move Target Node: drag the target node with left click pressed"
223-
+ System.lineSeparator() + System.lineSeparator()
224-
+ "Place Wall Node: press or drag empty cells with left click" + System.lineSeparator()
225-
+ System.lineSeparator() + "Erase Wall Node: press or drag wall cells with right click",
268+
JOptionPane.showMessageDialog(null, "Move Starting Node: drag the starting node with left click pressed"
269+
+ System.lineSeparator() + System.lineSeparator()
270+
+ "Move Target Node: drag the target node with left click pressed" + System.lineSeparator()
271+
+ System.lineSeparator() + "Place Wall Node: press or drag empty cells with left click"
272+
+ System.lineSeparator() + System.lineSeparator()
273+
+ "Erase Wall Node: press or drag wall cells with right click" + System.lineSeparator()
274+
+ System.lineSeparator() + "Pause/resume: press space to pause or resume the proccess",
226275
"Instructions", JOptionPane.INFORMATION_MESSAGE);
227276
}
228277
});
@@ -246,6 +295,7 @@ public void actionPerformed(ActionEvent e) {
246295
this.add(path);
247296
this.add(wall);
248297
this.add(help);
298+
249299
}
250300

251301
@Override
@@ -276,4 +326,18 @@ public void paintComponent(Graphics graphics) {
276326
}
277327

278328
private static final long serialVersionUID = -7860009947400256170L;
329+
330+
@Override
331+
public void actionPerformed(ActionEvent e) {
332+
this.requestFocus();
333+
334+
if (MyUtils.solving) {
335+
return;
336+
}
337+
gridPanel.resetPath();
338+
339+
MyUtils.solving = true;
340+
algo = new SearchAlgorithms(gridPanel.getGrid(), gridPanel);
341+
algo.start();
342+
}
279343
}

Path-Finding_Visualizer/src/GridPanel.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import java.awt.BasicStroke;
22
import java.awt.Color;
33
import java.awt.Cursor;
4+
import java.awt.Font;
45
import java.awt.Graphics;
56
import java.awt.Graphics2D;
67
import java.awt.event.MouseEvent;
@@ -62,6 +63,7 @@ public void updateFinish(int x, int y) {
6263
public void resetGrid() {
6364
MyUtils.solving = false;
6465
MyUtils.breakAlgo = true;
66+
MyUtils.stopped = false;
6567
grid.initialiseGrid();
6668
this.revalidate();
6769
this.repaint();
@@ -88,6 +90,14 @@ public void paintComponent(Graphics graphics) {
8890
g.setStroke(new BasicStroke(1.5f));
8991
g.setColor(Color.black);
9092
grid.drawGrid(g, this);
93+
94+
if(MyUtils.stopped) {
95+
g.setFont(new Font("Poppins", Font.CENTER_BASELINE, 60));
96+
g.setColor(new Color(255, 0, 142));
97+
g.drawString("Paused", width / 2 - 110 , height / 4);
98+
g.setFont(new Font("Poppins", Font.CENTER_BASELINE, 30));
99+
g.drawString("Press space to resume", width / 2 - 160 , height / 3);
100+
}
91101
}
92102

93103
@Override
@@ -96,6 +106,7 @@ public void mouseClicked(MouseEvent e) {
96106
}
97107

98108
public void resetPath() {
109+
MyUtils.stopped = false;
99110
grid.resetPath();
100111
this.revalidate();
101112
this.repaint();

Path-Finding_Visualizer/src/MyUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public class MyUtils {
88
public static boolean solving = false;
99
public static boolean breakAlgo = false;
1010
public static boolean allowDiagonials = false;
11+
public static boolean stopped = false;
1112
}

0 commit comments

Comments
 (0)