public AStar(Map map) {
this.map = map;
+ PathfinderUtils.path.removeAll(PathfinderUtils.path);
}
public void start() {
if (nextX == PathfinderUtils.startNode.getX() && nextY == PathfinderUtils.startNode.getY())
return;
if (nextX == PathfinderUtils.endNode.getX() && nextY == PathfinderUtils.endNode.getY()) {
+ PathfinderUtils.endNode.setParentNode(parent);
+
+ PathfinderUtils.drawPath();
map.isFinished = true;
ControlPanel.toggleRunBtn.setText("Clear");
map.repaint();
menu.renderMenu();
// Draws open nodes
- g.setColor(Color.white);
+ g.setColor(new Color(69, 133, 136));
for (Node node : PathfinderUtils.openNodes) {
g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
}
// Draws closed nodes
- g.setColor(new Color(132, 255, 138));
+ g.setColor(new Color(77, 77, 77));
for (Node node : PathfinderUtils.closedNodes) {
g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
}
+
+ // if path is found start drawing the shortest path
+ if (isFinished) {
+ g.setColor(new Color(250, 189, 47));
+ for (Node node : PathfinderUtils.path) {
+ g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
+ }
+ }
}
// Drawing on the grid
// Checks if start node and end node are the same
if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) {
- if (PathfinderUtils.checkDuplicateNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) {
+ 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);
return;
// Checks if end node and start node are the same
if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) {
- if (PathfinderUtils.checkDuplicateNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) {
+ 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);
return;
Node barrierNode = new Node(posX, posY);
if(PathfinderUtils.startNode != null)
- if(PathfinderUtils.checkDuplicateNode(barrierNode, PathfinderUtils.startNode))
+ if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.startNode))
return;
if(PathfinderUtils.endNode != null)
- if(PathfinderUtils.checkDuplicateNode(barrierNode, PathfinderUtils.endNode))
+ if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.endNode))
return;
PathfinderUtils.barriers.add(barrierNode);
static List<Node> barriers = new ArrayList<>();
static List<Node> openNodes = new ArrayList<>();
static List<Node> closedNodes = new ArrayList<>();
+ static List<Node> path = new ArrayList<>();
static Node startNode, endNode;
// Returns index of barrier if the coordinates are of a barrier node
barriers.remove(id);
}
- static boolean checkDuplicateNode(Node a, Node b) {
- if(a.getX() == b.getX() && a.getY() == b.getY())
+ static boolean isSameNode(Node a, Node b) {
+ if (a.getX() == b.getX() && a.getY() == b.getY())
return true;
return false;
}
}
}
}
+
+ static void drawPath() {
+ if (path.size() == 0) {
+ Node node = endNode.getParentNode();
+
+ while (!isSameNode(node, startNode)) {
+ path.add(node);
+
+ for (int i = 0; i < closedNodes.size(); i++) {
+ Node current = closedNodes.get(i);
+
+ if (isSameNode(current, node)) {
+ node = current.getParentNode();
+ break;
+ }
+ }
+ }
+ }
+ }
}
-------------------------------------------------------------------------------
Test set: Main.AppTest
-------------------------------------------------------------------------------
-Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 s - in Main.AppTest
+Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in Main.AppTest
<?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.029" 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.019" 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"/>
<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/surefirebooter8510599142121748610.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-27T09-11-09_330-jvmRun1 surefire6120348368433176072tmp surefire_01333341478733871355tmp"/>
+ <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="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"/>
<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/surefirebooter8510599142121748610.jar"/>
+ <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter3942171315997095630.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"/>