Skip to content

Commit

Permalink
added stop/resume
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardpepa committed Jan 27, 2022
1 parent 3690eb0 commit e693474
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 22 deletions.
Binary file modified Path Finding Visualizer.jar
Binary file not shown.
108 changes: 86 additions & 22 deletions Path-Finding_Visualizer/src/ControlsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
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.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

import javax.swing.JButton;
Expand All @@ -16,14 +20,14 @@
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ControlsPanel extends JPanel {
public class ControlsPanel extends JPanel implements ActionListener {

private JButton search;
private JButton reset;
private JButton resetPath;
private JButton generate;
private JComboBox<String> algorithms;
// private JComboBox<String> mazes;

private JSlider size;
private JSlider delay;
private JLabel sizeLabel;
Expand All @@ -40,31 +44,60 @@ public class ControlsPanel extends JPanel {
private JLabel path;
private JLabel wall;
private JButton help;
private GridPanel gridPanel;
SearchAlgorithms algo = null;

public ControlsPanel(int width, int height, GridPanel gridPanel) {
this.gridPanel = gridPanel;

this.setPreferredSize(new Dimension(width, height));
this.setFocusable(true);
this.setLayout(null);
this.requestFocus();
this.addFocusListener(new FocusListener() {

search = new JButton("Visualize");
search.setBounds(25, 10, 150, 30);
search.addActionListener(new ActionListener() {
@Override
public void focusLost(FocusEvent e) {
ControlsPanel p = (ControlsPanel) e.getSource();
p.requestFocus();
}

@Override
public void actionPerformed(ActionEvent e) {
if (MyUtils.solving) {
return;
}
gridPanel.resetPath();
public void focusGained(FocusEvent e) {

MyUtils.solving = true;
SearchAlgorithms algo = new SearchAlgorithms(gridPanel.getGrid(), gridPanel);
algo.start();
}
});
this.addKeyListener(new KeyAdapter() {
@SuppressWarnings("removal")
@Override
public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (algo == null || !MyUtils.solving) {
return;
}

if (MyUtils.stopped) {
MyUtils.stopped = false;
algo.resume();
return;
}

MyUtils.stopped = true;
algo.suspend();

}
}
});

search = new JButton("Visualize");
search.setBounds(25, 10, 150, 30);
search.addActionListener(this);
search.setFocusable(false);

generate = new JButton("Generate maze");
generate.setBounds(25, 50, 150, 30);
generate.setFocusable(false);
generate.addActionListener(new ActionListener() {

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

reset = new JButton("Reset grid");
reset.setBounds(25, 90, 150, 30);
reset.setFocusable(false);
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Expand All @@ -104,6 +138,7 @@ public void actionPerformed(ActionEvent e) {

resetPath = new JButton("Reset path");
resetPath.setBounds(25, 130, 150, 30);
resetPath.setFocusable(false);
resetPath.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Expand All @@ -118,7 +153,7 @@ public void actionPerformed(ActionEvent e) {
algorithms = new JComboBox<>(algorithmsName);
algorithms.setSelectedIndex(0);
algorithms.setBounds(25, 175, 150, 30);

algorithms.setFocusable(false);
algorithms.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Expand All @@ -132,6 +167,7 @@ public void actionPerformed(ActionEvent e) {

allowDiagonials = new JCheckBox("Allow diagonial moves");
allowDiagonials.setBounds(20, 215, 200, 20);
allowDiagonials.setFocusable(false);
allowDiagonials.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Expand All @@ -149,12 +185,13 @@ public void stateChanged(ChangeEvent e) {

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

size = new JSlider(20, 60, Node.size);
size.setMajorTickSpacing(10);
size.setMinorTickSpacing(10);
size.setBounds(18, 270, 150, 20);

size.setFocusable(false);
size.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Expand All @@ -173,11 +210,13 @@ public void stateChanged(ChangeEvent e) {

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

delay = new JSlider(0, 100, MyUtils.delay);
delay.setMajorTickSpacing(10);
delay.setMinorTickSpacing(10);
delay.setBounds(18, 320, 150, 20);
delay.setFocusable(false);

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

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

start = new JLabel("Starting node");
start.setBounds(50, 350, 100, 15);
start.setFocusable(false);

target = new JLabel("Target node");
target.setBounds(50, 375, 100, 15);
target.setFocusable(false);

current = new JLabel("Current node");
current.setBounds(50, 400, 100, 15);
current.setFocusable(false);

visited = new JLabel("Visited node");
visited.setBounds(50, 425, 100, 15);
visited.setFocusable(false);

frontier = new JLabel("Frontier node");
frontier.setBounds(50, 450, 100, 15);
frontier.setFocusable(false);

path = new JLabel("Path node");
path.setBounds(50, 475, 100, 15);
path.setFocusable(false);

wall = new JLabel("Wall node");
wall.setBounds(50, 500, 100, 15);
wall.setFocusable(false);

help = new JButton("** Help **");
help.setBounds(25, (int) (this.getPreferredSize().getHeight() - 70), 150, 30);
help.setFocusable(false);

help.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"Move Starting Node: drag the starting node with left click pressed" + System.lineSeparator()
+ System.lineSeparator()
+ "Move Target Node: drag the target node with left click pressed"
+ System.lineSeparator() + System.lineSeparator()
+ "Place Wall Node: press or drag empty cells with left click" + System.lineSeparator()
+ System.lineSeparator() + "Erase Wall Node: press or drag wall cells with right click",
JOptionPane.showMessageDialog(null, "Move Starting Node: drag the starting node with left click pressed"
+ System.lineSeparator() + System.lineSeparator()
+ "Move Target Node: drag the target node with left click pressed" + System.lineSeparator()
+ System.lineSeparator() + "Place Wall Node: press or drag empty cells with left click"
+ System.lineSeparator() + System.lineSeparator()
+ "Erase Wall Node: press or drag wall cells with right click" + System.lineSeparator()
+ System.lineSeparator() + "Pause/resume: press space to pause or resume the proccess",
"Instructions", JOptionPane.INFORMATION_MESSAGE);
}
});
Expand All @@ -246,6 +295,7 @@ public void actionPerformed(ActionEvent e) {
this.add(path);
this.add(wall);
this.add(help);

}

@Override
Expand Down Expand Up @@ -276,4 +326,18 @@ public void paintComponent(Graphics graphics) {
}

private static final long serialVersionUID = -7860009947400256170L;

@Override
public void actionPerformed(ActionEvent e) {
this.requestFocus();

if (MyUtils.solving) {
return;
}
gridPanel.resetPath();

MyUtils.solving = true;
algo = new SearchAlgorithms(gridPanel.getGrid(), gridPanel);
algo.start();
}
}
11 changes: 11 additions & 0 deletions Path-Finding_Visualizer/src/GridPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
Expand Down Expand Up @@ -62,6 +63,7 @@ public void updateFinish(int x, int y) {
public void resetGrid() {
MyUtils.solving = false;
MyUtils.breakAlgo = true;
MyUtils.stopped = false;
grid.initialiseGrid();
this.revalidate();
this.repaint();
Expand All @@ -88,6 +90,14 @@ public void paintComponent(Graphics graphics) {
g.setStroke(new BasicStroke(1.5f));
g.setColor(Color.black);
grid.drawGrid(g, this);

if(MyUtils.stopped) {
g.setFont(new Font("Poppins", Font.CENTER_BASELINE, 60));
g.setColor(new Color(255, 0, 142));
g.drawString("Paused", width / 2 - 110 , height / 4);
g.setFont(new Font("Poppins", Font.CENTER_BASELINE, 30));
g.drawString("Press space to resume", width / 2 - 160 , height / 3);
}
}

@Override
Expand All @@ -96,6 +106,7 @@ public void mouseClicked(MouseEvent e) {
}

public void resetPath() {
MyUtils.stopped = false;
grid.resetPath();
this.revalidate();
this.repaint();
Expand Down
1 change: 1 addition & 0 deletions Path-Finding_Visualizer/src/MyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public class MyUtils {
public static boolean solving = false;
public static boolean breakAlgo = false;
public static boolean allowDiagonials = false;
public static boolean stopped = false;
}

0 comments on commit e693474

Please sign in to comment.