From: LeonardoBizzoni Date: Fri, 2 Jul 2021 07:20:49 +0000 (+0200) Subject: Final A* X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=3437013b7cf3d0cd5684f6ce2ae249a40e1192cb;p=astar-visualizer Final A* --- diff --git a/src/main/java/Main/AStar.java b/src/main/java/Main/AStar.java index 333e84a..1625baf 100644 --- a/src/main/java/Main/AStar.java +++ b/src/main/java/Main/AStar.java @@ -43,7 +43,7 @@ public class AStar{ PathfinderUtils.drawPath(); map.isFinished = true; - ControlPanel.toggleRunBtn.setText("Clear"); + map.running = false; map.repaint(); return; } @@ -94,7 +94,6 @@ public class AStar{ } map.isFinished = true; - ControlPanel.toggleRunBtn.setText("Clear"); map.repaint(); return null; } diff --git a/src/main/java/Main/ControlPanel.java b/src/main/java/Main/ControlPanel.java deleted file mode 100644 index 30e7aa4..0000000 --- a/src/main/java/Main/ControlPanel.java +++ /dev/null @@ -1,33 +0,0 @@ -package Main; - -import java.awt.Color; -import java.awt.Insets; - -import javax.swing.JButton; -import javax.swing.JComboBox; - -public class ControlPanel { - static JButton toggleRunBtn; - static JComboBox algo; - - public ControlPanel(Map map) { - algo = new JComboBox<>(new String[] { "Select an algorithm", "A*", "Dijkstra", "Breadth-first search"}); - algo.setVisible(true); - algo.setFocusable(false); - - toggleRunBtn = new JButton("Run"); - toggleRunBtn.setVisible(true); - toggleRunBtn.setMargin(new Insets(0, 0, 0, 0)); - toggleRunBtn.setBackground(Color.white); - toggleRunBtn.addActionListener(map); - toggleRunBtn.setFocusable(false); - - map.add(algo); - map.add(toggleRunBtn); - } - - public void renderMenu() { - algo.setBounds(10,10, algo.getWidth(), algo.getHeight()); - toggleRunBtn.setBounds(220, 10, 48, 24); - } -} diff --git a/src/main/java/Main/Map.java b/src/main/java/Main/Map.java index c92abfe..c2d94e5 100644 --- a/src/main/java/Main/Map.java +++ b/src/main/java/Main/Map.java @@ -1,12 +1,3 @@ -/* TODO - * aggiungere una sorta di menu dove scegliere: - * - velocità di riproduzione - * - tipo di algoritmo: - * + a* - * + dijkstra - * + breadth-first search - * zoommare sulla griglia - */ package Main; import java.awt.Color; @@ -14,8 +5,6 @@ import java.awt.Graphics; import java.awt.Dimension; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; @@ -25,14 +14,14 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; -class Map extends JPanel implements ActionListener, MouseListener, MouseMotionListener, KeyListener { +class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListener { private JFrame window; private char key = (char) 0; - private ControlPanel menu = new ControlPanel(this); static int width; static int height; boolean isFinished = false; + boolean running = false; int size = 20; @@ -89,8 +78,6 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); } - menu.renderMenu(); - // Draws open nodes g.setColor(new Color(69, 133, 136)); for (Node node : PathfinderUtils.openNodes) { @@ -120,18 +107,22 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi int posX = e.getX() - (e.getX() % size); int posY = e.getY() - (e.getY() % size); - // Checks if start node and end node are the same - if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) { - if (PathfinderUtils.isSameNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) { - JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", - "Same node error", JOptionPane.ERROR_MESSAGE); + if (PathfinderUtils.startNode == null && PathfinderUtils.endNode == null) { + PathfinderUtils.startNode = new Node(posX, posY); + } else if (PathfinderUtils.endNode != null && PathfinderUtils.startNode == null) { + if (posX == PathfinderUtils.endNode.getX() && posY == PathfinderUtils.endNode.getY()) { + JOptionPane.showMessageDialog(null, "Same node error", + "End node and start node can't be the same node", JOptionPane.ERROR_MESSAGE); return; } - } - - if (PathfinderUtils.startNode == null) { PathfinderUtils.startNode = new Node(posX, posY); } else { + if (posX == PathfinderUtils.endNode.getX() && posY == PathfinderUtils.endNode.getY()) { + JOptionPane.showMessageDialog(null, "Same node error", + "End node and start node can't be the same node", JOptionPane.ERROR_MESSAGE); + return; + } + PathfinderUtils.startNode.setX(posX); PathfinderUtils.startNode.setY(posY); } @@ -143,18 +134,22 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi int posX = e.getX() - (e.getX() % size); int posY = e.getY() - (e.getY() % size); - // Checks if end node and start node are the same - if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) { - if (PathfinderUtils.isSameNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) { - JOptionPane.showMessageDialog(null, "End node and start node can't be the same node!", - "SAME NODE ERROR", JOptionPane.ERROR_MESSAGE); + if (PathfinderUtils.startNode == null && PathfinderUtils.endNode == null) { + PathfinderUtils.endNode = new Node(posX, posY); + } else if (PathfinderUtils.startNode != null && PathfinderUtils.endNode == null) { + if (posX == PathfinderUtils.startNode.getX() && posY == PathfinderUtils.startNode.getY()) { + JOptionPane.showMessageDialog(null, "Same node error", + "End node and start node can't be the same node", JOptionPane.ERROR_MESSAGE); return; } - } - - if (PathfinderUtils.endNode == null) { PathfinderUtils.endNode = new Node(posX, posY); } else { + if (posX == PathfinderUtils.startNode.getX() && posY == PathfinderUtils.startNode.getY()) { + JOptionPane.showMessageDialog(null, "Same node error", + "End node and start node can't be the same node", JOptionPane.ERROR_MESSAGE); + return; + } + PathfinderUtils.endNode.setX(posX); PathfinderUtils.endNode.setY(posY); } @@ -213,6 +208,30 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi @Override public void keyPressed(KeyEvent e) { key = e.getKeyChar(); + + if (key == KeyEvent.VK_SPACE) { + if (running == false && isFinished == false) { + if (PathfinderUtils.startNode == null || PathfinderUtils.endNode == null) { + JOptionPane.showMessageDialog(null, "Missing node", "Missing start or end node", + JOptionPane.ERROR_MESSAGE); + return; + } + + running = true; + new AStar(this).start(); + } else if (running == false && isFinished == true) { + PathfinderUtils.barriers.clear(); + PathfinderUtils.openNodes.clear(); + PathfinderUtils.closedNodes.clear(); + PathfinderUtils.startNode = null; + PathfinderUtils.endNode = null; + + isFinished = false; + running = false; + + this.repaint(); + } + } } @Override @@ -230,49 +249,6 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi mapDrawing(e); } - @Override - public void actionPerformed(ActionEvent e) { - if (ControlPanel.toggleRunBtn.getText().equals("Run")) { - - if (PathfinderUtils.startNode == null || PathfinderUtils.endNode == null) - return; - - switch (ControlPanel.algo.getItemAt(ControlPanel.algo.getSelectedIndex())) { - case ("A*"): - new AStar(this).start(); - break; - - case ("Dijkstra"): - System.out.println("Dijkstra selected"); - // AStar.start(); - break; - - case ("Breadth-first search"): - System.out.println("Breadth-first search selected"); - // AStar.start(); - break; - - default: - JOptionPane.showMessageDialog(null, "You must select an algorithm before starting the pathfinder", - "Algorithm not selected", JOptionPane.ERROR_MESSAGE); - return; - } - } - - else if (ControlPanel.toggleRunBtn.getText().equals("Clear")) { - PathfinderUtils.barriers.clear(); - PathfinderUtils.openNodes.clear(); - PathfinderUtils.closedNodes.clear(); - PathfinderUtils.startNode = null; - PathfinderUtils.endNode = null; - - isFinished = false; - - this.repaint(); - ControlPanel.toggleRunBtn.setText("Run"); - } - } - @Override public void keyTyped(KeyEvent e) { } diff --git a/target/PathVisualizer-1.0-SNAPSHOT.jar b/target/PathVisualizer-1.0-SNAPSHOT.jar index 7079e78..365027e 100644 Binary files a/target/PathVisualizer-1.0-SNAPSHOT.jar and b/target/PathVisualizer-1.0-SNAPSHOT.jar differ diff --git a/target/classes/Main/AStar.class b/target/classes/Main/AStar.class index b964859..0452160 100644 Binary files a/target/classes/Main/AStar.class and b/target/classes/Main/AStar.class differ diff --git a/target/classes/Main/ControlPanel.class b/target/classes/Main/ControlPanel.class deleted file mode 100644 index 865752d..0000000 Binary files a/target/classes/Main/ControlPanel.class and /dev/null differ diff --git a/target/classes/Main/Map.class b/target/classes/Main/Map.class index a6a0f83..bb87574 100644 Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst index b1dccf0..e2dbad2 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -1,6 +1,5 @@ Main/Node.class Main/AStar.class -Main/ControlPanel.class Main/Map.class Main/App.class Main/PathfinderUtils.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst index cdd74a9..cc3946d 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -1,4 +1,3 @@ -/home/leo/Docs/Proj/PathVisualizer/src/main/java/Main/ControlPanel.java /home/leo/Docs/Proj/PathVisualizer/src/main/java/Main/Map.java /home/leo/Docs/Proj/PathVisualizer/src/main/java/Main/Node.java /home/leo/Docs/Proj/PathVisualizer/src/main/java/Main/AStar.java diff --git a/target/surefire-reports/Main.AppTest.txt b/target/surefire-reports/Main.AppTest.txt index 9c021fa..4193956 100644 --- a/target/surefire-reports/Main.AppTest.txt +++ b/target/surefire-reports/Main.AppTest.txt @@ -1,4 +1,4 @@ ------------------------------------------------------------------------------- Test set: Main.AppTest ------------------------------------------------------------------------------- -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 s - in Main.AppTest +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.024 s - in Main.AppTest diff --git a/target/surefire-reports/TEST-Main.AppTest.xml b/target/surefire-reports/TEST-Main.AppTest.xml index 07c98c8..b9ce485 100644 --- a/target/surefire-reports/TEST-Main.AppTest.xml +++ b/target/surefire-reports/TEST-Main.AppTest.xml @@ -1,5 +1,5 @@ - + @@ -16,7 +16,7 @@ - + @@ -32,7 +32,7 @@ - + @@ -59,5 +59,5 @@ - + \ No newline at end of file