Add a button to clear path
model: * Add an attribute to MazeAppModel to keep track of if the path is drawn or not. For future use to enable/disable the Clear button depending on if the path is drawn or not ui: * Add a "Clear shortest path" button to the Maze menu * Add a method to clear the path in MazeApp (just trigger a repainting of the MazeApp, which erase the path, since its not drawn by default) and update MazeAppModel accordingly
This commit is contained in:
1 parent
c0afbe104c
commit
7242872b83
4 files changed
+42
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 ClearShortestPathMenuItem extends JMenuItem
|
||||
implements ActionListener {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
|
||||
public ClearShortestPathMenuItem(MazeApp mazeApp) {
|
||||
|
||||
super("Clear shortest path");
|
||||
this.mazeApp = mazeApp;
|
||||
addActionListener(this);
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent evt) {
|
||||
mazeApp.clearShortestPath();
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,12 @@ public class MazeApp extends JFrame
|
||||
}
|
||||
|
||||
public void drawShortestPath() {
|
||||
mazeAppModel.setPathDrawn(true);
|
||||
windowPanel.paintShortestPath();
|
||||
}
|
||||
|
||||
public void clearShortestPath() {
|
||||
mazeAppModel.setPathDrawn(false);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ public class MazeMenu extends JMenu {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
private final DrawShortestPathMenuItem drawMenuItem;
|
||||
private final ClearShortestPathMenuItem clearMenuItem;
|
||||
|
||||
public MazeMenu(MazeApp mazeApp) {
|
||||
|
||||
@@ -13,5 +14,6 @@ public class MazeMenu extends JMenu {
|
||||
this.mazeApp = mazeApp;
|
||||
|
||||
add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp));
|
||||
add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user