-/* TODO
- * aggiungere una sorta di menu dove scegliere:
- * - velocità di riproduzione
- * - tipo di algoritmo:
- * + a*
- * + dijkstra
- * + breadth-first search
- * zoommare sulla griglia
- */
package Main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
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.MouseMotionListener;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
-class Map extends JPanel implements ActionListener, MouseListener, MouseMotionListener, KeyListener {
+class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListener {
private JFrame window;
private char key = (char) 0;
- private ControlPanel menu = new ControlPanel(this);
static int width;
static int height;
boolean isFinished = false;
+ boolean running = false;
int size = 20;
g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
}
- menu.renderMenu();
-
// Draws open nodes
g.setColor(new Color(69, 133, 136));
for (Node node : PathfinderUtils.openNodes) {
int posX = e.getX() - (e.getX() % size);
int posY = e.getY() - (e.getY() % size);
- // Checks if start node and end node are the same
- if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) {
- 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);
+ if (PathfinderUtils.startNode == null && PathfinderUtils.endNode == null) {
+ PathfinderUtils.startNode = new Node(posX, posY);
+ } else if (PathfinderUtils.endNode != null && PathfinderUtils.startNode == null) {
+ if (posX == PathfinderUtils.endNode.getX() && posY == PathfinderUtils.endNode.getY()) {
+ JOptionPane.showMessageDialog(null, "Same node error",
+ "End node and start node can't be the same node", JOptionPane.ERROR_MESSAGE);
return;
}
- }
-
- if (PathfinderUtils.startNode == null) {
PathfinderUtils.startNode = new Node(posX, posY);
} else {
+ if (posX == PathfinderUtils.endNode.getX() && posY == PathfinderUtils.endNode.getY()) {
+ JOptionPane.showMessageDialog(null, "Same node error",
+ "End node and start node can't be the same node", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+
PathfinderUtils.startNode.setX(posX);
PathfinderUtils.startNode.setY(posY);
}
int posX = e.getX() - (e.getX() % size);
int posY = e.getY() - (e.getY() % size);
- // Checks if end node and start node are the same
- if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) {
- 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);
+ if (PathfinderUtils.startNode == null && PathfinderUtils.endNode == null) {
+ PathfinderUtils.endNode = new Node(posX, posY);
+ } else if (PathfinderUtils.startNode != null && PathfinderUtils.endNode == null) {
+ if (posX == PathfinderUtils.startNode.getX() && posY == PathfinderUtils.startNode.getY()) {
+ JOptionPane.showMessageDialog(null, "Same node error",
+ "End node and start node can't be the same node", JOptionPane.ERROR_MESSAGE);
return;
}
- }
-
- if (PathfinderUtils.endNode == null) {
PathfinderUtils.endNode = new Node(posX, posY);
} else {
+ if (posX == PathfinderUtils.startNode.getX() && posY == PathfinderUtils.startNode.getY()) {
+ JOptionPane.showMessageDialog(null, "Same node error",
+ "End node and start node can't be the same node", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+
PathfinderUtils.endNode.setX(posX);
PathfinderUtils.endNode.setY(posY);
}
@Override
public void keyPressed(KeyEvent e) {
key = e.getKeyChar();
+
+ if (key == KeyEvent.VK_SPACE) {
+ if (running == false && isFinished == false) {
+ if (PathfinderUtils.startNode == null || PathfinderUtils.endNode == null) {
+ JOptionPane.showMessageDialog(null, "Missing node", "Missing start or end node",
+ JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+
+ running = true;
+ new AStar(this).start();
+ } else if (running == false && isFinished == true) {
+ PathfinderUtils.barriers.clear();
+ PathfinderUtils.openNodes.clear();
+ PathfinderUtils.closedNodes.clear();
+ PathfinderUtils.startNode = null;
+ PathfinderUtils.endNode = null;
+
+ isFinished = false;
+ running = false;
+
+ this.repaint();
+ }
+ }
}
@Override
mapDrawing(e);
}
- @Override
- public void actionPerformed(ActionEvent e) {
- if (ControlPanel.toggleRunBtn.getText().equals("Run")) {
-
- if (PathfinderUtils.startNode == null || PathfinderUtils.endNode == null)
- return;
-
- switch (ControlPanel.algo.getItemAt(ControlPanel.algo.getSelectedIndex())) {
- case ("A*"):
- new AStar(this).start();
- break;
-
- case ("Dijkstra"):
- System.out.println("Dijkstra selected");
- // AStar.start();
- break;
-
- case ("Breadth-first search"):
- System.out.println("Breadth-first search selected");
- // AStar.start();
- break;
-
- default:
- JOptionPane.showMessageDialog(null, "You must select an algorithm before starting the pathfinder",
- "Algorithm not selected", JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
-
- else if (ControlPanel.toggleRunBtn.getText().equals("Clear")) {
- PathfinderUtils.barriers.clear();
- PathfinderUtils.openNodes.clear();
- PathfinderUtils.closedNodes.clear();
- PathfinderUtils.startNode = null;
- PathfinderUtils.endNode = null;
-
- isFinished = false;
-
- this.repaint();
- ControlPanel.toggleRunBtn.setText("Run");
- }
- }
-
@Override
public void keyTyped(KeyEvent e) {
}
<?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.028" 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.024" 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/surefirebooter7869637901010268867.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-07-01T10-30-57_583-jvmRun1 surefire18123134203155873160tmp surefire_012477696981176381238tmp"/>
+ <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter8825560741906188442.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-07-02T09-18-56_069-jvmRun1 surefire1726243427666730328tmp surefire_02585865431578143327tmp"/>
<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/surefirebooter7869637901010268867.jar"/>
+ <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter8825560741906188442.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.002"/>
+ <testcase name="shouldAnswerWithTrue" classname="Main.AppTest" time="0.001"/>
</testsuite>
\ No newline at end of file