From 9f56d3082a23438b424f01f383031d7abb52fcf5 Mon Sep 17 00:00:00 2001 From: Antonin GODARD Date: Thu, 21 Dec 2017 09:38:31 +0100 Subject: [PATCH 1/7] Add paintShortestPath to MazeAppModel --- src/model/MazeAppModel.java | 20 +++++++++++++++++++- src/ui/MazeDrawing.java | 1 - 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index f46b461..815f3f6 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -8,6 +8,7 @@ import java.util.Observable; import java.util.Observer; import dijkstra.VertexInterface; +import maze.EBox; import dijkstra.Dijkstra; import dijkstra.Previous; @@ -18,11 +19,15 @@ public class MazeAppModel extends Observable { private VertexInterface arrival = maze.getArrival(); private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure); private ArrayList shortest = previous.getShortestPathTo(arrival); - private ArrayList boxList = maze.getVertexes(); + //private ArrayList boxList = maze.getVertexes(); private Boolean modified = false; public void setMaze(Maze maze) { this.maze = new Maze(maze); + this.departure = maze.getDeparture(); + this.arrival = maze.getArrival(); + this.previous = (Previous) Dijkstra.dijkstra(maze, departure); + this.shortest = previous.getShortestPathTo(arrival); modified = true; setChanged(); notifyObservers(maze); @@ -73,4 +78,17 @@ public class MazeAppModel extends Observable { } } } + + public void paintShortestPath(Graphics2D g, int width, int height) { + + g.setColor(Color.ORANGE); + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + int boxWidth = width/dimensionX; + int boxHeight = height/dimensionY; + for (VertexInterface vertex : shortest) { + EBox box = (EBox) vertex; + g.fillOval(box.getX()*boxHeight, box.getY()*boxWidth, boxWidth, boxHeight); + } + } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index f43e4ea..2a83fe1 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -30,7 +30,6 @@ public class MazeDrawing extends JPanel { int height = getHeight(); mazeApp.getMazeAppModel().paintMaze(g2d, width, height); - } public void notifyForUpdates(Object param) { From b5eb6b0b8cb8765b422d7e150b19bb2e41b9ba46 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 23 Dec 2017 22:31:56 +0100 Subject: [PATCH 2/7] Move paintShortestPath from model to ui --- src/model/MazeAppModel.java | 14 -------------- src/ui/MazeApp.java | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 815f3f6..e0be3d1 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -8,7 +8,6 @@ import java.util.Observable; import java.util.Observer; import dijkstra.VertexInterface; -import maze.EBox; import dijkstra.Dijkstra; import dijkstra.Previous; @@ -78,17 +77,4 @@ public class MazeAppModel extends Observable { } } } - - public void paintShortestPath(Graphics2D g, int width, int height) { - - g.setColor(Color.ORANGE); - int dimensionX = maze.getDimensionX(); - int dimensionY = maze.getDimensionY(); - int boxWidth = width/dimensionX; - int boxHeight = height/dimensionY; - for (VertexInterface vertex : shortest) { - EBox box = (EBox) vertex; - g.fillOval(box.getX()*boxHeight, box.getY()*boxWidth, boxWidth, boxHeight); - } - } } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 2e7b37d..03f00ef 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; @@ -40,4 +41,17 @@ public class MazeApp extends JFrame System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } + + public void paintShortestPath(Graphics2D g, int width, int height) { + + g.setColor(Color.ORANGE); + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + int boxWidth = width/dimensionX; + int boxHeight = height/dimensionY; + for (VertexInterface vertex : shortest) { + EBox box = (EBox) vertex; + g.fillOval(box.getX()*boxHeight, box.getY()*boxWidth, boxWidth, boxHeight); + } + } } From 1e934b05e64d477bf8adc4f2df188a0eff9b6d16 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 24 Dec 2017 19:09:14 +0100 Subject: [PATCH 3/7] Fix paintShortestPath and move it into MazeDrawing model: * Add getShortest() to MazeAppModel, which returns the list of vertexes on the shortest path from departure to arrival ui: * Move paintShortestPath from MazeApp to MazeDrawing * Fix a bug in paintShortestPath where a switch between width and height caused ellipses to be mislocated (probably still the same confusion between the x and y axes) --- src/model/MazeAppModel.java | 4 ++++ src/ui/MazeApp.java | 13 ------------- src/ui/MazeDrawing.java | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index e0be3d1..b782287 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -36,6 +36,10 @@ public class MazeAppModel extends Observable { return maze; } + public ArrayList getShortest() { + return shortest; + } + public boolean isModified() { return modified; } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 03f00ef..48c6256 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -41,17 +41,4 @@ public class MazeApp extends JFrame System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } - - public void paintShortestPath(Graphics2D g, int width, int height) { - - g.setColor(Color.ORANGE); - int dimensionX = maze.getDimensionX(); - int dimensionY = maze.getDimensionY(); - int boxWidth = width/dimensionX; - int boxHeight = height/dimensionY; - for (VertexInterface vertex : shortest) { - EBox box = (EBox) vertex; - g.fillOval(box.getX()*boxHeight, box.getY()*boxWidth, boxWidth, boxHeight); - } - } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 2a83fe1..ce96b79 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -7,6 +7,8 @@ import java.awt.Graphics2D; import java.awt.GridLayout; import javax.swing.JPanel; import maze.Maze; +import maze.EBox; +import dijkstra.VertexInterface; public class MazeDrawing extends JPanel { @@ -38,4 +40,17 @@ public class MazeDrawing extends JPanel { this.repaint(); } } + + public 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); + } + } } From c0afbe104c3523451dd431e6a8355d783165c5f0 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 24 Dec 2017 20:20:40 +0100 Subject: [PATCH 4/7] 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 --- src/ui/DrawShortestPathMenuItem.java | 25 +++++++++++++++++++++++++ src/ui/MazeApp.java | 4 ++++ src/ui/MazeDrawing.java | 8 ++++++-- src/ui/MazeMenu.java | 17 +++++++++++++++++ src/ui/MazeMenuBar.java | 2 ++ src/ui/WindowPanel.java | 3 +++ 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/ui/DrawShortestPathMenuItem.java create mode 100644 src/ui/MazeMenu.java 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 48c6256..13e350d 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -41,4 +41,8 @@ public class MazeApp extends JFrame System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } + + public void drawShortestPath() { + windowPanel.paintShortestPath(); + } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index ce96b79..b5ca367 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -19,7 +19,7 @@ public class MazeDrawing extends JPanel { this.mazeApp = mazeApp; setBackground(Color.BLACK); setPreferredSize(new Dimension(500,500)) ; - + } @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); 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); } } + + 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..d8348a6 --- /dev/null +++ b/src/ui/MazeMenu.java @@ -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)); + } +} 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(); + } } From 7242872b834a9c04037d353e13a51fd54dd06164 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 24 Dec 2017 21:27:20 +0100 Subject: [PATCH 5/7] 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 --- src/model/MazeAppModel.java | 9 +++++++++ src/ui/ClearShortestPathMenuItem.java | 25 +++++++++++++++++++++++++ src/ui/MazeApp.java | 6 ++++++ src/ui/MazeMenu.java | 2 ++ 4 files changed, 42 insertions(+) create mode 100644 src/ui/ClearShortestPathMenuItem.java diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index b782287..ddbd438 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -20,6 +20,7 @@ public class MazeAppModel extends Observable { private ArrayList shortest = previous.getShortestPathTo(arrival); //private ArrayList boxList = maze.getVertexes(); private Boolean modified = false; + private boolean pathDrawn = false; public void setMaze(Maze maze) { this.maze = new Maze(maze); @@ -44,6 +45,14 @@ public class MazeAppModel extends Observable { return modified; } + public boolean isPathDrawn() { + return pathDrawn; + } + + public void setPathDrawn(boolean pathDrawn) { + this.pathDrawn = pathDrawn; + } + public void saveToFile() { maze.saveToTextFile("data/labyOutput.txt"); } 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/MazeApp.java b/src/ui/MazeApp.java index 13e350d..9a2ce6a 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -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(); + } } diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index d8348a6..e257339 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -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)); } } From 11200ebed29760b5601cb35a4384c5920d808b79 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Tue, 26 Dec 2017 22:16:40 +0100 Subject: [PATCH 6/7] Fix repainting when the path is drawn ui: * Fix repainting when the path is supposed to be drawn. At any repainting (window resizing, menu unfolding), the maze was repainted without the shortest path. We now use the attribute stored in the model to know whether the path should be drawn or not when we repaint the panel (see #24) --- src/ui/MazeDrawing.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index b5ca367..32874f0 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -32,6 +32,9 @@ public class MazeDrawing extends JPanel { int height = getHeight(); mazeApp.getMazeAppModel().paintMaze(g2d, width, height); + if (mazeApp.getMazeAppModel().isPathDrawn()) { + paintShortestPath(g2d, width, height); + } } public void notifyForUpdates(Object param) { From c7134e9e913d3c1206b83de0e1d6a1631f0e41bd Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Wed, 27 Dec 2017 15:13:40 +0100 Subject: [PATCH 7/7] Fix infinite path in Dijkstra algorithm --- src/dijkstra/Dijkstra.java | 2 +- src/dijkstra/VertexInterface.java | 9 ++++++++- src/maze/Box.java | 8 ++++---- src/maze/Maze.java | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) 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 8e6edad..2f22ccb 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -141,7 +141,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.