From: LeonardoBizzoni Date: Fri, 18 Jun 2021 10:45:13 +0000 (+0200) Subject: Added basic control menu X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=34ceb52ac73ab743bd97358c0902d010689b120d;p=astar-visualizer Added basic control menu --- diff --git a/src/main/java/Main/ControlPanel.java b/src/main/java/Main/ControlPanel.java new file mode 100644 index 0000000..6c57821 --- /dev/null +++ b/src/main/java/Main/ControlPanel.java @@ -0,0 +1,79 @@ +package Main; + +import java.awt.Color; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JOptionPane; + +public class ControlPanel { + private Map map; + + private JButton toggleRunBtn; + private JComboBox algo; + + public ControlPanel(Map map) { + this.map = map; + + algo = new JComboBox<>(new String[] { "Select an algorithm", "A*", "Dijkstra", "Breadth-first search"}); + algo.setVisible(true); + + toggleRunBtn = new JButton("Run"); + toggleRunBtn.setVisible(true); + toggleRunBtn.setMargin(new Insets(0, 0, 0, 0)); + toggleRunBtn.setBackground(Color.white); + toggleRunBtn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + if (toggleRunBtn.getText().equals("Run")) { + switch (algo.getItemAt(algo.getSelectedIndex())) { + + case ("A*"): + System.out.println("A* selected"); + // AStar.start(); + break; + + case ("Dijkstra"): + System.out.println("Dijkstra selected"); + // AStar.start(); + break; + + case ("Greedy best-first search"): + System.out.println("Greedy best-first search 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; + } + + toggleRunBtn.setText("Stop"); + } + + else { + toggleRunBtn.setText("Run"); + } + } + }); + + map.add(algo); + map.add(toggleRunBtn); + } + + public void renderMenu() { + algo.setBounds(10,10, algo.getWidth(), algo.getHeight()); + toggleRunBtn.setBounds(220, 10, 48, 24); + } +} diff --git a/src/main/java/Main/Map.java b/src/main/java/Main/Map.java index 912cf19..36f8ff2 100644 --- a/src/main/java/Main/Map.java +++ b/src/main/java/Main/Map.java @@ -4,12 +4,7 @@ * - tipo di algoritmo: * + a* * + dijkstra - * + greedy best-first search - * + swarm - * + convergent swarm - * + bidirectional swarm * + breadth-first search - * + depth-first search * zoommare sulla griglia */ package Main; @@ -31,9 +26,11 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe private JFrame window; private int size = 30; private char key = (char) 0; + private ControlPanel menu = new ControlPanel(this); public Map() { this.setBackground(new Color(40, 40, 40)); + addMouseListener(this); addKeyListener(this); setFocusable(true); @@ -79,6 +76,8 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe for (Node node : PathfinderUtils.barriers) { g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); } + + menu.renderMenu(); } // Drawing on the grid @@ -178,7 +177,6 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe @Override public void keyPressed(KeyEvent e) { key = e.getKeyChar(); - // TODO far partire/fermare l'algoritmo con il tasto invio } @Override @@ -213,4 +211,5 @@ class Map extends JPanel implements MouseListener, MouseMotionListener, KeyListe @Override public void mouseMoved(MouseEvent e) {} + } diff --git a/target/PathVisualizer-1.0-SNAPSHOT.jar b/target/PathVisualizer-1.0-SNAPSHOT.jar index bf40587..f674501 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/ControlPanel$1.class b/target/classes/Main/ControlPanel$1.class new file mode 100644 index 0000000..fa3a702 Binary files /dev/null and b/target/classes/Main/ControlPanel$1.class differ diff --git a/target/classes/Main/ControlPanel.class b/target/classes/Main/ControlPanel.class new file mode 100644 index 0000000..340d99e Binary files /dev/null and b/target/classes/Main/ControlPanel.class differ diff --git a/target/classes/Main/Map.class b/target/classes/Main/Map.class index d1fe5a2..f084550 100644 Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.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 0fc7d35..6b22cb6 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,4 +1,6 @@ +Main/ControlPanel$1.class Main/Node.class +Main/ControlPanel.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 c09b5f3..b97cf8a 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/ControlPanel.java /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 diff --git a/target/surefire-reports/Main.AppTest.txt b/target/surefire-reports/Main.AppTest.txt index 815c74a..b957bda 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.027 s - in Main.AppTest +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.026 s - in Main.AppTest diff --git a/target/surefire-reports/TEST-Main.AppTest.xml b/target/surefire-reports/TEST-Main.AppTest.xml index 50af648..9f3f13d 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