diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index 1ed1786..5dc2371 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -22,7 +22,7 @@ public class Dijkstra { int n = g.getVertexNumber(); for (int j = 0; j < n; j++) { - for (VertexInterface vertex: g.generateNeighbours(pivot)) { + for (VertexInterface vertex: pivot.generateNeighbours()) { if (!a.contains(vertex)) { if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) { pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index 51d147d..32e1e44 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -1,5 +1,7 @@ package dijkstra; +import java.util.ArrayList; + /** * This describes a vertex as seen in graphs (see INF101 course). * @@ -10,5 +12,10 @@ public interface VertexInterface { * @return The label of the element. */ public String getLabel() ; - + + /** + * Updates the attribute of the element. + * @return The neighbourList of the element. + */ + public ArrayList generateNeighbours(); } diff --git a/src/maze/Box.java b/src/maze/Box.java index 3a665e7..0c17d17 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -22,7 +22,7 @@ public abstract class Box private int y ; private String label = null ; private Maze maze ; - private ArrayList neighbourList = new ArrayList() ; + private ArrayList neighbourList; public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation @@ -33,9 +33,9 @@ public abstract class Box } - public ArrayList generateNeighbors() { + public ArrayList generateNeighbours() { + this.neighbourList = maze.generateNeighbours(this); return maze.generateNeighbours(this) ; - } public boolean isWalkable() { @@ -50,7 +50,7 @@ public abstract class Box return false ; } - public ArrayList getNeighbourList() { + public ArrayList getNeighbourList() { return neighbourList; } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index f626bef..9f85bad 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -209,7 +209,7 @@ public class Maze return neighbourList ; } - + /**Constructs the maze from a text file, according to each single character on each line. * @param filename * The path to the file containing the representation of the maze. diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 4214be0..c987abe 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -21,7 +21,8 @@ public class MazeAppModel extends Observable { private Previous previous; private ArrayList shortest; private ArrayList boxList; - private boolean saved = true; + private boolean saved = true; + private boolean pathDrawn = false; public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); @@ -62,6 +63,18 @@ public class MazeAppModel extends Observable { return saved; } + public ArrayList getShortest() { + return shortest; + } + + public boolean isPathDrawn() { + return pathDrawn; + } + + public void setPathDrawn(boolean pathDrawn) { + this.pathDrawn = pathDrawn; + } + public void saveToFile() { maze.saveToTextFile(filename); saved = true; diff --git a/src/ui/ClearShortestPathMenuItem.java b/src/ui/ClearShortestPathMenuItem.java new file mode 100644 index 0000000..e5bff7b --- /dev/null +++ b/src/ui/ClearShortestPathMenuItem.java @@ -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(); + } +} diff --git a/src/ui/DrawShortestPathMenuItem.java b/src/ui/DrawShortestPathMenuItem.java new file mode 100644 index 0000000..4338172 --- /dev/null +++ b/src/ui/DrawShortestPathMenuItem.java @@ -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(); + } +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index de0f36a..c5a9dd7 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -3,6 +3,7 @@ package ui; import javax.swing.*; import maze.Maze; import model.MazeAppModel; +import maze.EBox; import java.util.Observable; import java.util.Observer; @@ -76,4 +77,14 @@ public class MazeApp extends JFrame public void windowOpened(WindowEvent e) { return; } + + public void drawShortestPath() { + mazeAppModel.setPathDrawn(true); + windowPanel.paintShortestPath(); + } + + public void clearShortestPath() { + mazeAppModel.setPathDrawn(false); + this.repaint(); + } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 3f33f4c..0a5bd55 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -8,6 +8,8 @@ import java.awt.GridLayout; import javax.swing.JPanel; import maze.Maze; import model.MazeAppModelMessage; +import maze.EBox; +import dijkstra.VertexInterface; public class MazeDrawing extends JPanel { @@ -30,6 +32,9 @@ public class MazeDrawing extends JPanel { int height = getHeight(); paintMaze(g2d, width, height); + if (mazeApp.getMazeAppModel().isPathDrawn()) { + paintShortestPath(g2d, width, height); + } } private void paintMaze(Graphics2D g, int width, int height) { @@ -70,4 +75,21 @@ public class MazeDrawing extends JPanel { this.repaint(); } } + + private void paintShortestPath(Graphics2D g, int width, int height) { + + g.setColor(Color.ORANGE); + int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); + int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); + int boxWidth = width / dimensionX; + int boxHeight = height / dimensionY; + for (VertexInterface vertex : mazeApp.getMazeAppModel().getShortest()) { + EBox box = (EBox) vertex; + g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight); + } + } + + public void paintShortestPath() { + paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); + } } diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java new file mode 100644 index 0000000..e257339 --- /dev/null +++ b/src/ui/MazeMenu.java @@ -0,0 +1,19 @@ +package ui; + +import javax.swing.JMenu; + +public class MazeMenu extends JMenu { + + private final MazeApp mazeApp; + private final DrawShortestPathMenuItem drawMenuItem; + private final ClearShortestPathMenuItem clearMenuItem; + + public MazeMenu(MazeApp mazeApp) { + + super("Maze"); + this.mazeApp = mazeApp; + + add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp)); + add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp)); + } +} diff --git a/src/ui/MazeMenuBar.java b/src/ui/MazeMenuBar.java index 60fba7a..afe76da 100644 --- a/src/ui/MazeMenuBar.java +++ b/src/ui/MazeMenuBar.java @@ -6,6 +6,7 @@ public class MazeMenuBar extends JMenuBar { private final MazeApp mazeApp; private final FileMenu fileMenu; + private final MazeMenu mazeMenu; public MazeMenuBar(MazeApp mazeApp) { @@ -13,5 +14,6 @@ public class MazeMenuBar extends JMenuBar { this.mazeApp = mazeApp; add(fileMenu = new FileMenu(mazeApp)); + add(mazeMenu = new MazeMenu(mazeApp)); } } diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 0599a33..8ff60ca 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -19,4 +19,7 @@ public class WindowPanel extends JPanel drawingPanelGrid.notifyForUpdates(param); } + public void paintShortestPath() { + drawingPanelGrid.paintShortestPath(); + } }