]> git.leonardobizzoni.com Git - astar-visualizer/commitdiff
Changed UI and added ability to delete nodes
authorLeonardoBizzoni <leo2002714@gmail.com>
Thu, 17 Jun 2021 16:19:09 +0000 (18:19 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Thu, 17 Jun 2021 16:19:09 +0000 (18:19 +0200)
src/main/java/Main/App.java
src/main/java/Main/Map.java
src/main/java/Main/PathfinderUtils.java [new file with mode: 0644]
target/PathVisualizer-1.0-SNAPSHOT.jar
target/classes/Main/Map.class
target/classes/Main/PathfinderUtils.class [new file with mode: 0644]
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 5e562da279fb22a00dfdf3c9c9b53e1c03912683..5d955ba82f110cc10ac49c2a4cce01f173395679 100644 (file)
@@ -5,5 +5,5 @@ public class App {
     public static void main(String[] args) {
         new Map();
     }
-    
+
 }
index 4c24ad54c553d2cf24e86bf52146786ae3d473ce..912cf19cb4046022dfa66cae085b81e531a8516f 100644 (file)
@@ -15,6 +15,7 @@
 package Main;
 
 import javax.swing.JFrame;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.SwingUtilities;
 
@@ -23,8 +24,6 @@ import java.awt.Graphics;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionListener;
-import java.util.ArrayList;
-import java.util.List;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 
@@ -33,16 +32,14 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe
     private int size = 30;
     private char key = (char) 0;
 
-    private Node startNode, endNode;
-    private List<Node> barriers = new ArrayList<>();
-
     public Map() {
+        this.setBackground(new Color(40, 40, 40));
         addMouseListener(this);
         addKeyListener(this);
         setFocusable(true);
         addMouseMotionListener(this);
 
-        // Settings up the window
+        // Setting up the window
         window = new JFrame();
         window.setContentPane(this);
         window.setTitle("Pathfinding Algorithm Visualizer");
@@ -58,7 +55,7 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe
         super.paintComponent(g);
 
         // Draws the grid
-        g.setColor(Color.lightGray);
+        g.setColor(new Color(50, 48, 47));
         for (int i = 0; i < this.getWidth(); i += size) {
             for (int j = 0; j < this.getHeight(); j += size) {
                 g.drawRect(i, j, size, size);
@@ -66,65 +63,116 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe
         }
 
         // Draws start node
-        if (startNode != null) {
+        if (PathfinderUtils.startNode != null) {
             g.setColor(new Color(117, 110, 202));
-            g.fillRect(startNode.getX() + 1, startNode.getY() + 1, size - 1, size - 1);
+            g.fillRect(PathfinderUtils.startNode.getX() + 1, PathfinderUtils.startNode.getY() + 1, size - 1, size - 1);
         }
 
         // Draws end node
-        if (endNode != null) {
+        if (PathfinderUtils.endNode != null) {
             g.setColor(new Color(204, 36, 29));
-            g.fillRect(endNode.getX() + 1, endNode.getY() + 1, size - 1, size - 1);
+            g.fillRect(PathfinderUtils.endNode.getX() + 1, PathfinderUtils.endNode.getY() + 1, size - 1, size - 1);
         }
 
         // Draws barrier nodes
-        g.setColor(new Color(40, 40, 40));
-        for (Node node : barriers) {
+        g.setColor(new Color(235, 219, 178));
+        for (Node node : PathfinderUtils.barriers) {
             g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
         }
     }
 
     // Drawing on the grid
     public void mapDrawing(MouseEvent e) {
+        // Creating nodes
         if (SwingUtilities.isLeftMouseButton(e)) {
             if (key == 's') {
-                int posX = e.getX() % size;
-                int posY = e.getY() % size;
+                int posX = e.getX() - (e.getX() % size);
+                int posY = e.getY() - (e.getY() % size);
+
+                // TODO fa cagare
+                // Checks if start node and end node are the same
+                if (PathfinderUtils.endNode != null) {
+                    if (PathfinderUtils.endNode.getX() == posX && PathfinderUtils.endNode.getY() == posY) {
+                        JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", "Same node error", JOptionPane.ERROR_MESSAGE);
+                        return;
+                    }
+                }
 
-                if (startNode == null) {
-                    startNode = new Node(e.getX() - posX, e.getY() - posY);
+                if (PathfinderUtils.startNode == null) {
+                    PathfinderUtils.startNode = new Node(posX, posY);
                 } else {
-                    startNode.setX(e.getX() - posX);
-                    startNode.setY(e.getY() - posY);
+                    PathfinderUtils.startNode.setX(posX);
+                    PathfinderUtils.startNode.setY(posY);
                 }
 
                 repaint();
             }
 
             else if (key == 'e') {
-                int posX = e.getX() % size;
-                int posY = e.getY() % size;
+                int posX = e.getX() - (e.getX() % size);
+                int posY = e.getY() - (e.getY() % size);
+
+                // TODO fa cagare
+                // Checks if end node and start node are the same
+                if (PathfinderUtils.startNode != null) {
+                    if (PathfinderUtils.startNode.getX() == posX && PathfinderUtils.startNode.getY() == posY) {
+                        JOptionPane.showMessageDialog(null, "End node and start node can't be the same node!", "SAME NODE ERROR", JOptionPane.ERROR_MESSAGE);
+                        return;
+                    }
+                }
 
-                if (endNode == null) {
-                    endNode = new Node(e.getX() - posX, e.getY() - posY);
+                if (PathfinderUtils.endNode == null) {
+                    PathfinderUtils.endNode = new Node(posX, posY);
                 } else {
-                    endNode.setX(e.getX() - posX);
-                    endNode.setY(e.getY() - posY);
+                    PathfinderUtils.endNode.setX(posX);
+                    PathfinderUtils.endNode.setY(posY);
                 }
 
                 repaint();
             }
 
             else {
-                int posX = e.getX() % size;
-                int posY = e.getY() % size;
+                int posX = e.getX() - (e.getX() % size);
+                int posY = e.getY() - (e.getY() % size);
+                Node barrierNode = new Node(posX, posY);
+
+                // TODO controlla se si sta creando una barriera su start o end
+
+
+                PathfinderUtils.barriers.add(barrierNode);
+
+                repaint();
+            }
+        }
 
-                barriers.add(new Node(e.getX() - posX, e.getY() - posY));
+        // Deleting nodes
+        else if (SwingUtilities.isRightMouseButton(e)) {
+            int posX = e.getX() - (e.getX() % size);
+            int posY = e.getY() - (e.getY() % size);
 
+            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();
+                }
+            }
+
+            else {
+                int nodeID = PathfinderUtils.locate(posX, posY);
+
+                if (nodeID != -1) {
+                    PathfinderUtils.remove(nodeID);
+                }
                 repaint();
             }
         }
-        // TODO cancellare le barriere col tasto destro del mouse
     }
 
     @Override
diff --git a/src/main/java/Main/PathfinderUtils.java b/src/main/java/Main/PathfinderUtils.java
new file mode 100644 (file)
index 0000000..5604538
--- /dev/null
@@ -0,0 +1,22 @@
+package Main;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PathfinderUtils {
+    static List<Node> barriers = new ArrayList<>();
+    static Node startNode, endNode;
+
+    static int locate(int x, int y) {
+        for (int i = 0; i < barriers.size(); i++) {
+            if(barriers.get(i).getX() == x && barriers.get(i).getY() == y)
+                return i;
+        }
+
+        return -1;
+    }
+
+    static void remove(int id) {
+        barriers.remove(id);
+    }
+}
index 406f8f9e75a312c81d28476fbb9aec14cf315f67..bf40587b1fa9d81903156ac14c690018aa04c22a 100644 (file)
Binary files a/target/PathVisualizer-1.0-SNAPSHOT.jar and b/target/PathVisualizer-1.0-SNAPSHOT.jar differ
index 34b60c569609036a36cd3e93f5622594ea220224..d1fe5a22435903ecc65abdb02597ee593dc34156 100644 (file)
Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ
diff --git a/target/classes/Main/PathfinderUtils.class b/target/classes/Main/PathfinderUtils.class
new file mode 100644 (file)
index 0000000..78aa867
Binary files /dev/null and b/target/classes/Main/PathfinderUtils.class differ
index 35be478c6086622ed845ea528d76e649c2e7f14d..c09b5f3cdaeacc955ef26ef6df3feef495c77d71 100644 (file)
@@ -1,3 +1,4 @@
 /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/App.java
+/home/leo/Docs/Proj/PathVisualizer/src/main/java/Main/PathfinderUtils.java
index 9c021fa55d59e46447aecdae15fcb21e2430f358..815c74ad10b30c04bd7a1d49279b7996993b7d2d 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.027 s - in Main.AppTest
index afe46ad821e9412d254e6dd3bc3063e31b5a9f2a..50af6480ce5f887fb82ba79f95ffa16437900a04 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.027" 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/surefirebooter16417584190550890545.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-17T11-55-55_292-jvmRun1 surefire4864576372469895393tmp surefire_01966433911489221877tmp"/>
+    <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter514838881418717725.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-17T18-00-26_353-jvmRun1 surefire13060722636073724137tmp surefire_07869077185134568567tmp"/>
     <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/surefirebooter16417584190550890545.jar"/>
+    <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter514838881418717725.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