public class AStar{
private Map map;
+ private int x, y;
public AStar(Map map) {
this.map = map;
- PathfinderUtils.path.removeAll(PathfinderUtils.path);
+ PathfinderUtils.path.clear();
}
public void start() {
public void searchPath(Node parent) {
for (int i = 0; i < 4; i++) {
- int x = (int) Math.round(parent.getX() + (-map.size* Math.cos((Math.PI / 2) * i)));
- int y = (int) Math.round(parent.getY() + (-map.size* Math.sin((Math.PI / 2) * i)));
+ x = (int) Math.round(parent.getX() + (-map.size* Math.cos((Math.PI / 2) * i)));
+ y = (int) Math.round(parent.getY() + (-map.size* Math.sin((Math.PI / 2) * i)));
calculateOpenNode(x, y, parent);
}
- parent = getNextBestNode();
+ if((parent = getNextBestNode()) == null)
+ return;
PathfinderUtils.closedNodes.add(parent);
PathfinderUtils.openNodes.remove(parent);
- if (!map.isFinished)
+ if (!map.isFinished)
searchPath(parent);
}
}
public Node getNextBestNode() {
- PathfinderUtils.sort();
- return PathfinderUtils.openNodes.get(0);
+ if (PathfinderUtils.openNodes.size() > 0) {
+ PathfinderUtils.sort();
+ return PathfinderUtils.openNodes.get(0);
+ }
+
+ map.isFinished = true;
+ ControlPanel.toggleRunBtn.setText("Clear");
+ map.repaint();
+ return null;
}
}
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.MouseEvent;
+import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
static int height;
boolean isFinished = false;
- int size = 30;
+ int size = 20;
public Map() {
this.setBackground(new Color(40, 40, 40));
}
// Draws barrier nodes
- g.setColor(new Color(235, 219, 178));
+ g.setColor(Color.white);
for (Node node : PathfinderUtils.barriers) {
g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
}
int posY = e.getY() - (e.getY() % size);
Node barrierNode = new Node(posX, posY);
- if(PathfinderUtils.startNode != null)
- if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.startNode))
+ if (PathfinderUtils.startNode != null)
+ if (PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.startNode))
return;
- if(PathfinderUtils.endNode != null)
- if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.endNode))
+ if (PathfinderUtils.endNode != null)
+ if (PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.endNode))
return;
PathfinderUtils.barriers.add(barrierNode);
@Override
public void actionPerformed(ActionEvent e) {
if (ControlPanel.toggleRunBtn.getText().equals("Run")) {
- switch (ControlPanel.algo.getItemAt(ControlPanel.algo.getSelectedIndex())) {
+ if (PathfinderUtils.startNode == null || PathfinderUtils.endNode == null)
+ return;
+
+ switch (ControlPanel.algo.getItemAt(ControlPanel.algo.getSelectedIndex())) {
case ("A*"):
new AStar(this).start();
break;
}
}
- else if(ControlPanel.toggleRunBtn.getText().equals("Clear")){
- PathfinderUtils.barriers.removeAll(PathfinderUtils.barriers);
- PathfinderUtils.openNodes.removeAll(PathfinderUtils.openNodes);
- PathfinderUtils.closedNodes.removeAll(PathfinderUtils.closedNodes);
+ else if (ControlPanel.toggleRunBtn.getText().equals("Clear")) {
+ PathfinderUtils.barriers.clear();
+ PathfinderUtils.openNodes.clear();
+ PathfinderUtils.closedNodes.clear();
PathfinderUtils.startNode = null;
PathfinderUtils.endNode = null;
-------------------------------------------------------------------------------
Test set: Main.AppTest
-------------------------------------------------------------------------------
-Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in Main.AppTest
+Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 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.019" 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.028" 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/surefirebooter3942171315997095630.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-30T11-56-47_958-jvmRun1 surefire2991361844254018063tmp surefire_012996660621204474861tmp"/>
+ <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="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/surefirebooter3942171315997095630.jar"/>
+ <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter7869637901010268867.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"/>
<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