Add menu button to draw the shortest path
ui: * Add methods to draw the shortest path from MazeApp, WindowPanel and MazeDrawing * Add a Maze menu with a button to draw the shortest path from departure to arrival
This commit is contained in:
1 parent
1e934b05e6
commit
c0afbe104c
6 files changed
+57
-2
No files matched your search
@@ -0,0 +1,25 @@
|
|||||||
|
package ui;
|
||||||
|
|
||||||
|
import javax.swing.JMenuItem;
|
||||||
|
|
||||||
|
import model.MazeAppModel;
|
||||||
|
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
public class DrawShortestPathMenuItem extends JMenuItem
|
||||||
|
implements ActionListener {
|
||||||
|
|
||||||
|
private final MazeApp mazeApp;
|
||||||
|
|
||||||
|
public DrawShortestPathMenuItem(MazeApp mazeApp) {
|
||||||
|
|
||||||
|
super("Draw shortest path");
|
||||||
|
this.mazeApp = mazeApp;
|
||||||
|
addActionListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
mazeApp.drawShortestPath();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,4 +41,8 @@ public class MazeApp extends JFrame
|
|||||||
System.out.println("New maze detected");
|
System.out.println("New maze detected");
|
||||||
windowPanel.notifyForUpdates(param);
|
windowPanel.notifyForUpdates(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void drawShortestPath() {
|
||||||
|
windowPanel.paintShortestPath();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ public class MazeDrawing extends JPanel {
|
|||||||
this.mazeApp = mazeApp;
|
this.mazeApp = mazeApp;
|
||||||
setBackground(Color.BLACK);
|
setBackground(Color.BLACK);
|
||||||
setPreferredSize(new Dimension(500,500)) ;
|
setPreferredSize(new Dimension(500,500)) ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -41,7 +41,7 @@ public class MazeDrawing extends JPanel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paintShortestPath(Graphics2D g, int width, int height) {
|
private void paintShortestPath(Graphics2D g, int width, int height) {
|
||||||
|
|
||||||
g.setColor(Color.ORANGE);
|
g.setColor(Color.ORANGE);
|
||||||
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
|
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
|
||||||
@@ -53,4 +53,8 @@ public class MazeDrawing extends JPanel {
|
|||||||
g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight);
|
g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void paintShortestPath() {
|
||||||
|
paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package ui;
|
||||||
|
|
||||||
|
import javax.swing.JMenu;
|
||||||
|
|
||||||
|
public class MazeMenu extends JMenu {
|
||||||
|
|
||||||
|
private final MazeApp mazeApp;
|
||||||
|
private final DrawShortestPathMenuItem drawMenuItem;
|
||||||
|
|
||||||
|
public MazeMenu(MazeApp mazeApp) {
|
||||||
|
|
||||||
|
super("Maze");
|
||||||
|
this.mazeApp = mazeApp;
|
||||||
|
|
||||||
|
add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ public class MazeMenuBar extends JMenuBar {
|
|||||||
|
|
||||||
private final MazeApp mazeApp;
|
private final MazeApp mazeApp;
|
||||||
private final FileMenu fileMenu;
|
private final FileMenu fileMenu;
|
||||||
|
private final MazeMenu mazeMenu;
|
||||||
|
|
||||||
public MazeMenuBar(MazeApp mazeApp) {
|
public MazeMenuBar(MazeApp mazeApp) {
|
||||||
|
|
||||||
@@ -13,5 +14,6 @@ public class MazeMenuBar extends JMenuBar {
|
|||||||
this.mazeApp = mazeApp;
|
this.mazeApp = mazeApp;
|
||||||
|
|
||||||
add(fileMenu = new FileMenu(mazeApp));
|
add(fileMenu = new FileMenu(mazeApp));
|
||||||
|
add(mazeMenu = new MazeMenu(mazeApp));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,4 +19,7 @@ public class WindowPanel extends JPanel
|
|||||||
drawingPanelGrid.notifyForUpdates(param);
|
drawingPanelGrid.notifyForUpdates(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void paintShortestPath() {
|
||||||
|
drawingPanelGrid.paintShortestPath();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user