]> git.leonardobizzoni.com Git - astar-visualizer/commitdiff
Clean up
authorLeonardoBizzoni <leo2002714@gmail.com>
Thu, 1 Jul 2021 08:38:49 +0000 (10:38 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Thu, 1 Jul 2021 08:38:49 +0000 (10:38 +0200)
src/main/java/Main/AStar.java
src/main/java/Main/Map.java
target/PathVisualizer-1.0-SNAPSHOT.jar
target/classes/Main/AStar.class
target/classes/Main/Map.class
target/surefire-reports/Main.AppTest.txt
target/surefire-reports/TEST-Main.AppTest.xml

index 62aa12861a86a9c7cffc32850bfc7887b26683d6..333e84a5888dc93cf2e6624630cd86de733b84dc 100644 (file)
@@ -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;
     }
 }
index e63983d6cf87626e68f12002a91af345cce0ba8f..c92abfe91ee57126a8e957a0a1312dcfba438164 100644 (file)
@@ -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;
 
index c080b58abe2c60b61b835439ba8217d266f24b4a..7079e78d0af7ac313cbab28dc5674f69b5c98585 100644 (file)
Binary files a/target/PathVisualizer-1.0-SNAPSHOT.jar and b/target/PathVisualizer-1.0-SNAPSHOT.jar differ
index 01b3d6cc68535b2c47ca86c5e0694b0a40414633..b96485953030c467b2c3388d72091fc1c63494c2 100644 (file)
Binary files a/target/classes/Main/AStar.class and b/target/classes/Main/AStar.class differ
index d729a4180b1e23700c5a13756a20d73e8e822497..a6a0f83c97daacf11e102ee6e36d4dbb9a82fdc8 100644 (file)
Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ
index c1e1e010e1a605c9dc2dee9fbe4f0ad6a93e2a36..9c021fa55d59e46447aecdae15fcb21e2430f358 100644 (file)
@@ -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
index 7752e56328f2b040bdf0878132bf73794f0ae79f..07c98c85d99cf2f4fa123aced9db1cc5f616c7ff 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="Main.AppTest" time="0.019" tests="1" errors="0" skipped="0" failures="0">
+<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="Main.AppTest" time="0.028" tests="1" errors="0" skipped="0" failures="0">
   <properties>
     <property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
     <property name="java.specification.version" value="11"/>
@@ -16,7 +16,7 @@
     <property name="sun.java.launcher" value="SUN_STANDARD"/>
     <property name="user.country" value="GB"/>
     <property name="sun.boot.library.path" value="/opt/openjdk-bin-11.0.11_p9/lib"/>
-    <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter3942171315997095630.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-30T11-56-47_958-jvmRun1 surefire2991361844254018063tmp surefire_012996660621204474861tmp"/>
+    <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter7869637901010268867.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-07-01T10-30-57_583-jvmRun1 surefire18123134203155873160tmp surefire_012477696981176381238tmp"/>
     <property name="jdk.debug" value="release"/>
     <property name="surefire.test.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/test-classes:/home/leo/Docs/Proj/PathVisualizer/target/classes:/home/leo/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/leo/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/>
     <property name="sun.cpu.endian" value="little"/>
@@ -32,7 +32,7 @@
     <property name="java.specification.name" value="Java Platform API Specification"/>
     <property name="java.vm.specification.vendor" value="Oracle Corporation"/>
     <property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
-    <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter3942171315997095630.jar"/>
+    <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter7869637901010268867.jar"/>
     <property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
     <property name="java.runtime.version" value="11.0.11+9"/>
     <property name="user.name" value="leo"/>
@@ -59,5 +59,5 @@
     <property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
     <property name="java.class.version" value="55.0"/>
   </properties>
-  <testcase name="shouldAnswerWithTrue" classname="Main.AppTest" time="0.001"/>
+  <testcase name="shouldAnswerWithTrue" classname="Main.AppTest" time="0.002"/>
 </testsuite>
\ No newline at end of file