From: LeonardoBizzoni Date: Thu, 17 Jun 2021 16:19:09 +0000 (+0200) Subject: Changed UI and added ability to delete nodes X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=9aa926644a82509289500e02b09d8adf67370895;p=astar-visualizer Changed UI and added ability to delete nodes --- diff --git a/src/main/java/Main/App.java b/src/main/java/Main/App.java index 5e562da..5d955ba 100644 --- a/src/main/java/Main/App.java +++ b/src/main/java/Main/App.java @@ -5,5 +5,5 @@ public class App { public static void main(String[] args) { new Map(); } - + } diff --git a/src/main/java/Main/Map.java b/src/main/java/Main/Map.java index 4c24ad5..912cf19 100644 --- a/src/main/java/Main/Map.java +++ b/src/main/java/Main/Map.java @@ -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 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 index 0000000..5604538 --- /dev/null +++ b/src/main/java/Main/PathfinderUtils.java @@ -0,0 +1,22 @@ +package Main; + +import java.util.ArrayList; +import java.util.List; + +public class PathfinderUtils { + static List 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); + } +} diff --git a/target/PathVisualizer-1.0-SNAPSHOT.jar b/target/PathVisualizer-1.0-SNAPSHOT.jar index 406f8f9..bf40587 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/Map.class b/target/classes/Main/Map.class index 34b60c5..d1fe5a2 100644 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 index 0000000..78aa867 Binary files /dev/null and b/target/classes/Main/PathfinderUtils.class differ diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst index 37eadb9..0fc7d35 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -1,3 +1,4 @@ Main/Node.class Main/Map.class Main/App.class +Main/PathfinderUtils.class 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 35be478..c09b5f3 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,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 diff --git a/target/surefire-reports/Main.AppTest.txt b/target/surefire-reports/Main.AppTest.txt index 9c021fa..815c74a 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.028 s - in Main.AppTest +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 s - in Main.AppTest diff --git a/target/surefire-reports/TEST-Main.AppTest.xml b/target/surefire-reports/TEST-Main.AppTest.xml index afe46ad..50af648 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