From: leo Date: Fri, 19 Nov 2021 20:05:03 +0000 (+0100) Subject: Added animation during pathfinding X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=c7d1a8fce7a499966644f785bfe81b5d7b3b1dff;p=astar-visualizer Added animation during pathfinding --- diff --git a/src/main/java/Main/AStar.java b/src/main/java/Main/AStar.java index e86a967..0639312 100644 --- a/src/main/java/Main/AStar.java +++ b/src/main/java/Main/AStar.java @@ -1,6 +1,8 @@ package Main; -public class AStar{ +import javax.swing.JOptionPane; + +public class AStar implements Runnable { private Map map; private int x, y; @@ -9,26 +11,33 @@ public class AStar{ PathfinderUtils.path.clear(); } - public void start() { + @Override + public void run() { searchPath(PathfinderUtils.startNode); } public void searchPath(Node parent) { - for (int i = 0; i < 4; i++) { - x = (int) Math.round(parent.getX() + (-map.getNodeSize()* Math.cos((Math.PI / 2) * i))); - y = (int) Math.round(parent.getY() + (-map.getNodeSize() * Math.sin((Math.PI / 2) * i))); + while (!map.isFinished()) { + for (int i = 0; i < 4; i++) { + x = (int) Math.round(parent.getX() + (-map.getNodeSize() * Math.cos((Math.PI / 2) * i))); + y = (int) Math.round(parent.getY() + (-map.getNodeSize() * Math.sin((Math.PI / 2) * i))); - calculateOpenNode(x, y, parent); - } + calculateOpenNode(x, y, parent); + } - if((parent = getNextBestNode()) == null) - return; + if ((parent = getNextBestNode()) == null) + return; - PathfinderUtils.closedNodes.add(parent); - PathfinderUtils.openNodes.remove(parent); + PathfinderUtils.closedNodes.add(parent); + PathfinderUtils.openNodes.remove(parent); - if (!map.isFinished()) - searchPath(parent); + map.repaint(); + try { + Thread.sleep(map.speed); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } public void calculateOpenNode(int nextX, int nextY, Node parent) { @@ -44,7 +53,6 @@ public class AStar{ PathfinderUtils.drawPath(); map.setFinished(true); map.setRunning(false); - map.repaint(); return; } @@ -85,6 +93,7 @@ public class AStar{ openNode.setF(f); PathfinderUtils.openNodes.add(openNode); + map.repaint(); } public Node getNextBestNode() { @@ -94,7 +103,8 @@ public class AStar{ } map.setFinished(true); - map.repaint(); + map.setRunning(false); + JOptionPane.showMessageDialog(null, "No path found!", "Pathfinder error", JOptionPane.ERROR_MESSAGE); return null; } } diff --git a/src/main/java/Main/Map.java b/src/main/java/Main/Map.java index 1d30711..6b15afc 100644 --- a/src/main/java/Main/Map.java +++ b/src/main/java/Main/Map.java @@ -20,7 +20,9 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe private boolean isFinished = false; private boolean running = false; + private int size = 20; + int speed = 10; public Map() { this.setBackground(new Color(40, 40, 40)); @@ -72,16 +74,18 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); } - // Draws open nodes + // Draws external nodes g.setColor(new Color(69, 133, 136)); - for (Node node : PathfinderUtils.openNodes) { - g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); + for (int i = 0; i < PathfinderUtils.openNodes.size(); i++) { + g.fillRect(PathfinderUtils.openNodes.get(i).getX() + 1, PathfinderUtils.openNodes.get(i).getY() + 1, + size - 1, size - 1); } - // Draws closed nodes + // Draws internal nodes g.setColor(new Color(77, 77, 77)); - for (Node node : PathfinderUtils.closedNodes) { - g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); + for (int i = 0; i < PathfinderUtils.closedNodes.size(); i++) { + g.fillRect(PathfinderUtils.closedNodes.get(i).getX() + 1, PathfinderUtils.closedNodes.get(i).getY() + 1, + size - 1, size - 1); } // if path is found start drawing the shortest path @@ -96,77 +100,93 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe // Drawing on the grid public void mapDrawing(MouseEvent e) { // Creating nodes + Node node = new Node(e.getX() - (e.getX() % size), e.getY() - (e.getY() % size)); + if (SwingUtilities.isLeftMouseButton(e)) { + // Start node if (key == 's') { - int posX = e.getX() - (e.getX() % size); - int posY = e.getY() - (e.getY() % size); - - 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); + // If both start and end node don't exist just create the start node + if (PathfinderUtils.startNode == null && PathfinderUtils.endNode == null) + // Set the node as the start node + PathfinderUtils.startNode = node; + + // If the end node exists check if the node is the same else create the start + // node + else if (PathfinderUtils.endNode != null && PathfinderUtils.startNode == null) { + if (PathfinderUtils.isSameNode(node, PathfinderUtils.endNode)) { + // Send an error message saying that start and end node are the same + JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", + "Same node error", JOptionPane.ERROR_MESSAGE); return; } - 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); + + // Set the node as the start node + PathfinderUtils.startNode = node; + + } + + // If they both exist check if same node else move che start node + else { + if (PathfinderUtils.isSameNode(node, PathfinderUtils.endNode)) { + JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", + "Same node error", JOptionPane.ERROR_MESSAGE); return; } - PathfinderUtils.startNode.setX(posX); - PathfinderUtils.startNode.setY(posY); + // Change start node coordinates without creating a new node + PathfinderUtils.startNode.setX(node.getX()); + PathfinderUtils.startNode.setY(node.getY()); } - - repaint(); } + // End node else if (key == 'e') { - int posX = e.getX() - (e.getX() % size); - int posY = e.getY() - (e.getY() % size); - - 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); + // If both start and end node don't exist just create the end node + if (PathfinderUtils.startNode == null && PathfinderUtils.endNode == null) + // Set the node as the end node + PathfinderUtils.endNode = node; + + // If the start node exists check if the node is the same else create the end node + else if (PathfinderUtils.startNode != null && PathfinderUtils.endNode == null) { + if (PathfinderUtils.isSameNode(node, PathfinderUtils.startNode)) { + // Send an error message saying that start and end node are the same + JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", + "Same node error", JOptionPane.ERROR_MESSAGE); return; } - 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); + + // Set the node as the end node + PathfinderUtils.endNode = node; + + } + // If they both exist check if same node else move the end node + else { + if (PathfinderUtils.startNode != null && PathfinderUtils.endNode == null) { + JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", + "Same node error", JOptionPane.ERROR_MESSAGE); return; } - PathfinderUtils.endNode.setX(posX); - PathfinderUtils.endNode.setY(posY); + // Change end node coordinates without creating a new node + PathfinderUtils.endNode.setX(node.getX()); + PathfinderUtils.endNode.setY(node.getY()); } - - repaint(); } + // Barriers else { - int posX = e.getX() - (e.getX() % size); - int posY = e.getY() - (e.getY() % size); - Node barrierNode = new Node(posX, posY); - - if (PathfinderUtils.startNode != null) - if (PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.startNode)) - return; - if (PathfinderUtils.endNode != null) - if (PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.endNode)) - return; - - PathfinderUtils.barriers.add(barrierNode); + // If the start/end node exists check if the node is the same + // If it is return + if (PathfinderUtils.startNode != null && PathfinderUtils.isSameNode(node, PathfinderUtils.startNode)) + return; + if (PathfinderUtils.endNode != null && PathfinderUtils.isSameNode(node, PathfinderUtils.endNode)) + return; - repaint(); + PathfinderUtils.barriers.add(node); } + + // Update the UI with barrier/start/end node + repaint(); } // Deleting nodes @@ -177,14 +197,12 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe if (key == 's' && PathfinderUtils.startNode != null) { if (PathfinderUtils.startNode.getX() == posX && PathfinderUtils.startNode.getY() == posY) { PathfinderUtils.startNode = null; - repaint(); } } else if (key == 'e' && PathfinderUtils.endNode != null) { if (PathfinderUtils.endNode.getX() == posX && PathfinderUtils.endNode.getY() == posY) { PathfinderUtils.endNode = null; - repaint(); } } @@ -194,8 +212,9 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe if (nodeID != -1) { PathfinderUtils.remove(nodeID); } - repaint(); } + + repaint(); } } @@ -206,13 +225,15 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe 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.showMessageDialog(null, "Missing start or end node", "Missing node", JOptionPane.ERROR_MESSAGE); return; } running = true; - new AStar(this).start(); + + // AStar needs to extend runnable because otherwise map.repaint() will wait to update the UI + new Thread(new AStar(this)).start(); } else if (running == false && isFinished == true) { PathfinderUtils.barriers.clear(); PathfinderUtils.openNodes.clear(); diff --git a/target/PathVisualizer-1.0-SNAPSHOT.jar b/target/PathVisualizer-1.0-SNAPSHOT.jar index 50e97bd..81eb8c0 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 8fb8818..7861da6 100644 Binary files a/target/classes/Main/AStar.class and b/target/classes/Main/AStar.class differ diff --git a/target/classes/Main/App.class b/target/classes/Main/App.class index 13e30fb..5d655f5 100644 Binary files a/target/classes/Main/App.class and b/target/classes/Main/App.class differ diff --git a/target/classes/Main/Map.class b/target/classes/Main/Map.class index e9a8060..d0ad2d5 100644 Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ diff --git a/target/classes/Main/Node.class b/target/classes/Main/Node.class index 3ec5964..930f295 100644 Binary files a/target/classes/Main/Node.class and b/target/classes/Main/Node.class differ diff --git a/target/classes/Main/PathfinderUtils.class b/target/classes/Main/PathfinderUtils.class index ef6af34..a6ec000 100644 Binary files a/target/classes/Main/PathfinderUtils.class and b/target/classes/Main/PathfinderUtils.class differ diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties index 6be0d6d..b1908d5 100644 --- a/target/maven-archiver/pom.properties +++ b/target/maven-archiver/pom.properties @@ -1,4 +1,4 @@ -#Created by Apache Maven 3.3.9 +#Created by Apache Maven 3.6.3 groupId=Main artifactId=PathVisualizer version=1.0-SNAPSHOT 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 a000219..cbf65bb 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,5 +1,5 @@ -/home/leo/Docs/Proj/pathfinding-visualizer/src/main/java/Main/PathfinderUtils.java -/home/leo/Docs/Proj/pathfinding-visualizer/src/main/java/Main/Node.java -/home/leo/Docs/Proj/pathfinding-visualizer/src/main/java/Main/App.java -/home/leo/Docs/Proj/pathfinding-visualizer/src/main/java/Main/Map.java -/home/leo/Docs/Proj/pathfinding-visualizer/src/main/java/Main/AStar.java +/home/leo/Docs/Proj/Pathfinder/src/main/java/Main/Node.java +/home/leo/Docs/Proj/Pathfinder/src/main/java/Main/App.java +/home/leo/Docs/Proj/Pathfinder/src/main/java/Main/PathfinderUtils.java +/home/leo/Docs/Proj/Pathfinder/src/main/java/Main/Map.java +/home/leo/Docs/Proj/Pathfinder/src/main/java/Main/AStar.java diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst index e1f0ebc..2af0a5f 100644 --- a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst +++ b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst @@ -1 +1 @@ -/home/leo/Docs/Proj/pathfinding-visualizer/src/test/java/Main/AppTest.java +/home/leo/Docs/Proj/Pathfinder/src/test/java/Main/AppTest.java diff --git a/target/surefire-reports/Main.AppTest.txt b/target/surefire-reports/Main.AppTest.txt index 53a7fdf..49e3cc6 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.036 s - in Main.AppTest +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.03 s - in Main.AppTest diff --git a/target/surefire-reports/TEST-Main.AppTest.xml b/target/surefire-reports/TEST-Main.AppTest.xml index d0700b0..bc761a0 100644 --- a/target/surefire-reports/TEST-Main.AppTest.xml +++ b/target/surefire-reports/TEST-Main.AppTest.xml @@ -1,54 +1,61 @@ - + - + + + - + - + + + - - - + + - + - - + + - - + + - - + + + - + - + + - + - - + + + + - - + + - + \ No newline at end of file diff --git a/target/test-classes/Main/AppTest.class b/target/test-classes/Main/AppTest.class index a08e3e3..6b3f4e2 100644 Binary files a/target/test-classes/Main/AppTest.class and b/target/test-classes/Main/AppTest.class differ