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) {
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;
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);
window.pack();
window.setVisible(true);
- this.revalidate();
- this.repaint();
+ revalidate();
+ repaint();
}
public void paintComponent(Graphics g) {
// 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);
}
}
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;
}
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;
}
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;
}
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();
PathfinderUtils.endNode = null;
isFinished = false;
- running = false;
- this.repaint();
+ repaint();
}
}
}
-#Created by Apache Maven 3.6.3
+#Created by Apache Maven 3.3.9
groupId=Main
artifactId=PathVisualizer
version=1.0-SNAPSHOT
-/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
-/home/leo/Docs/Proj/Pathfinder/src/test/java/Main/AppTest.java
+/home/leo/Docs/Proj/pathfinding-visualizer/src/test/java/Main/AppTest.java
-------------------------------------------------------------------------------
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
<?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.025" 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.021" 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.cpu.isalist" value=""/>
+ <property name="java.specification.version" value="16"/>
<property name="sun.jnu.encoding" value="ANSI_X3.4-1968"/>
- <property name="java.class.path" value="/home/leo/Docs/Proj/Pathfinder/target/test-classes:/home/leo/Docs/Proj/Pathfinder/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="java.class.path" value="/home/leo/Docs/Proj/pathfinding-visualizer/target/test-classes:/home/leo/Docs/Proj/pathfinding-visualizer/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="java.vm.vendor" value="Oracle Corporation"/>
<property name="sun.arch.data.model" value="64"/>
- <property name="java.vendor.url" value="http://java.oracle.com/"/>
- <property name="user.timezone" value=""/>
- <property name="java.vm.specification.version" value="11"/>
+ <property name="java.vendor.url" value="https://java.oracle.com/"/>
<property name="os.name" value="Linux"/>
+ <property name="java.vm.specification.version" value="16"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="user.country" value="US"/>
- <property name="sun.boot.library.path" value="/usr/lib/jvm/openjdk11-bin/lib"/>
- <property name="sun.java.command" value="/home/leo/Docs/Proj/Pathfinder/target/surefire/surefirebooter7497617524640717844.jar /home/leo/Docs/Proj/Pathfinder/target/surefire 2021-11-19T21-17-25_132-jvmRun1 surefire5030477053776971331tmp surefire_02397316267537071803tmp"/>
+ <property name="sun.boot.library.path" value="/home/leo/.config/jdks/openjdk-16.0.2/lib"/>
+ <property name="sun.java.command" value="/home/leo/Docs/Proj/pathfinding-visualizer/target/surefire/surefirebooter10436255508326751885.jar /home/leo/Docs/Proj/pathfinding-visualizer/target/surefire 2021-11-20T09-06-55_141-jvmRun1 surefire17825492757088903556tmp surefire_08327865763710316556tmp"/>
<property name="jdk.debug" value="release"/>
- <property name="surefire.test.class.path" value="/home/leo/Docs/Proj/Pathfinder/target/test-classes:/home/leo/Docs/Proj/Pathfinder/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="surefire.test.class.path" value="/home/leo/Docs/Proj/pathfinding-visualizer/target/test-classes:/home/leo/Docs/Proj/pathfinding-visualizer/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="user.home" value="/home/leo"/>
<property name="user.language" value="en"/>
<property name="java.specification.vendor" value="Oracle Corporation"/>
- <property name="java.version.date" value="2018-09-25"/>
- <property name="java.home" value="/usr/lib/jvm/openjdk11-bin"/>
+ <property name="java.version.date" value="2021-07-20"/>
+ <property name="java.home" value="/home/leo/.config/jdks/openjdk-16.0.2"/>
<property name="file.separator" value="/"/>
- <property name="basedir" value="/home/leo/Docs/Proj/Pathfinder"/>
- <property name="java.vm.compressedOopsMode" value="Zero based"/>
+ <property name="basedir" value="/home/leo/Docs/Proj/pathfinding-visualizer"/>
+ <property name="java.vm.compressedOopsMode" value="32-bit"/>
<property name="line.separator" value=" "/>
- <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/Pathfinder/target/surefire/surefirebooter7497617524640717844.jar"/>
+ <property name="java.specification.name" value="Java Platform API Specification"/>
+ <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/pathfinding-visualizer/target/surefire/surefirebooter10436255508326751885.jar"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
- <property name="java.runtime.version" value="11+28"/>
+ <property name="java.runtime.version" value="16.0.2+7-67"/>
<property name="user.name" value="leo"/>
<property name="path.separator" value=":"/>
<property name="os.version" value="5.13.19_1"/>
<property name="java.runtime.name" value="OpenJDK Runtime Environment"/>
<property name="file.encoding" value="ANSI_X3.4-1968"/>
<property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/>
- <property name="java.vendor.version" value="18.9"/>
<property name="localRepository" value="/home/leo/.m2/repository"/>
- <property name="java.vendor.url.bug" value="http://bugreport.java.com/bugreport/"/>
+ <property name="java.vendor.url.bug" value="https://bugreport.java.com/bugreport/"/>
<property name="java.io.tmpdir" value="/tmp"/>
- <property name="java.version" value="11"/>
- <property name="user.dir" value="/home/leo/Docs/Proj/Pathfinder"/>
+ <property name="java.version" value="16.0.2"/>
+ <property name="user.dir" value="/home/leo/Docs/Proj/pathfinding-visualizer"/>
<property name="os.arch" value="amd64"/>
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
- <property name="java.awt.printerjob" value="sun.print.PSPrinterJob"/>
- <property name="sun.os.patch.level" value="unknown"/>
<property name="java.library.path" value="/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib"/>
+ <property name="java.vm.info" value="mixed mode, sharing"/>
<property name="java.vendor" value="Oracle Corporation"/>
- <property name="java.vm.info" value="mixed mode"/>
- <property name="java.vm.version" value="11+28"/>
+ <property name="java.vm.version" value="16.0.2+7-67"/>
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
- <property name="java.class.version" value="55.0"/>
+ <property name="java.class.version" value="60.0"/>
</properties>
<testcase name="shouldAnswerWithTrue" classname="Main.AppTest" time="0.001"/>
</testsuite>
\ No newline at end of file