From: LeonardoBizzoni Date: Sat, 20 Nov 2021 08:08:13 +0000 (+0100) Subject: Clean up X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=ec165069db5e2b2088b2551c860e077076e5ec34;p=astar-visualizer Clean up --- diff --git a/src/main/java/Main/AStar.java b/src/main/java/Main/AStar.java index 0639312..db68bbd 100644 --- a/src/main/java/Main/AStar.java +++ b/src/main/java/Main/AStar.java @@ -25,13 +25,13 @@ public class AStar implements Runnable { calculateOpenNode(x, y, parent); } + map.repaint(); if ((parent = getNextBestNode()) == null) return; PathfinderUtils.closedNodes.add(parent); PathfinderUtils.openNodes.remove(parent); - map.repaint(); try { Thread.sleep(map.speed); } catch (InterruptedException e) { diff --git a/src/main/java/Main/Map.java b/src/main/java/Main/Map.java index 5a7fdc2..00d4a32 100644 --- a/src/main/java/Main/Map.java +++ b/src/main/java/Main/Map.java @@ -1,8 +1,8 @@ package Main; import java.awt.Color; -import java.awt.Graphics; import java.awt.Dimension; +import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; @@ -22,10 +22,10 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe private boolean running = false; private int size = 20; - int speed = 2; + int speed = 5; public Map() { - this.setBackground(new Color(40, 40, 40)); + setBackground(new Color(40, 40, 40)); addMouseListener(this); addKeyListener(this); @@ -41,8 +41,8 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe window.pack(); window.setVisible(true); - this.revalidate(); - this.repaint(); + revalidate(); + repaint(); } public void paintComponent(Graphics g) { @@ -50,8 +50,8 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe // Draws the grid 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) { + for (int i = 0; i < getWidth(); i += size) { + for (int j = 0; j < getHeight(); j += size) { g.drawRect(i, j, size, size); } } @@ -115,21 +115,18 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe 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); + JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", "Same node error", JOptionPane.ERROR_MESSAGE); return; } // Set the node as the start node PathfinderUtils.startNode = node; - } - // If they both exist check if same node else move che start node + // If the start node exists check if end node exist and move the start else { if (PathfinderUtils.endNode != null && 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); + JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", "Same node error", JOptionPane.ERROR_MESSAGE); return; } @@ -150,8 +147,7 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe 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); + JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", "Same node error", JOptionPane.ERROR_MESSAGE); return; } @@ -159,11 +155,10 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe PathfinderUtils.endNode = node; } - // If they both exist check if same node else move the end node + // If the end node exists check if start node exist and move the start else { if (PathfinderUtils.startNode !=null && PathfinderUtils.isSameNode(node, PathfinderUtils.startNode)) { - JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", - "Same node error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", "Same node error", JOptionPane.ERROR_MESSAGE); return; } @@ -184,55 +179,53 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe PathfinderUtils.barriers.add(node); } - - // Update the UI with barrier/start/end node - repaint(); } // 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) { + if (PathfinderUtils.isSameNode(node, PathfinderUtils.startNode)) { PathfinderUtils.startNode = null; } } else if (key == 'e' && PathfinderUtils.endNode != null) { - if (PathfinderUtils.endNode.getX() == posX && PathfinderUtils.endNode.getY() == posY) { + if (PathfinderUtils.isSameNode(node, PathfinderUtils.endNode)) { PathfinderUtils.endNode = null; } } else { - int nodeID = PathfinderUtils.locateBarrier(posX, posY); + int nodeID = PathfinderUtils.locateBarrier(node.getX(), node.getY()); if (nodeID != -1) { PathfinderUtils.remove(nodeID); } } - repaint(); } + + repaint(); } @Override public void keyPressed(KeyEvent e) { key = e.getKeyChar(); + if (key == 'q') { + System.exit(0); + } + if (key == KeyEvent.VK_SPACE) { if (running == false && isFinished == false) { if (PathfinderUtils.startNode == null || PathfinderUtils.endNode == null) { - JOptionPane.showMessageDialog(null, "Missing start or end node", "Missing node", - JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "Missing start or end node", "Missing node", JOptionPane.ERROR_MESSAGE); return; } running = true; - // AStar needs to extend runnable because otherwise map.repaint() will wait to update the UI + // AStar needs to extend runnable because otherwise calling map.repaint() will wait for AStar to complete the pathfinding before updating the UI new Thread(new AStar(this)).start(); } else if (running == false && isFinished == true) { PathfinderUtils.barriers.clear(); @@ -242,9 +235,8 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe PathfinderUtils.endNode = null; isFinished = false; - running = false; - this.repaint(); + repaint(); } } } diff --git a/target/PathVisualizer-1.0-SNAPSHOT.jar b/target/PathVisualizer-1.0-SNAPSHOT.jar index b8d5142..d3bf56f 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 7861da6..5a7dca5 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 5d655f5..13e30fb 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 c18b509..4ddee13 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 930f295..3ec5964 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 a6ec000..ef6af34 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 b1908d5..6be0d6d 100644 --- a/target/maven-archiver/pom.properties +++ b/target/maven-archiver/pom.properties @@ -1,4 +1,4 @@ -#Created by Apache Maven 3.6.3 +#Created by Apache Maven 3.3.9 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 cbf65bb..a000219 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/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 +/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 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 2af0a5f..e1f0ebc 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/Pathfinder/src/test/java/Main/AppTest.java +/home/leo/Docs/Proj/pathfinding-visualizer/src/test/java/Main/AppTest.java diff --git a/target/surefire-reports/Main.AppTest.txt b/target/surefire-reports/Main.AppTest.txt index 3aed947..67bc74a 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.025 s - in Main.AppTest +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 s - in Main.AppTest diff --git a/target/surefire-reports/TEST-Main.AppTest.xml b/target/surefire-reports/TEST-Main.AppTest.xml index 595731f..1598d02 100644 --- a/target/surefire-reports/TEST-Main.AppTest.xml +++ b/target/surefire-reports/TEST-Main.AppTest.xml @@ -1,61 +1,54 @@ - + - - - + - + - - - + + - - + + - + - - + + - - + + - - - + + - + - - + - - + + - - + - - + - + \ No newline at end of file diff --git a/target/test-classes/Main/AppTest.class b/target/test-classes/Main/AppTest.class index 6b3f4e2..a08e3e3 100644 Binary files a/target/test-classes/Main/AppTest.class and b/target/test-classes/Main/AppTest.class differ