]> git.leonardobizzoni.com Git - astar-visualizer/commitdiff
Final A*
authorLeonardoBizzoni <leo2002714@gmail.com>
Fri, 2 Jul 2021 07:20:49 +0000 (09:20 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Fri, 2 Jul 2021 07:20:49 +0000 (09:20 +0200)
src/main/java/Main/AStar.java
src/main/java/Main/ControlPanel.java [deleted file]
src/main/java/Main/Map.java
target/PathVisualizer-1.0-SNAPSHOT.jar
target/classes/Main/AStar.class
target/classes/Main/ControlPanel.class [deleted file]
target/classes/Main/Map.class
target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst
target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
target/surefire-reports/Main.AppTest.txt
target/surefire-reports/TEST-Main.AppTest.xml

index 333e84a5888dc93cf2e6624630cd86de733b84dc..1625baf6c557db84969a8d873c776081cefb2754 100644 (file)
@@ -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 (file)
index 30e7aa4..0000000
+++ /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<String> 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);
-    }
-}
index c92abfe91ee57126a8e957a0a1312dcfba438164..c2d94e55be7c86b65d270114794b9bce403af6e0 100644 (file)
@@ -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) {
     }
index 7079e78d0af7ac313cbab28dc5674f69b5c98585..365027e5af47c8ee82876d7e5d0e04ff6bf7c779 100644 (file)
Binary files a/target/PathVisualizer-1.0-SNAPSHOT.jar and b/target/PathVisualizer-1.0-SNAPSHOT.jar differ
index b96485953030c467b2c3388d72091fc1c63494c2..045216018ebc3e349e1fcd2efc2ada63079923a1 100644 (file)
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 (file)
index 865752d..0000000
Binary files a/target/classes/Main/ControlPanel.class and /dev/null differ
index a6a0f83c97daacf11e102ee6e36d4dbb9a82fdc8..bb875742982432dbd0fac47c3058cbeb0ecf4a70 100644 (file)
Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ
index b1dccf06b9b3d1b6c61bfc2ec791098bffc88fc3..e2dbad22775391ff4d45139036e12cbd8756e3be 100644 (file)
@@ -1,6 +1,5 @@
 Main/Node.class
 Main/AStar.class
-Main/ControlPanel.class
 Main/Map.class
 Main/App.class
 Main/PathfinderUtils.class
index cdd74a9efead34156a15a8d5baa5f65cc30a91d3..cc3946dcdafcaa71064aff5f6319f99be6755177 100644 (file)
@@ -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
index 9c021fa55d59e46447aecdae15fcb21e2430f358..4193956ad376161ae8aed170e350e2f2454be268 100644 (file)
@@ -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
index 07c98c85d99cf2f4fa123aced9db1cc5f616c7ff..b9ce485d69de5c498e4fb17fe6477bad70de3120 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.028" 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.024" 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/surefirebooter7869637901010268867.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-07-01T10-30-57_583-jvmRun1 surefire18123134203155873160tmp surefire_012477696981176381238tmp"/>
+    <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter8825560741906188442.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-07-02T09-18-56_069-jvmRun1 surefire1726243427666730328tmp surefire_02585865431578143327tmp"/>
     <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/surefirebooter7869637901010268867.jar"/>
+    <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter8825560741906188442.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.002"/>
+  <testcase name="shouldAnswerWithTrue" classname="Main.AppTest" time="0.001"/>
 </testsuite>
\ No newline at end of file