From: LeonardoBizzoni Date: Thu, 1 Jul 2021 08:38:49 +0000 (+0200) Subject: Clean up X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=9663557444611dd2329bd80abb5b50267b0c6ebe;p=astar-visualizer Clean up --- diff --git a/src/main/java/Main/AStar.java b/src/main/java/Main/AStar.java index 62aa128..333e84a 100644 --- a/src/main/java/Main/AStar.java +++ b/src/main/java/Main/AStar.java @@ -2,10 +2,11 @@ package Main; public class AStar{ private Map map; + private int x, y; public AStar(Map map) { this.map = map; - PathfinderUtils.path.removeAll(PathfinderUtils.path); + PathfinderUtils.path.clear(); } public void start() { @@ -14,18 +15,19 @@ public class AStar{ public void searchPath(Node parent) { for (int i = 0; i < 4; i++) { - int x = (int) Math.round(parent.getX() + (-map.size* Math.cos((Math.PI / 2) * i))); - int y = (int) Math.round(parent.getY() + (-map.size* Math.sin((Math.PI / 2) * i))); + x = (int) Math.round(parent.getX() + (-map.size* Math.cos((Math.PI / 2) * i))); + y = (int) Math.round(parent.getY() + (-map.size* Math.sin((Math.PI / 2) * i))); calculateOpenNode(x, y, parent); } - parent = getNextBestNode(); + if((parent = getNextBestNode()) == null) + return; PathfinderUtils.closedNodes.add(parent); PathfinderUtils.openNodes.remove(parent); - if (!map.isFinished) + if (!map.isFinished) searchPath(parent); } @@ -86,7 +88,14 @@ public class AStar{ } public Node getNextBestNode() { - PathfinderUtils.sort(); - return PathfinderUtils.openNodes.get(0); + if (PathfinderUtils.openNodes.size() > 0) { + PathfinderUtils.sort(); + return PathfinderUtils.openNodes.get(0); + } + + map.isFinished = true; + ControlPanel.toggleRunBtn.setText("Clear"); + map.repaint(); + return null; } } diff --git a/src/main/java/Main/Map.java b/src/main/java/Main/Map.java index e63983d..c92abfe 100644 --- a/src/main/java/Main/Map.java +++ b/src/main/java/Main/Map.java @@ -16,7 +16,8 @@ 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.MouseEvent; +import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; @@ -33,7 +34,7 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi static int height; boolean isFinished = false; - int size = 30; + int size = 20; public Map() { this.setBackground(new Color(40, 40, 40)); @@ -83,7 +84,7 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi } // Draws barrier nodes - g.setColor(new Color(235, 219, 178)); + g.setColor(Color.white); for (Node node : PathfinderUtils.barriers) { g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); } @@ -166,11 +167,11 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi int posY = e.getY() - (e.getY() % size); Node barrierNode = new Node(posX, posY); - if(PathfinderUtils.startNode != null) - if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.startNode)) + if (PathfinderUtils.startNode != null) + if (PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.startNode)) return; - if(PathfinderUtils.endNode != null) - if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.endNode)) + if (PathfinderUtils.endNode != null) + if (PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.endNode)) return; PathfinderUtils.barriers.add(barrierNode); @@ -232,8 +233,11 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi @Override public void actionPerformed(ActionEvent e) { if (ControlPanel.toggleRunBtn.getText().equals("Run")) { - switch (ControlPanel.algo.getItemAt(ControlPanel.algo.getSelectedIndex())) { + if (PathfinderUtils.startNode == null || PathfinderUtils.endNode == null) + return; + + switch (ControlPanel.algo.getItemAt(ControlPanel.algo.getSelectedIndex())) { case ("A*"): new AStar(this).start(); break; @@ -255,10 +259,10 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi } } - else if(ControlPanel.toggleRunBtn.getText().equals("Clear")){ - PathfinderUtils.barriers.removeAll(PathfinderUtils.barriers); - PathfinderUtils.openNodes.removeAll(PathfinderUtils.openNodes); - PathfinderUtils.closedNodes.removeAll(PathfinderUtils.closedNodes); + else if (ControlPanel.toggleRunBtn.getText().equals("Clear")) { + PathfinderUtils.barriers.clear(); + PathfinderUtils.openNodes.clear(); + PathfinderUtils.closedNodes.clear(); PathfinderUtils.startNode = null; PathfinderUtils.endNode = null; diff --git a/target/PathVisualizer-1.0-SNAPSHOT.jar b/target/PathVisualizer-1.0-SNAPSHOT.jar index c080b58..7079e78 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 01b3d6c..b964859 100644 Binary files a/target/classes/Main/AStar.class and b/target/classes/Main/AStar.class differ diff --git a/target/classes/Main/Map.class b/target/classes/Main/Map.class index d729a41..a6a0f83 100644 Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ diff --git a/target/surefire-reports/Main.AppTest.txt b/target/surefire-reports/Main.AppTest.txt index c1e1e01..9c021fa 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.019 s - in Main.AppTest +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 s - in Main.AppTest diff --git a/target/surefire-reports/TEST-Main.AppTest.xml b/target/surefire-reports/TEST-Main.AppTest.xml index 7752e56..07c98c8 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