From eaba5b610604f0ab0c301fca5868354f658589d3 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Thu, 4 Jan 2018 21:33:02 +0100 Subject: [PATCH 1/5] Refactorize code --- src/maze/Maze.java | 20 +++++++++ src/model/MazeAppModel.java | 30 +++++++++---- src/ui/BoxTypeEditionMenu.java | 4 +- src/ui/LoadMazeMenuItem.java | 5 +-- src/ui/MazeApp.java | 73 +++++++++++++++++++++++++++++--- src/ui/MazeDrawing.java | 39 ++++++++--------- src/ui/NewClearMazeMenuItem.java | 1 - src/ui/QuitMenuItem.java | 14 +++--- src/ui/SetABoxMenuItem.java | 2 +- src/ui/SetDBoxMenuItem.java | 2 +- src/ui/SetEBoxMenuItem.java | 2 +- src/ui/SetWBoxMenuItem.java | 2 +- 12 files changed, 144 insertions(+), 50 deletions(-) diff --git a/src/maze/Maze.java b/src/maze/Maze.java index a9b4246..a30a2b8 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -296,6 +296,26 @@ public class Maze dimensionY = dimensionY - 1; } + public Box getBox(int boxLine, int boxRow) { + return this.maze[boxLine][boxRow]; + } + + public void setABox(int boxLine, int boxRow) { + this.maze[boxLine][boxRow] = new ABox("A", boxLine, boxRow, this); + } + + public void setDBox(int boxLine, int boxRow) { + this.maze[boxLine][boxRow] = new DBox("D", boxLine, boxRow, this); + } + + public void setEBox(int boxLine, int boxRow) { + this.maze[boxLine][boxRow] = new EBox("E", boxLine, boxRow, this); + } + + public void setWBox(int boxLine, int boxRow) { + this.maze[boxLine][boxRow] = new WBox("W", boxLine, boxRow, this); + } + public int getCost(VertexInterface start, VertexInterface end) { Box startBox = (Box) start; diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index e4bf63d..4e0e939 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -67,6 +67,14 @@ public class MazeAppModel extends Observable { return maze; } + public Box[][] getBoxMaze() { + return this.maze.getMaze(); + } + + public Box getBox(int boxLine, int boxRow) { + return this.maze.getBox(boxLine, boxRow); + } + public String getFilename() { return filename; } @@ -118,14 +126,14 @@ public class MazeAppModel extends Observable { } public void setWBox(int boxLine, int boxRow) { - maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze); + maze.setWBox(boxLine, boxRow); maze.resetNeighbourLists(); initModelFromMaze(); saved = false; } public void setEBox(int boxLine, int boxRow) { - maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze); + maze.setEBox(boxLine, boxRow); maze.resetNeighbourLists(); initModelFromMaze(); saved = false; @@ -135,10 +143,9 @@ public class MazeAppModel extends Observable { if (departure != null) { int departureRow = ((EBox) departure).getX(); int departureLine = ((EBox) departure).getY(); - maze.getMaze()[departureLine][departureRow] = - new EBox("E", departureLine, departureRow, maze); + maze.setEBox(departureLine, departureRow); } - maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze); + maze.setDBox(boxLine, boxRow); maze.resetNeighbourLists(); initModelFromMaze(); saved = false; @@ -148,10 +155,9 @@ public class MazeAppModel extends Observable { if (arrival != null) { int arrivalRow = ((EBox) arrival).getX(); int arrivalLine = ((EBox) arrival).getY(); - maze.getMaze()[arrivalLine][arrivalRow] = - new EBox("E", arrivalLine, arrivalRow, maze); + maze.setEBox(arrivalLine, arrivalRow); } - maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze); + maze.setABox(boxLine, boxRow); maze.resetNeighbourLists(); initModelFromMaze(); saved = false; @@ -211,4 +217,12 @@ public class MazeAppModel extends Observable { public void clearMaze() { newClearMaze(maze.getDimensionY(), maze.getDimensionX()); } + + public int getDimensionX() { + return this.maze.getDimensionX(); + } + + public int getDimensionY() { + return this.maze.getDimensionX(); + } } diff --git a/src/ui/BoxTypeEditionMenu.java b/src/ui/BoxTypeEditionMenu.java index 3f07272..b49e166 100644 --- a/src/ui/BoxTypeEditionMenu.java +++ b/src/ui/BoxTypeEditionMenu.java @@ -5,6 +5,7 @@ import javax.swing.JMenuItem; import maze.Box; public class BoxTypeEditionMenu extends JPopupMenu { + JMenuItem setWBox; JMenuItem setEBox; JMenuItem setDBox; @@ -15,11 +16,12 @@ public class BoxTypeEditionMenu extends JPopupMenu { JMenuItem delLine; public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) { + add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow)); add(setWBox = new SetWBoxMenuItem(mazeApp, boxLine, boxRow)); add(setDBox = new SetDBoxMenuItem(mazeApp, boxLine, boxRow)); add(setABox = new SetABoxMenuItem(mazeApp, boxLine, boxRow)); - Box box = (Box) mazeApp.getMazeAppModel().getMaze().getMaze()[boxLine][boxRow]; + Box box = (Box) mazeApp.getModelBox(boxLine, boxRow); setEBox.setEnabled((box.isWalkable() && (box.isDeparture() || box.isArrival())) || ! box.isWalkable()); setWBox.setEnabled(box.isWalkable()); diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 05cf17e..65328e4 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -22,15 +22,14 @@ public class LoadMazeMenuItem extends JMenuItem public void actionPerformed(ActionEvent evt) { - MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - if (! mazeAppModel.isSaved()) { + if (mazeApp.isModelSaved()) { if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ return; } } String filename = FileBox.load(mazeApp); if (filename != null) { - mazeApp.getMazeAppModel().loadMaze(filename); + mazeApp.loadModelMaze(filename); } } } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index bbfaee6..1612085 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -1,10 +1,13 @@ package ui; import javax.swing.*; + +import dijkstra.VertexInterface; import maze.Maze; import model.MazeAppModel; import maze.EBox; +import java.util.ArrayList; import java.util.Observable; import java.util.Observer; import java.awt.event.WindowEvent; @@ -40,7 +43,6 @@ public class MazeApp extends JFrame @Override public void update(Observable obs, Object param) { - System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } @@ -83,6 +85,11 @@ public class MazeApp extends JFrame mazeAppModel.setPathDrawn(2); } + public void animateShortestPath() { + windowPanel.animateShortestPath(); + mazeAppModel.setPathDrawn(1); + } + public void clearShortestPath() { windowPanel.clearShortestPath(); mazeAppModel.setPathDrawn(0); @@ -126,9 +133,63 @@ public class MazeApp extends JFrame this.repaint(); } - public void animateShortestPath() { - - windowPanel.animateShortestPath(); - mazeAppModel.setPathDrawn(1); + public boolean isModelSaved( ) { + return this.mazeAppModel.isSaved(); } -} + + public void loadModelMaze(String filename) { + this.mazeAppModel.loadMaze(filename); + } + + public maze.Box getModelBox(int boxLine, int boxRow) { + return this.mazeAppModel.getBox(boxLine, boxRow); + } + + public void setModelABox(int boxLine, int boxRow) { + this.mazeAppModel.setABox(boxLine, boxRow); + } + + public void setModelDBox(int boxLine, int boxRow) { + this.mazeAppModel.setDBox(boxLine, boxRow); + } + + public void setModelEBox(int boxLine, int boxRow) { + this.mazeAppModel.setEBox(boxLine, boxRow); + } + + public void setModelWBox(int boxLine, int boxRow) { + this.mazeAppModel.setWBox(boxLine, boxRow); + } + + public int isPathDrawn() { + return this.mazeAppModel.isPathDrawn(); + } + + public Maze getModelMaze() { + return this.mazeAppModel.getMaze(); + } + + public ArrayList getModelShortest() { + return this.mazeAppModel.getShortest(); + } + + public ArrayList getModelShortestDeepCopy() { + return this.mazeAppModel.getShortestDeepCopy(); + } + + public int getModelDimensionX() { + return this.mazeAppModel.getDimensionX(); + } + + public int getModelDimensionY() { + return this.mazeAppModel.getDimensionY(); + } + + public void setPathDrawn(int i) { + this.mazeAppModel.setPathDrawn(i); + } + + public void toggleModelBox(int boxLine, int boxRow) { + this.mazeAppModel.toggleBox(boxLine, boxRow); + } +} \ No newline at end of file diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index aea6f9e..1a82246 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -48,15 +48,16 @@ public class MazeDrawing extends JPanel implements MouseListener { int height = getHeight(); paintMaze(g2d, width, height); - if (mazeApp.getMazeAppModel().isPathDrawn() == 1) { + if (mazeApp.isPathDrawn() == 1) { animateShortestPath(g2d, width, height); - } else if (mazeApp.getMazeAppModel().isPathDrawn() == 2) { + } else if (mazeApp.isPathDrawn() == 2) { paintShortestPath(g2d, width, height); } } private void paintMaze(Graphics2D g, int width, int height) { - Maze maze = mazeApp.getMazeAppModel().getMaze(); + + Maze maze = mazeApp.getModelMaze(); int dimensionX = maze.getDimensionX(); int dimensionY = maze.getDimensionY(); int boxWidth = width / dimensionX; @@ -64,11 +65,11 @@ public class MazeDrawing extends JPanel implements MouseListener { for (int line = 0; line < dimensionY; line++) { for (int row = 0; row < dimensionX; row++) { - if (maze.getMaze()[line][row].isWalkable()) { - if (maze.getMaze()[line][row].isDeparture()) { + if (maze.getBox(line, row).isWalkable()) { + if (maze.getBox(line, row).isDeparture()) { g.setColor(Color.GREEN); } - else if (maze.getMaze()[line][row].isArrival()) { + else if (maze.getBox(line, row).isArrival()) { g.setColor(Color.RED); } else { @@ -90,9 +91,9 @@ public class MazeDrawing extends JPanel implements MouseListener { public void notifyForUpdates(Object param) { if (param == MazeAppModelMessage.MazeRenewal) { - if (this.mazeApp.getMazeAppModel().getShortest() != null) { + if (this.mazeApp.getModelShortest() != null) { this.finalPath - = this.mazeApp.getMazeAppModel().getShortestDeepCopy(); + = this.mazeApp.getModelShortestDeepCopy(); Collections.reverse(finalPath); } this.repaint(); @@ -102,15 +103,15 @@ public class MazeDrawing extends JPanel implements MouseListener { private void paintShortestPath(Graphics2D g, int width, int height) { g.setColor(Color.WHITE); - int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); - int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); + int dimensionX = mazeApp.getModelDimensionX(); + int dimensionY = mazeApp.getModelDimensionY(); int boxWidth = width / dimensionX; int boxHeight = height / dimensionY; int factorWidth = boxWidth / 3; int factorHeight = boxHeight / 3; ArrayList shortest = - mazeApp.getMazeAppModel().getShortest(); - if (shortest == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { + mazeApp.getModelShortest(); + if (shortest == null && mazeApp.isPathDrawn() == 0) { mazeApp.clearShortestPath(); JOptionPane.showMessageDialog(this, "Check that your graph contains a departure and an arrival," @@ -135,14 +136,14 @@ public class MazeDrawing extends JPanel implements MouseListener { private void animateShortestPath(Graphics2D g, int width, int height) { g.setColor(Color.WHITE); - int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); - int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); + int dimensionX = mazeApp.getModelDimensionX(); + int dimensionY = mazeApp.getModelDimensionY(); int boxWidth = width / dimensionX; int boxHeight = height / dimensionY; int factorWidth = boxWidth / 3; int factorHeight = boxHeight / 3; - if (this.finalPath == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { + if (this.finalPath == null && mazeApp.isPathDrawn() == 0) { mazeApp.clearShortestPath(); JOptionPane.showMessageDialog(this, "Check that your graph contains a departure and an arrival," @@ -193,7 +194,7 @@ public class MazeDrawing extends JPanel implements MouseListener { if (pathCounter >= finalPath.size()) { pathDrawingMode = false; - mazeApp.getMazeAppModel().setPathDrawn(2); + mazeApp.setPathDrawn(2); } } @@ -207,7 +208,7 @@ public class MazeDrawing extends JPanel implements MouseListener { boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY()); } else if (e.getButton() == MouseEvent.BUTTON1) { - mazeApp.getMazeAppModel().toggleBox(boxLine, boxRow); + mazeApp.toggleModelBox(boxLine, boxRow); } } else { shortestPath.add(this.finalPath.get(pathCounter)); @@ -231,13 +232,13 @@ public class MazeDrawing extends JPanel implements MouseListener { } private int getBoxRow(int x) { - int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); + int dimensionX = mazeApp.getModelDimensionX(); int boxWidth = getWidth() / dimensionX; return x / boxWidth; } private int getBoxLine(int y) { - int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); + int dimensionY = mazeApp.getModelDimensionY(); int boxHeight = getHeight() / dimensionY; return y / boxHeight; } diff --git a/src/ui/NewClearMazeMenuItem.java b/src/ui/NewClearMazeMenuItem.java index 19cc68a..bb79730 100644 --- a/src/ui/NewClearMazeMenuItem.java +++ b/src/ui/NewClearMazeMenuItem.java @@ -24,7 +24,6 @@ public class NewClearMazeMenuItem extends JMenuItem @Override public void actionPerformed(ActionEvent arg0) { - spinnerBox.createAndShowGUI(); } } diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index c8632ce..c7b46f4 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -24,13 +24,11 @@ public class QuitMenuItem extends JMenuItem public void actionPerformed(ActionEvent evt) { - MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - - if (! mazeAppModel.isSaved()) { - if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { - return; - } - } - System.exit(0); + if (! mazeApp.isModelSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { + return; + } + } + System.exit(0); } } diff --git a/src/ui/SetABoxMenuItem.java b/src/ui/SetABoxMenuItem.java index 2449a0c..02d98e0 100644 --- a/src/ui/SetABoxMenuItem.java +++ b/src/ui/SetABoxMenuItem.java @@ -21,6 +21,6 @@ public class SetABoxMenuItem extends JMenuItem } public void actionPerformed(ActionEvent evt) { - mazeApp.getMazeAppModel().setABox(boxLine, boxRow); + mazeApp.setModelABox(boxLine, boxRow); } } diff --git a/src/ui/SetDBoxMenuItem.java b/src/ui/SetDBoxMenuItem.java index fd9c5fd..1c46aa5 100644 --- a/src/ui/SetDBoxMenuItem.java +++ b/src/ui/SetDBoxMenuItem.java @@ -21,6 +21,6 @@ public class SetDBoxMenuItem extends JMenuItem } public void actionPerformed(ActionEvent evt) { - mazeApp.getMazeAppModel().setDBox(boxLine, boxRow); + mazeApp.setModelDBox(boxLine, boxRow); } } diff --git a/src/ui/SetEBoxMenuItem.java b/src/ui/SetEBoxMenuItem.java index 85a41fd..f19ae09 100644 --- a/src/ui/SetEBoxMenuItem.java +++ b/src/ui/SetEBoxMenuItem.java @@ -21,6 +21,6 @@ public class SetEBoxMenuItem extends JMenuItem } public void actionPerformed(ActionEvent evt) { - mazeApp.getMazeAppModel().setEBox(boxLine, boxRow); + mazeApp.setModelEBox(boxLine, boxRow); } } diff --git a/src/ui/SetWBoxMenuItem.java b/src/ui/SetWBoxMenuItem.java index 3cc032b..7888de9 100644 --- a/src/ui/SetWBoxMenuItem.java +++ b/src/ui/SetWBoxMenuItem.java @@ -21,6 +21,6 @@ public class SetWBoxMenuItem extends JMenuItem } public void actionPerformed(ActionEvent evt) { - mazeApp.getMazeAppModel().setWBox(boxLine, boxRow); + mazeApp.setModelWBox(boxLine, boxRow); } } -- 2.56.1 From e76db727c70ea913eb2886178cb43a79d6165f00 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 13 Jan 2018 19:19:41 +0100 Subject: [PATCH 2/5] Fix regression on getDimensionX and add finals * Make all appropriate methods final * Add new messages to MazeAppModelMessage and handle them to act more accurately on observers updates * Make all buttons from 'File' menu subclasses of FileMenu in order to have a cleaner ui folder * Remove legacy code (for example, the former code used to animate the path drawing) * Improve delegation --- src/dijkstra/ASet.java | 2 +- src/dijkstra/Dijkstra.java | 2 +- src/dijkstra/Pi.java | 2 +- src/dijkstra/Previous.java | 6 +- src/maze/ABox.java | 4 +- src/maze/Box.java | 11 ++- src/maze/DBox.java | 4 +- src/maze/EBox.java | 2 +- src/maze/Maze.java | 28 +++--- src/maze/MazeReadingException.java | 2 +- src/maze/WBox.java | 4 +- src/model/MazeAppModel.java | 48 ++++++---- src/model/MazeAppModelMessage.java | 19 +++- src/ui/AddLineMenuItem.java | 2 +- src/ui/AddRowMenuItem.java | 2 +- src/ui/AnimatePathMenuItem.java | 2 +- src/ui/BoxTypeEditionMenu.java | 2 +- src/ui/ClearMazeMenuItem.java | 2 +- src/ui/ClearShortestPathMenuItem.java | 2 +- src/ui/DelLineMenuItem.java | 2 +- src/ui/DelRowMenuItem.java | 2 +- src/ui/DrawShortestPathMenuItem.java | 2 +- src/ui/FileBox.java | 2 +- src/ui/FileMenu.java | 122 +++++++++++++++++++++++++- src/ui/LoadMazeMenuItem.java | 35 -------- src/ui/MazeApp.java | 20 ++++- src/ui/MazeDrawing.java | 28 ++---- src/ui/MazeMenu.java | 2 +- src/ui/MazeMenuBar.java | 2 +- src/ui/NewClearMazeMenuItem.java | 2 +- src/ui/NewMazeSpinnerBox.java | 4 +- src/ui/QuitMenuItem.java | 34 ------- src/ui/SaveBox.java | 11 +-- src/ui/SaveMazeAsMenuItem.java | 32 ------- src/ui/SaveMazeMenuItem.java | 47 ---------- src/ui/SetABoxMenuItem.java | 2 +- src/ui/SetDBoxMenuItem.java | 2 +- src/ui/SetEBoxMenuItem.java | 2 +- src/ui/SetWBoxMenuItem.java | 2 +- src/ui/WindowPanel.java | 2 +- 40 files changed, 246 insertions(+), 257 deletions(-) delete mode 100644 src/ui/LoadMazeMenuItem.java delete mode 100644 src/ui/QuitMenuItem.java delete mode 100644 src/ui/SaveMazeAsMenuItem.java delete mode 100644 src/ui/SaveMazeMenuItem.java diff --git a/src/dijkstra/ASet.java b/src/dijkstra/ASet.java index 09e41a8..3bfeff3 100644 --- a/src/dijkstra/ASet.java +++ b/src/dijkstra/ASet.java @@ -2,7 +2,7 @@ package dijkstra; import java.util.HashSet; -public class ASet implements ASetInterface { +public final class ASet implements ASetInterface { private HashSet vertexHashSet; diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index d709f32..2437f1a 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -3,7 +3,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; -public class Dijkstra { +public final class Dijkstra { private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, diff --git a/src/dijkstra/Pi.java b/src/dijkstra/Pi.java index 6b7a7ca..43356bb 100644 --- a/src/dijkstra/Pi.java +++ b/src/dijkstra/Pi.java @@ -2,7 +2,7 @@ package dijkstra; import java.util.Hashtable; -public class Pi implements PiInterface { +public final class Pi implements PiInterface { private Hashtable hashPi; diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 0b5c613..95f956d 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -6,7 +6,7 @@ import java.util.List; import maze.Box; -public class Previous implements PreviousInterface { +public final class Previous implements PreviousInterface { private Hashtable hashPrevious; private ArrayList shortestPath; @@ -32,13 +32,13 @@ public class Previous implements PreviousInterface { shortestPath.add(vertex); VertexInterface father = getPrevious(vertex); - + if (getPrevious(father) == null) { shortestPath.add(father); return shortestPath; } else { return getShortestPathTo(father); - } + } } } diff --git a/src/maze/ABox.java b/src/maze/ABox.java index 66b5a08..3492c9d 100644 --- a/src/maze/ABox.java +++ b/src/maze/ABox.java @@ -5,9 +5,9 @@ package maze; * It's the last Box to be walked through and * it's an exit to the Maze. */ -public class ABox extends EBox{ +public final class ABox extends EBox{ - public ABox (String label, int y, int x, Maze maze) { + public ABox(String label, int y, int x, Maze maze) { super(label, y, x, maze) ; } diff --git a/src/maze/Box.java b/src/maze/Box.java index db1871f..8b4d4d5 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -24,17 +24,16 @@ public abstract class Box private String label; private Maze maze; private ArrayList neighbourList; - private ArrayList candidates; + private ArrayList candidates = + new ArrayList(); - public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation - + public Box(String label, int y , int x, Maze maze) { //y first because of graphical maze representation this.label = label; this.x = x; this.y = y; this.maze = maze; - this.candidates = new ArrayList(); } - + public ArrayList generateNeighbours() { this.neighbourList = maze.generateNeighbours(this); return this.neighbourList; @@ -63,7 +62,7 @@ public abstract class Box public boolean isDeparture() { return false; } - + public ArrayList getNeighbourList() { return neighbourList; } diff --git a/src/maze/DBox.java b/src/maze/DBox.java index aacfc1d..21057cc 100644 --- a/src/maze/DBox.java +++ b/src/maze/DBox.java @@ -5,9 +5,9 @@ package maze; * is first in the Maze. */ -public class DBox extends EBox{ +public final class DBox extends EBox{ - public DBox (String label, int y, int x, Maze maze) { + public DBox(String label, int y, int x, Maze maze) { super(label, y, x, maze); } diff --git a/src/maze/EBox.java b/src/maze/EBox.java index 8c50e0c..b308f8d 100644 --- a/src/maze/EBox.java +++ b/src/maze/EBox.java @@ -7,7 +7,7 @@ package maze; public class EBox extends Box{ - public EBox (String label, int y, int x, Maze maze) { + public EBox(String label, int y, int x, Maze maze) { super(label, y, x, maze); } } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index a30a2b8..c0c173e 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -16,7 +16,7 @@ import dijkstra.VertexInterface; * @author Sylvain Rager * */ -public class Maze +public final class Maze implements GraphInterface { private int dimensionX; @@ -26,7 +26,7 @@ public class Maze public Maze(String filename) { initFromTextFile(filename); } - + public Maze(Maze maze) { this.dimensionX = maze.getDimensionX(); @@ -369,7 +369,7 @@ public class Maze } public ArrayList generateNeighbours(VertexInterface vertex) { - + Box box = (Box) vertex; int x = box.getX(); int y = box.getY(); @@ -383,28 +383,28 @@ public class Maze if (x > 0 && maze[y][x-1].isWalkable()) { neighbourList.add(maze[y][x-1]); } - + if (x < dimensionX-1 && maze[y][x+1].isWalkable()) { neighbourList.add(maze[y][x+1]); } - + if (y > 0 && maze[y-1][x].isWalkable()) { neighbourList.add(maze[y-1][x]); } - + if (y < dimensionY-1 && maze[y+1][x].isWalkable()) { neighbourList.add(maze[y+1][x]); } - + return neighbourList; } - /**Constructs the maze from a text file, according to each single character on each line. + /**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. */ - private final void initFromTextFile(String filename) { + private void initFromTextFile(String filename) { FileReader fr = null; BufferedReader br = null; @@ -422,9 +422,9 @@ public class Maze try { //generates the maze fr = new FileReader(filename); br = new BufferedReader(fr); - + String currentLine = null; - + currentLine = br.readLine(); dimensionX = currentLine.length(); //maze's width int currentLineLength; @@ -443,7 +443,7 @@ public class Maze char currentChar = currentLine.charAt(compteurColonne); switch(currentChar) { - + case 'W': maze[compteurLigne][compteurColonne] = new WBox("W", compteurLigne, compteurColonne, this) ; @@ -483,7 +483,7 @@ public class Maze } } - public final void saveToTextFile(String fileName) { + public void saveToTextFile(String fileName) { FileOutputStream fos = null; PrintWriter pw = null; @@ -527,7 +527,7 @@ public class Maze return dimensionY; } - public Box[][] getMaze() { + protected Box[][] getMaze() { return maze; } diff --git a/src/maze/MazeReadingException.java b/src/maze/MazeReadingException.java index 47b5600..2e42204 100644 --- a/src/maze/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -8,7 +8,7 @@ package maze; * */ -public class MazeReadingException +public final class MazeReadingException extends Exception { private static final long serialVersionUID = 1L; diff --git a/src/maze/WBox.java b/src/maze/WBox.java index cb17318..6c4434a 100644 --- a/src/maze/WBox.java +++ b/src/maze/WBox.java @@ -5,9 +5,9 @@ package maze; * that can't be walked through (a wall). */ -public class WBox extends Box { +public final class WBox extends Box { - public WBox (String label, int y, int x, Maze maze) { + public WBox(String label, int y, int x, Maze maze) { super(label, y, x, maze); } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 4e0e939..846db8d 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -11,14 +11,13 @@ import dijkstra.*; import maze.*; public class MazeAppModel extends Observable { - + /** The file into which the maze is to be saved */ private String filename; private Maze maze; private VertexInterface departure; private VertexInterface arrival; private Previous previous; private ArrayList shortest; - private ArrayList boxList; private boolean saved = true; /* * There are three states for pathDrawn : @@ -38,7 +37,6 @@ public class MazeAppModel extends Observable { private void initModelFromMaze() { departure = maze.getDeparture(); arrival = maze.getArrival(); - boxList = maze.getVertexes(); if (departure == null || arrival == null) { previous = null; shortest = null; @@ -52,8 +50,6 @@ public class MazeAppModel extends Observable { shortest = previous.getShortestPathTo(arrival); } } - setChanged(); - notifyObservers(MazeAppModelMessage.MazeRenewal); } public void loadMaze(String filename) { @@ -61,16 +57,14 @@ public class MazeAppModel extends Observable { maze = new Maze(this.filename); initModelFromMaze(); saved = true; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeLoaded); } public Maze getMaze() { return maze; } - public Box[][] getBoxMaze() { - return this.maze.getMaze(); - } - public Box getBox(int boxLine, int boxRow) { return this.maze.getBox(boxLine, boxRow); } @@ -87,12 +81,12 @@ public class MazeAppModel extends Observable { return shortest; } - public ArrayList getShortestDeepCopy() { - ArrayList deepCopy = new ArrayList(); + public ArrayList getShortestShallowCopy() { + ArrayList shallowCopy = new ArrayList(); for (VertexInterface vertex : this.shortest) { - deepCopy.add(vertex); + shallowCopy.add(vertex); } - return deepCopy; + return shallowCopy; } public int isPathDrawn() { @@ -106,10 +100,12 @@ public class MazeAppModel extends Observable { public void saveToFile() { maze.saveToTextFile(filename); saved = true; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeSaved); } public void saveToFile(String filename) { - this.filename = new String(filename); + this.filename = filename; maze.saveToTextFile(this.filename); saved = true; setChanged(); @@ -117,7 +113,7 @@ public class MazeAppModel extends Observable { } public void toggleBox(int boxLine, int boxRow) { - if (maze.getMaze()[boxLine][boxRow].isWalkable()) { + if (maze.getBox(boxLine, boxRow).isWalkable()) { setWBox(boxLine, boxRow); } else { @@ -130,6 +126,8 @@ public class MazeAppModel extends Observable { maze.resetNeighbourLists(); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void setEBox(int boxLine, int boxRow) { @@ -137,6 +135,8 @@ public class MazeAppModel extends Observable { maze.resetNeighbourLists(); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void setDBox(int boxLine, int boxRow) { @@ -149,6 +149,8 @@ public class MazeAppModel extends Observable { maze.resetNeighbourLists(); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void setABox(int boxLine, int boxRow) { @@ -161,6 +163,8 @@ public class MazeAppModel extends Observable { maze.resetNeighbourLists(); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void newClearMaze(int numberOfLines, int numberOfRows) { @@ -168,6 +172,8 @@ public class MazeAppModel extends Observable { maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows)); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void addEmptyColumn() { @@ -178,12 +184,16 @@ public class MazeAppModel extends Observable { maze.addEmptyColumn(posColumn); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void delColumn(int posColumn) { maze.delColumn(posColumn); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void delColumn() { @@ -198,12 +208,16 @@ public class MazeAppModel extends Observable { maze.addEmptyLine(posLine); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void delLine(int posLine) { maze.delLine(posLine); initModelFromMaze(); saved = false; + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void delLine() { @@ -219,10 +233,10 @@ public class MazeAppModel extends Observable { } public int getDimensionX() { - return this.maze.getDimensionX(); + return maze.getDimensionX(); } public int getDimensionY() { - return this.maze.getDimensionX(); + return maze.getDimensionY(); } } diff --git a/src/model/MazeAppModelMessage.java b/src/model/MazeAppModelMessage.java index af9cf63..358b2db 100644 --- a/src/model/MazeAppModelMessage.java +++ b/src/model/MazeAppModelMessage.java @@ -1,6 +1,19 @@ package model; -public class MazeAppModelMessage { - public final static String MazeRenewal = "MazeRenewal"; - public final static String FileChange = "FileChange"; +/** + * This class is used to transport messages about an event happening + * to the model + */ +public abstract class MazeAppModelMessage { + /** Indicates that the maze has been renewed + *(a replacement of the maze -resizing, new maze, ...- + * or a change on any box) + */ + public final static String MazeRenewal = "Maze renewed"; + /** Indicates a new file has been selected for the maze to be saved in */ + public final static String FileChange = "File changed"; + /** Indicates the maze has just been saved */ + public final static String MazeSaved = "Maze saved"; + /** Indicates a maze has been loaded from file */ + public final static String MazeLoaded = "Maze loaded"; } diff --git a/src/ui/AddLineMenuItem.java b/src/ui/AddLineMenuItem.java index 095760d..be9a246 100644 --- a/src/ui/AddLineMenuItem.java +++ b/src/ui/AddLineMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class AddLineMenuItem extends JMenuItem +public final class AddLineMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/AddRowMenuItem.java b/src/ui/AddRowMenuItem.java index b49dfd7..2bf355b 100644 --- a/src/ui/AddRowMenuItem.java +++ b/src/ui/AddRowMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class AddRowMenuItem extends JMenuItem +public final class AddRowMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/AnimatePathMenuItem.java b/src/ui/AnimatePathMenuItem.java index f1f2f08..dd37230 100644 --- a/src/ui/AnimatePathMenuItem.java +++ b/src/ui/AnimatePathMenuItem.java @@ -7,7 +7,7 @@ import javax.swing.Action; import javax.swing.Icon; import javax.swing.JMenuItem; -public class AnimatePathMenuItem extends JMenuItem +public final class AnimatePathMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/BoxTypeEditionMenu.java b/src/ui/BoxTypeEditionMenu.java index b49e166..9c83640 100644 --- a/src/ui/BoxTypeEditionMenu.java +++ b/src/ui/BoxTypeEditionMenu.java @@ -4,7 +4,7 @@ import javax.swing.JPopupMenu; import javax.swing.JMenuItem; import maze.Box; -public class BoxTypeEditionMenu extends JPopupMenu { +public final class BoxTypeEditionMenu extends JPopupMenu { JMenuItem setWBox; JMenuItem setEBox; diff --git a/src/ui/ClearMazeMenuItem.java b/src/ui/ClearMazeMenuItem.java index aeed823..24d0d0f 100644 --- a/src/ui/ClearMazeMenuItem.java +++ b/src/ui/ClearMazeMenuItem.java @@ -6,7 +6,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; import javax.swing.KeyStroke; -public class ClearMazeMenuItem extends JMenuItem +public final class ClearMazeMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/ClearShortestPathMenuItem.java b/src/ui/ClearShortestPathMenuItem.java index e5bff7b..7cbd439 100644 --- a/src/ui/ClearShortestPathMenuItem.java +++ b/src/ui/ClearShortestPathMenuItem.java @@ -7,7 +7,7 @@ import model.MazeAppModel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class ClearShortestPathMenuItem extends JMenuItem +public final class ClearShortestPathMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/DelLineMenuItem.java b/src/ui/DelLineMenuItem.java index 165ac6e..d33db6a 100644 --- a/src/ui/DelLineMenuItem.java +++ b/src/ui/DelLineMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class DelLineMenuItem extends JMenuItem +public final class DelLineMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/DelRowMenuItem.java b/src/ui/DelRowMenuItem.java index 243b129..c8b876a 100644 --- a/src/ui/DelRowMenuItem.java +++ b/src/ui/DelRowMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class DelRowMenuItem extends JMenuItem +public final class DelRowMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/DrawShortestPathMenuItem.java b/src/ui/DrawShortestPathMenuItem.java index 4338172..b713994 100644 --- a/src/ui/DrawShortestPathMenuItem.java +++ b/src/ui/DrawShortestPathMenuItem.java @@ -7,7 +7,7 @@ import model.MazeAppModel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class DrawShortestPathMenuItem extends JMenuItem +public final class DrawShortestPathMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/FileBox.java b/src/ui/FileBox.java index fc651c2..9ad443c 100644 --- a/src/ui/FileBox.java +++ b/src/ui/FileBox.java @@ -3,7 +3,7 @@ package ui; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; -public class FileBox { +public final class FileBox { private static JFileChooser fileChooser = new JFileChooser(); private static FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt"); diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index baadb8e..3a246a3 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -1,8 +1,17 @@ package ui; import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.KeyStroke; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Observer; +import java.util.Observable; +import javax.swing.JOptionPane; -public class FileMenu extends JMenu { +import model.MazeAppModelMessage; + +public final class FileMenu extends JMenu { private final MazeApp mazeApp; private final LoadMazeMenuItem loadMazeMenuItem; @@ -20,4 +29,115 @@ public class FileMenu extends JMenu { add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp)); add(quitMenuItem = new QuitMenuItem(mazeApp)); } + + + public final class LoadMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public LoadMazeMenuItem(MazeApp mazeApp) { + + super("Load maze"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control O")); + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + if (! mazeApp.isModelSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ + return; + } + } + String filename = FileBox.load(mazeApp); + if (filename != null) { + mazeApp.loadModelMaze(filename); + } + } + } + + + public final class SaveMazeMenuItem extends JMenuItem + implements ActionListener, Observer { + + private final MazeApp mazeApp; + + public SaveMazeMenuItem(MazeApp mazeApp) { + + super("Save maze"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control S")); + mazeApp.getMazeAppModel().addObserver(this); + if (mazeApp.getMazeAppModel().getFilename() == null) { + this.setEnabled(false); + } + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + if (mazeApp.getModelFilename() != null) { + mazeApp.saveModelMaze(); + } + } + + @Override + public void update(Observable obs, Object param) { + if (param == MazeAppModelMessage.MazeRenewal && + mazeApp.getModelFilename() != null) { + this.setEnabled(true); + } + else { + this.setEnabled(false); + } + } + } + + + public final class SaveMazeAsMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public SaveMazeAsMenuItem(MazeApp mazeApp) { + + super("Save maze as"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control shift S")); + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + String filename = FileBox.save(mazeApp); + if (filename != null) { + mazeApp.saveModelMaze(filename); + } + } + } + + + public final class QuitMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public QuitMenuItem(MazeApp mazeApp) { + + super("Quit"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control Q")); + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + if (! mazeApp.isModelSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { + return; + } + } + System.exit(0); + } + } } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java deleted file mode 100644 index 65328e4..0000000 --- a/src/ui/LoadMazeMenuItem.java +++ /dev/null @@ -1,35 +0,0 @@ -package ui; - -import javax.swing.JMenuItem; -import javax.swing.KeyStroke; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import model.MazeAppModel; - -public class LoadMazeMenuItem extends JMenuItem - implements ActionListener { - - private final MazeApp mazeApp; - - public LoadMazeMenuItem(MazeApp mazeApp) { - - super("Load maze"); - this.mazeApp = mazeApp; - this.setAccelerator(KeyStroke.getKeyStroke("control O")); - addActionListener(this); - } - - public void actionPerformed(ActionEvent evt) { - - if (mazeApp.isModelSaved()) { - if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ - return; - } - } - String filename = FileBox.load(mazeApp); - if (filename != null) { - mazeApp.loadModelMaze(filename); - } - } -} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 1612085..5e2b31b 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -13,7 +13,7 @@ import java.util.Observer; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; -public class MazeApp extends JFrame +public final class MazeApp extends JFrame implements Observer, WindowListener { private final MazeMenuBar mazeMenuBar; @@ -145,6 +145,18 @@ public class MazeApp extends JFrame return this.mazeAppModel.getBox(boxLine, boxRow); } + public void saveModelMaze(String filename) { + mazeAppModel.saveToFile(filename); + } + + public void saveModelMaze() { + mazeAppModel.saveToFile(); + } + + public String getModelFilename() { + return mazeAppModel.getFilename(); + } + public void setModelABox(int boxLine, int boxRow) { this.mazeAppModel.setABox(boxLine, boxRow); } @@ -173,8 +185,8 @@ public class MazeApp extends JFrame return this.mazeAppModel.getShortest(); } - public ArrayList getModelShortestDeepCopy() { - return this.mazeAppModel.getShortestDeepCopy(); + public ArrayList getModelShortestShallowCopy() { + return this.mazeAppModel.getShortestShallowCopy(); } public int getModelDimensionX() { @@ -192,4 +204,4 @@ public class MazeApp extends JFrame public void toggleModelBox(int boxLine, int boxRow) { this.mazeAppModel.toggleBox(boxLine, boxRow); } -} \ No newline at end of file +} diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 1a82246..2437cbf 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -18,7 +18,7 @@ import model.MazeAppModelMessage; import maze.EBox; import dijkstra.VertexInterface; -public class MazeDrawing extends JPanel implements MouseListener { +public final class MazeDrawing extends JPanel implements MouseListener { private final MazeApp mazeApp; private boolean pathDrawingMode; @@ -39,7 +39,7 @@ public class MazeDrawing extends JPanel implements MouseListener { } @Override - protected final void paintComponent(Graphics g) { + protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; super.paintComponent(g2d); @@ -90,10 +90,11 @@ public class MazeDrawing extends JPanel implements MouseListener { public void notifyForUpdates(Object param) { - if (param == MazeAppModelMessage.MazeRenewal) { + if (param == MazeAppModelMessage.MazeRenewal || + param == MazeAppModelMessage.MazeLoaded) { if (this.mazeApp.getModelShortest() != null) { this.finalPath - = this.mazeApp.getModelShortestDeepCopy(); + = this.mazeApp.getModelShortestShallowCopy(); Collections.reverse(finalPath); } this.repaint(); @@ -172,25 +173,6 @@ public class MazeDrawing extends JPanel implements MouseListener { boxWidth - 2 * factorWidth, boxHeight - 2 * factorHeight); } -// Timer timer = new Timer(16, new ActionListener(){ -// int compteur = shortestPath.size()-1; -// VertexInterface vertex; -// @Override -// public void actionPerformed(ActionEvent e) { -// if (compteur < 0) { -// ((Timer)e.getSource()).stop(); -// } else { -// vertex = shortestPath.get(compteur); -// EBox box = (EBox) vertex; -// g.fillOval(box.getX() * boxWidth + factorWidth, -// box.getY()*boxHeight + factorHeight, -// boxWidth - 2 * factorWidth, -// boxHeight - 2 * factorHeight); -// compteur -= 1; -// } -// } -// }); -// timer.start(); if (pathCounter >= finalPath.size()) { pathDrawingMode = false; diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index e3840c1..4640758 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -2,7 +2,7 @@ package ui; import javax.swing.JMenu; -public class MazeMenu extends JMenu { +public final class MazeMenu extends JMenu { private final MazeApp mazeApp; private final DrawShortestPathMenuItem drawMenuItem; diff --git a/src/ui/MazeMenuBar.java b/src/ui/MazeMenuBar.java index afe76da..0a51c7a 100644 --- a/src/ui/MazeMenuBar.java +++ b/src/ui/MazeMenuBar.java @@ -2,7 +2,7 @@ package ui; import javax.swing.JMenuBar; -public class MazeMenuBar extends JMenuBar { +public final class MazeMenuBar extends JMenuBar { private final MazeApp mazeApp; private final FileMenu fileMenu; diff --git a/src/ui/NewClearMazeMenuItem.java b/src/ui/NewClearMazeMenuItem.java index bb79730..a0a663b 100644 --- a/src/ui/NewClearMazeMenuItem.java +++ b/src/ui/NewClearMazeMenuItem.java @@ -7,7 +7,7 @@ import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.KeyStroke; -public class NewClearMazeMenuItem extends JMenuItem +public final class NewClearMazeMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/NewMazeSpinnerBox.java b/src/ui/NewMazeSpinnerBox.java index 2e5397c..5fb11f8 100644 --- a/src/ui/NewMazeSpinnerBox.java +++ b/src/ui/NewMazeSpinnerBox.java @@ -16,7 +16,7 @@ import javax.swing.SpinnerModel; import javax.swing.SpinnerNumberModel; import javax.swing.SpringLayout; -public class NewMazeSpinnerBox extends JPanel +public final class NewMazeSpinnerBox extends JPanel implements ActionListener { private final MazeApp mazeApp; @@ -48,7 +48,7 @@ public class NewMazeSpinnerBox extends JPanel JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel); } - static protected JSpinner addLabeledSpinner(Container c, + protected static JSpinner addLabeledSpinner(Container c, String label, SpinnerModel model) { diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java deleted file mode 100644 index c7b46f4..0000000 --- a/src/ui/QuitMenuItem.java +++ /dev/null @@ -1,34 +0,0 @@ -package ui; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.KeyStroke; - -import model.MazeAppModel; - -public class QuitMenuItem extends JMenuItem - implements ActionListener { - - private final MazeApp mazeApp; - - public QuitMenuItem(MazeApp mazeApp) { - - super("Quit"); - this.mazeApp = mazeApp; - this.setAccelerator(KeyStroke.getKeyStroke("control Q")); - addActionListener(this); - } - - public void actionPerformed(ActionEvent evt) { - - if (! mazeApp.isModelSaved()) { - if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { - return; - } - } - System.exit(0); - } -} diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java index ae354f2..f065d58 100644 --- a/src/ui/SaveBox.java +++ b/src/ui/SaveBox.java @@ -3,15 +3,12 @@ package ui; import javax.swing.JOptionPane; import java.awt.Component; -import model.MazeAppModel; - -public class SaveBox { +public final class SaveBox { private static String message = "Maze not saved. Save it ?"; public static boolean promptAndContinue(Component parent, MazeApp mazeApp, String title) { - MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); int response = JOptionPane.showOptionDialog(parent, message, title, @@ -22,13 +19,13 @@ public class SaveBox { case JOptionPane.CANCEL_OPTION: return false; case JOptionPane.OK_OPTION: - if (mazeAppModel.getFilename() != null) { - mazeAppModel.saveToFile(); + if (mazeApp.getModelFilename() != null) { + mazeApp.saveModelMaze(); } else { String filename = FileBox.save(mazeApp); if (filename != null) { - mazeApp.getMazeAppModel().saveToFile(filename); + mazeApp.saveModelMaze(filename); } else { return promptAndContinue(null, mazeApp, title); diff --git a/src/ui/SaveMazeAsMenuItem.java b/src/ui/SaveMazeAsMenuItem.java deleted file mode 100644 index 22195f1..0000000 --- a/src/ui/SaveMazeAsMenuItem.java +++ /dev/null @@ -1,32 +0,0 @@ -package ui; - -import javax.swing.JMenuItem; -import javax.swing.KeyStroke; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import model.MazeAppModel; - -public class SaveMazeAsMenuItem extends JMenuItem - implements ActionListener { - - private final MazeApp mazeApp; - - public SaveMazeAsMenuItem(MazeApp mazeApp) { - - super("Save maze as"); - this.mazeApp = mazeApp; - this.setAccelerator(KeyStroke.getKeyStroke("control shift S")); - addActionListener(this); - } - - public void actionPerformed(ActionEvent evt) { - - MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - - String filename = FileBox.save(mazeApp); - if (filename != null) { - mazeApp.getMazeAppModel().saveToFile(filename); - } - } -} diff --git a/src/ui/SaveMazeMenuItem.java b/src/ui/SaveMazeMenuItem.java deleted file mode 100644 index 13b8c8e..0000000 --- a/src/ui/SaveMazeMenuItem.java +++ /dev/null @@ -1,47 +0,0 @@ -package ui; - -import javax.swing.JMenuItem; -import javax.swing.KeyStroke; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Observer; -import java.util.Observable; - -import model.MazeAppModel; - -public class SaveMazeMenuItem extends JMenuItem - implements ActionListener, Observer { - - private final MazeApp mazeApp; - - public SaveMazeMenuItem(MazeApp mazeApp) { - - super("Save maze"); - this.mazeApp = mazeApp; - this.setAccelerator(KeyStroke.getKeyStroke("control S")); - mazeApp.getMazeAppModel().addObserver(this); - if (mazeApp.getMazeAppModel().getFilename() == null) { - this.setEnabled(false); - } - addActionListener(this); - } - - public void actionPerformed(ActionEvent evt) { - - MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - - if (mazeAppModel.getFilename() != null) { - mazeAppModel.saveToFile(); - } - } - - @Override - public void update(Observable obs, Object param) { - if (mazeApp.getMazeAppModel().getFilename() != null) { - this.setEnabled(true); - } - else { - this.setEnabled(false); - } - } -} diff --git a/src/ui/SetABoxMenuItem.java b/src/ui/SetABoxMenuItem.java index 02d98e0..11200d7 100644 --- a/src/ui/SetABoxMenuItem.java +++ b/src/ui/SetABoxMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class SetABoxMenuItem extends JMenuItem +public final class SetABoxMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/SetDBoxMenuItem.java b/src/ui/SetDBoxMenuItem.java index 1c46aa5..74530d1 100644 --- a/src/ui/SetDBoxMenuItem.java +++ b/src/ui/SetDBoxMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class SetDBoxMenuItem extends JMenuItem +public final class SetDBoxMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/SetEBoxMenuItem.java b/src/ui/SetEBoxMenuItem.java index f19ae09..a73e054 100644 --- a/src/ui/SetEBoxMenuItem.java +++ b/src/ui/SetEBoxMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class SetEBoxMenuItem extends JMenuItem +public final class SetEBoxMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/SetWBoxMenuItem.java b/src/ui/SetWBoxMenuItem.java index 7888de9..3781a73 100644 --- a/src/ui/SetWBoxMenuItem.java +++ b/src/ui/SetWBoxMenuItem.java @@ -5,7 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; -public class SetWBoxMenuItem extends JMenuItem +public final class SetWBoxMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 4219cda..72b38b5 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -4,7 +4,7 @@ import javax.swing.*; import maze.Maze; import java.awt.*; -public class WindowPanel extends JPanel { +public final class WindowPanel extends JPanel { private final MazeDrawing drawingPanelGrid; -- 2.56.1 From dc8de8c707f2c0d73620b6f6829892af95b11238 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 14 Jan 2018 16:44:15 +0100 Subject: [PATCH 3/5] Add MazeModelInterface and Maze exceptions * Add an interface for MazeModel * Add MazeReadingException and MazeSavingException to handle errors when reading/saving a maze from/to a file (see #4) * Useless methods removal (shallowShortest) * Add javadoc --- src/dijkstra/VertexInterface.java | 3 ++ src/maze/Maze.java | 64 +++++++++++++++++----- src/maze/MazeReadingException.java | 7 +-- src/maze/MazeSavingException.java | 16 ++++++ src/model/MazeAppModel.java | 86 +++++++++++++++++++----------- src/model/MazeModelInterface.java | 30 +++++++++++ src/ui/FileBox.java | 4 -- src/ui/FileMenu.java | 36 +++++++++++-- src/ui/MazeApp.java | 49 ++++++++++++++--- src/ui/MazeDrawing.java | 61 ++++++++++++++++----- src/ui/SaveBox.java | 23 +++++++- 11 files changed, 300 insertions(+), 79 deletions(-) create mode 100644 src/maze/MazeSavingException.java create mode 100644 src/model/MazeModelInterface.java diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index 4061a20..5e060ed 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -19,6 +19,9 @@ public interface VertexInterface { */ public ArrayList generateNeighbours(); + /** Returns the neighbourList of the element. */ + public ArrayList getNeighbourList(); + /** * Adds all vertexes not in the ASet and candidate for an eventual path. * @param vertex diff --git a/src/maze/Maze.java b/src/maze/Maze.java index c0c173e..36e215c 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -3,6 +3,7 @@ import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.FileReader; import java.io.PrintWriter; +import java.io.IOException; import java.util.ArrayList; import dijkstra.GraphInterface; @@ -17,13 +18,14 @@ import dijkstra.VertexInterface; * */ public final class Maze - implements GraphInterface { + implements GraphInterface { private int dimensionX; private int dimensionY; private Box[][] maze; - public Maze(String filename) { + public Maze(String filename) + throws MazeReadingException { initFromTextFile(filename); } @@ -51,6 +53,12 @@ public final class Maze this.maze = matrixDeepCopy(newMaze); } + /** + * matrixDeepCopy methods allow to deep copy a matrix of Box + * (as stored in a Maze) or only a part of it inside a new matrix of Box. + * The new matrix is therefore a truly new matrix with new boxes, which + * are not references to a previously used object. + */ private Box[][] matrixDeepCopy(Box[][] original, Box[][] destination, int numberOfLines, @@ -126,6 +134,7 @@ public final class Maze return matrixDeepCopy(original, original.length, original[0].length); } + /** Create a square maze of empty boxes of the given dimension */ public static Maze emptyMaze(int dimension) { Box[][] maze = new Box[dimension][dimension]; for (int line = 0; line < dimension; line++) { @@ -136,6 +145,7 @@ public final class Maze return new Maze(dimension, dimension, maze); } + /** Create a rectangle maze of empty boxes of the given dimensions */ public static Maze emptyMaze(int numberOfLines, int numberOfRows) { Box[][] maze = new Box[numberOfLines][numberOfRows]; for (int line = 0; line < numberOfLines; line++) { @@ -146,6 +156,7 @@ public final class Maze return new Maze(numberOfLines, numberOfRows, maze); } + /** Create a square maze of wall boxes of the given dimension */ public static Maze wallMaze(int dimension) { Box[][] maze = new Box[dimension][dimension]; for (int line = 0; line < dimension; line++) { @@ -156,6 +167,7 @@ public final class Maze return new Maze(dimension, dimension, maze); } + /** Create a rectangle maze of wall boxes of the given dimensions */ public static Maze wallMaze(int numberOfLines, int numberOfRows) { Box[][] maze = new Box[numberOfLines][numberOfRows]; for (int line = 0; line < numberOfLines; line++) { @@ -166,6 +178,7 @@ public final class Maze return new Maze(numberOfLines, numberOfRows, maze); } + /** Add a column of empty boxes at the given position */ public void addEmptyColumn(int posColumn) { Box[][] newMaze = matrixDeepCopy(maze, dimensionY, @@ -296,24 +309,45 @@ public final class Maze dimensionY = dimensionY - 1; } + /** Return the box at given coordinates */ public Box getBox(int boxLine, int boxRow) { return this.maze[boxLine][boxRow]; } + /** + * Set the box at given coordinates to an arrival box. If an arrival box + * was already in the maze, set it to an empty box. + */ public void setABox(int boxLine, int boxRow) { + if (getArrival() != null) { + setEBox(((Box) getArrival()).getY(), ((Box) getArrival()).getX()); + } this.maze[boxLine][boxRow] = new ABox("A", boxLine, boxRow, this); + resetNeighbourLists(); } + /** + * Set the box at given coordinates to a departure box. If a departure box + * was already in the maze, set it to an empty box. + */ public void setDBox(int boxLine, int boxRow) { + if (getDeparture() != null) { + setEBox(((Box) getDeparture()).getY(), ((Box) getDeparture()).getX()); + } this.maze[boxLine][boxRow] = new DBox("D", boxLine, boxRow, this); + resetNeighbourLists(); } + /** Set the box at given coordinates to an empty box */ public void setEBox(int boxLine, int boxRow) { this.maze[boxLine][boxRow] = new EBox("E", boxLine, boxRow, this); + resetNeighbourLists(); } + /** Set the box at given coordinates to a wall box */ public void setWBox(int boxLine, int boxRow) { this.maze[boxLine][boxRow] = new WBox("W", boxLine, boxRow, this); + resetNeighbourLists(); } public int getCost(VertexInterface start, VertexInterface end) { @@ -403,8 +437,8 @@ public final class Maze * @param filename * The path to the file containing the representation of the maze. */ - - private void initFromTextFile(String filename) { + private void initFromTextFile(String filename) + throws MazeReadingException { FileReader fr = null; BufferedReader br = null; @@ -474,16 +508,19 @@ public final class Maze currentLine = br.readLine(); compteurLigne++; } - } catch (MazeReadingException mre) { - mre.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException e) { + throw new MazeReadingException(filename); } finally { try { br.close(); } catch (Exception e) {} } } - public void saveToTextFile(String fileName) { + /** + * Save the current maze to a text file. + * @param fileName The name of the file in which the maze will be saved + */ + public void saveToTextFile(String fileName) + throws MazeSavingException { FileOutputStream fos = null; PrintWriter pw = null; @@ -499,9 +536,8 @@ public final class Maze } pw.print("\n"); } - - } catch(Exception e) { - e.printStackTrace(); + } catch(IOException e) { + throw new MazeSavingException(fileName); } finally { try { pw.close(); } catch(Exception e) {} } @@ -519,10 +555,12 @@ public final class Maze return mazeStr; } + /** Gives the number of rows in the maze */ public int getDimensionX() { return dimensionX; } + /** Gives the number of lines in the maze */ public int getDimensionY() { return dimensionY; } @@ -531,7 +569,7 @@ public final class Maze return maze; } - public void resetNeighbourLists() { + private void resetNeighbourLists() { for (Box[] line: maze) { for (Box box: line) { box.resetNeighbourList(); diff --git a/src/maze/MazeReadingException.java b/src/maze/MazeReadingException.java index 2e42204..0eecdd2 100644 --- a/src/maze/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -4,16 +4,13 @@ package maze; *

* This class is used to throw exception when reading and * initiating the maze from a text file. - * - * */ - public final class MazeReadingException extends Exception { private static final long serialVersionUID = 1L; - public MazeReadingException (String filename) { - super("Invalid maze format : " + filename); + public MazeReadingException(String filename) { + super("Invalid maze format : " + filename + " or can't open file."); } } diff --git a/src/maze/MazeSavingException.java b/src/maze/MazeSavingException.java new file mode 100644 index 0000000..c9a29ea --- /dev/null +++ b/src/maze/MazeSavingException.java @@ -0,0 +1,16 @@ +package maze; + +/**

MazeSavingException exception

+ *

+ * This class is used to throw exception when reading and + * initiating the maze from a text file. + */ +public final class MazeSavingException + extends Exception { + + private static final long serialVersionUID = 1L; + + public MazeSavingException(String filename) { + super("Can't save to file " + filename + "."); + } +} diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 846db8d..1b235b0 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -8,9 +8,14 @@ import java.util.Observable; import java.util.Observer; import dijkstra.*; -import maze.*; +import maze.Maze; +import maze.Box; +import maze.MazeReadingException; +import maze.MazeSavingException; -public class MazeAppModel extends Observable { +public class MazeAppModel + extends Observable + implements MazeModelInterface { /** The file into which the maze is to be saved */ private String filename; private Maze maze; @@ -43,7 +48,7 @@ public class MazeAppModel extends Observable { } else { previous = (Previous) Dijkstra.dijkstra(maze, departure); - if (((Box) arrival).getNeighbourList() == null) { + if (arrival.getNeighbourList() == null) { shortest = null; } else { @@ -52,9 +57,11 @@ public class MazeAppModel extends Observable { } } - public void loadMaze(String filename) { - this.filename = new String(filename); - maze = new Maze(this.filename); + /** Load a new maze from a file and send a notification to the view */ + public void loadMaze(String filename) + throws MazeReadingException { + maze = new Maze(filename); + this.filename = filename; initModelFromMaze(); saved = true; setChanged(); @@ -65,53 +72,61 @@ public class MazeAppModel extends Observable { return maze; } + /** Return the box at given coordinates */ public Box getBox(int boxLine, int boxRow) { return this.maze.getBox(boxLine, boxRow); } + /** Return the name of the file where the maze is currently being saved */ public String getFilename() { return filename; } + /** Tell whether the current maze has been saved or not */ public boolean isSaved() { return saved; } + /** Return the Dijkstra shortest path from departure to arrival */ public ArrayList getShortest() { return shortest; } - public ArrayList getShortestShallowCopy() { - ArrayList shallowCopy = new ArrayList(); - for (VertexInterface vertex : this.shortest) { - shallowCopy.add(vertex); - } - return shallowCopy; - } - + /** Indicate whether the path is drawn, currently being drawn, or neither */ public int isPathDrawn() { return pathDrawn; } + /** Set the state of the path drawing : + * 0 if not drawn + * 1 if being animated + * 2 if completely drawn + */ public void setPathDrawn(int pathDrawn) { this.pathDrawn = pathDrawn; } - public void saveToFile() { + /** + * Save the maze to the current file being used and notify observers + * (such as the "Save" button) + */ + public void saveToFile() throws MazeSavingException { maze.saveToTextFile(filename); saved = true; setChanged(); notifyObservers(MazeAppModelMessage.MazeSaved); } - public void saveToFile(String filename) { + /** Save the maze to the given file and set it to be the current file */ + public void saveToFile(String filename) throws MazeSavingException { + maze.saveToTextFile(filename); this.filename = filename; - maze.saveToTextFile(this.filename); saved = true; setChanged(); notifyObservers(MazeAppModelMessage.FileChange); } + /** Expose a way to toggle a box's type given its coordinates */ public void toggleBox(int boxLine, int boxRow) { if (maze.getBox(boxLine, boxRow).isWalkable()) { setWBox(boxLine, boxRow); @@ -121,52 +136,46 @@ public class MazeAppModel extends Observable { } } + /** Set the box at given coordinates to be a wall box */ public void setWBox(int boxLine, int boxRow) { maze.setWBox(boxLine, boxRow); - maze.resetNeighbourLists(); initModelFromMaze(); saved = false; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** Set the box at given coordinates to be an empty box */ public void setEBox(int boxLine, int boxRow) { maze.setEBox(boxLine, boxRow); - maze.resetNeighbourLists(); initModelFromMaze(); saved = false; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** Set the box at given coordinates to be a departure box */ public void setDBox(int boxLine, int boxRow) { - if (departure != null) { - int departureRow = ((EBox) departure).getX(); - int departureLine = ((EBox) departure).getY(); - maze.setEBox(departureLine, departureRow); - } maze.setDBox(boxLine, boxRow); - maze.resetNeighbourLists(); initModelFromMaze(); saved = false; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** Set the box at given coordinates to be an arrival box */ public void setABox(int boxLine, int boxRow) { - if (arrival != null) { - int arrivalRow = ((EBox) arrival).getX(); - int arrivalLine = ((EBox) arrival).getY(); - maze.setEBox(arrivalLine, arrivalRow); - } maze.setABox(boxLine, boxRow); - maze.resetNeighbourLists(); initModelFromMaze(); saved = false; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** + * Set the maze to be a new clear (full of empty boxes) maze of given + * dimensions + */ public void newClearMaze(int numberOfLines, int numberOfRows) { maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows)); @@ -176,10 +185,12 @@ public class MazeAppModel extends Observable { notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** Add an empty column at the end of the maze */ public void addEmptyColumn() { addEmptyColumn(maze.getDimensionX()); } + /** Add an empty column to the maze at the given position */ public void addEmptyColumn(int posColumn) { maze.addEmptyColumn(posColumn); initModelFromMaze(); @@ -188,6 +199,7 @@ public class MazeAppModel extends Observable { notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** Delete a column from the maze at the given position */ public void delColumn(int posColumn) { maze.delColumn(posColumn); initModelFromMaze(); @@ -196,14 +208,17 @@ public class MazeAppModel extends Observable { notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** Delete the last column of the maze */ public void delColumn() { delColumn(maze.getDimensionX() - 1); } + /** Add an empty line at the end of the maze */ public void addEmptyLine() { addEmptyLine(maze.getDimensionY()); } + /** Add an empty line at the given position */ public void addEmptyLine(int posLine) { maze.addEmptyLine(posLine); initModelFromMaze(); @@ -212,6 +227,7 @@ public class MazeAppModel extends Observable { notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** Delete a line from the maze at the given position */ public void delLine(int posLine) { maze.delLine(posLine); initModelFromMaze(); @@ -220,22 +236,30 @@ public class MazeAppModel extends Observable { notifyObservers(MazeAppModelMessage.MazeRenewal); } + /** Delete the last line of the maze */ public void delLine() { delLine(maze.getDimensionY() - 1); } + /** + * Get the departure box from the maze + * @return The departure box from the maze, if one exists, else null + */ public VertexInterface getDeparture() { return departure; } + /** Clear the maze : create a new one of the current dimensions */ public void clearMaze() { newClearMaze(maze.getDimensionY(), maze.getDimensionX()); } + /** Get the number of rows in the maze */ public int getDimensionX() { return maze.getDimensionX(); } + /** Get the number of lines in the maze */ public int getDimensionY() { return maze.getDimensionY(); } diff --git a/src/model/MazeModelInterface.java b/src/model/MazeModelInterface.java new file mode 100644 index 0000000..4d1144c --- /dev/null +++ b/src/model/MazeModelInterface.java @@ -0,0 +1,30 @@ +package model; + +import java.util.ArrayList; +import maze.Maze; +import maze.Box; +import maze.MazeReadingException; +import maze.MazeSavingException; +import dijkstra.VertexInterface; + +public interface MazeModelInterface { + public void loadMaze(String filename) throws MazeReadingException; + public void saveToFile(String filename) throws MazeSavingException; + public Maze getMaze(); + public Box getBox(int boxLine, int boxRow); + public String getFilename(); + public boolean isSaved(); + public ArrayList getShortest(); + public void toggleBox(int boxLine, int boxRow); + public void setABox(int boxLine, int boxRow); + public void setEBox(int boxLine, int boxRow); + public void setDBox(int boxLine, int boxRow); + public void setWBox(int boxLine, int boxRow); + public void clearMaze(); + public void addEmptyLine(int posLine); + public void addEmptyColumn(int posRow); + public void delLine(int posLine); + public void delColumn(int posRow); + public int getDimensionX(); + public int getDimensionY(); +} diff --git a/src/ui/FileBox.java b/src/ui/FileBox.java index 9ad443c..f43d511 100644 --- a/src/ui/FileBox.java +++ b/src/ui/FileBox.java @@ -13,8 +13,6 @@ public final class FileBox { int returnVal = fileChooser.showOpenDialog(mazeApp); if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to open this file: " + - fileChooser.getSelectedFile().getName()); String filename = fileChooser.getSelectedFile().getPath(); return filename; } @@ -26,8 +24,6 @@ public final class FileBox { int returnVal = fileChooser.showSaveDialog(mazeApp); if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to save to this file: " + - fileChooser.getSelectedFile().getName()); String filename = fileChooser.getSelectedFile().getPath(); return filename; } diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index 3a246a3..28f7d18 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -10,6 +10,8 @@ import java.util.Observable; import javax.swing.JOptionPane; import model.MazeAppModelMessage; +import maze.MazeReadingException; +import maze.MazeSavingException; public final class FileMenu extends JMenu { @@ -53,7 +55,17 @@ public final class FileMenu extends JMenu { } String filename = FileBox.load(mazeApp); if (filename != null) { - mazeApp.loadModelMaze(filename); + try { + mazeApp.loadModelMaze(filename); + } + catch (MazeReadingException mre) { + JOptionPane.showMessageDialog(this, + "Please check you have sufficient permissions to" + + " read the file and that the file has a correct" + + " maze format.", + "Error loading maze", + JOptionPane.ERROR_MESSAGE); + } } } } @@ -70,7 +82,7 @@ public final class FileMenu extends JMenu { this.mazeApp = mazeApp; this.setAccelerator(KeyStroke.getKeyStroke("control S")); mazeApp.getMazeAppModel().addObserver(this); - if (mazeApp.getMazeAppModel().getFilename() == null) { + if (mazeApp.getModelFilename() == null) { this.setEnabled(false); } addActionListener(this); @@ -78,7 +90,15 @@ public final class FileMenu extends JMenu { public void actionPerformed(ActionEvent evt) { if (mazeApp.getModelFilename() != null) { - mazeApp.saveModelMaze(); + try { + mazeApp.saveModelMaze(); + } + catch (MazeSavingException mse) { + JOptionPane.showMessageDialog(this, + mse.getMessage(), + "Maze saving error", + JOptionPane.ERROR_MESSAGE); + } } } @@ -111,7 +131,15 @@ public final class FileMenu extends JMenu { public void actionPerformed(ActionEvent evt) { String filename = FileBox.save(mazeApp); if (filename != null) { - mazeApp.saveModelMaze(filename); + try { + mazeApp.saveModelMaze(filename); + } + catch (MazeSavingException mse) { + JOptionPane.showMessageDialog(this, + mse.getMessage(), + "Maze saving error", + JOptionPane.ERROR_MESSAGE); + } } } } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 5e2b31b..fa9ae6f 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -6,6 +6,8 @@ import dijkstra.VertexInterface; import maze.Maze; import model.MazeAppModel; import maze.EBox; +import maze.MazeReadingException; +import maze.MazeSavingException; import java.util.ArrayList; import java.util.Observable; @@ -43,10 +45,13 @@ public final class MazeApp extends JFrame @Override public void update(Observable obs, Object param) { - System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } + /** + * Exit the application, asking if the user wants to save an unsaved + * maze or not + */ public void windowClosing(WindowEvent e) { if (! mazeAppModel.isSaved()) { if (! SaveBox.promptAndContinue(null, this, "Quit application")) { @@ -80,127 +85,155 @@ public final class MazeApp extends JFrame return; } + /** Draw the full path and update its state in the model */ public void drawShortestPath() { windowPanel.paintShortestPath(); mazeAppModel.setPathDrawn(2); } + /** + * Trigger the animation of path drawing and update its state in + * the model + */ public void animateShortestPath() { windowPanel.animateShortestPath(); mazeAppModel.setPathDrawn(1); } + /** Clear the shortest path drawn and update its state in the model */ public void clearShortestPath() { windowPanel.clearShortestPath(); mazeAppModel.setPathDrawn(0); this.repaint(); } + /** @see model.MazeAppModel */ public void addEmptyColumn(int posColumn) { mazeAppModel.addEmptyColumn(posColumn); } + /** @see model.MazeAppModel */ public void addEmptyColumn() { mazeAppModel.addEmptyColumn(); } + /** @see model.MazeAppModel */ public void delColumn(int posColumn) { mazeAppModel.delColumn(posColumn); } + /** @see model.MazeAppModel */ public void delColumn() { mazeAppModel.delColumn(); } + /** @see model.MazeAppModel */ public void addEmptyLine(int posLine) { mazeAppModel.addEmptyLine(posLine); } + /** @see model.MazeAppModel */ public void addEmptyLine() { mazeAppModel.addEmptyLine(); } + /** @see model.MazeAppModel */ public void delLine(int posLine) { mazeAppModel.delLine(posLine); } + /** @see model.MazeAppModel */ public void delLine() { mazeAppModel.delLine(); } + /** @see model.MazeAppModel */ public void clearMaze() { mazeAppModel.clearMaze(); this.repaint(); } + /** @see model.MazeAppModel */ public boolean isModelSaved( ) { return this.mazeAppModel.isSaved(); } - public void loadModelMaze(String filename) { + /** @see model.MazeAppModel */ + public void loadModelMaze(String filename) throws MazeReadingException { this.mazeAppModel.loadMaze(filename); } + /** @see model.MazeAppModel */ public maze.Box getModelBox(int boxLine, int boxRow) { return this.mazeAppModel.getBox(boxLine, boxRow); } - public void saveModelMaze(String filename) { + /** @see model.MazeAppModel */ + public void saveModelMaze(String filename) throws MazeSavingException { mazeAppModel.saveToFile(filename); } - public void saveModelMaze() { + /** @see model.MazeAppModel */ + public void saveModelMaze() throws MazeSavingException { mazeAppModel.saveToFile(); } + /** @see model.MazeAppModel */ public String getModelFilename() { return mazeAppModel.getFilename(); } + /** @see model.MazeAppModel */ public void setModelABox(int boxLine, int boxRow) { this.mazeAppModel.setABox(boxLine, boxRow); } + /** @see model.MazeAppModel */ public void setModelDBox(int boxLine, int boxRow) { this.mazeAppModel.setDBox(boxLine, boxRow); } + /** @see model.MazeAppModel */ public void setModelEBox(int boxLine, int boxRow) { this.mazeAppModel.setEBox(boxLine, boxRow); } + /** @see model.MazeAppModel */ public void setModelWBox(int boxLine, int boxRow) { this.mazeAppModel.setWBox(boxLine, boxRow); } + /** @see model.MazeAppModel */ public int isPathDrawn() { return this.mazeAppModel.isPathDrawn(); } + /** @see model.MazeAppModel */ public Maze getModelMaze() { return this.mazeAppModel.getMaze(); } + /** @see model.MazeAppModel */ public ArrayList getModelShortest() { return this.mazeAppModel.getShortest(); } - public ArrayList getModelShortestShallowCopy() { - return this.mazeAppModel.getShortestShallowCopy(); - } - + /** @see model.MazeAppModel */ public int getModelDimensionX() { return this.mazeAppModel.getDimensionX(); } + /** @see model.MazeAppModel */ public int getModelDimensionY() { return this.mazeAppModel.getDimensionY(); } + /** @see model.MazeAppModel */ public void setPathDrawn(int i) { this.mazeAppModel.setPathDrawn(i); } + /** @see model.MazeAppModel */ public void toggleModelBox(int boxLine, int boxRow) { this.mazeAppModel.toggleBox(boxLine, boxRow); } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 2437cbf..3fd2874 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -15,13 +15,12 @@ import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import maze.Maze; import model.MazeAppModelMessage; -import maze.EBox; +import maze.Box; import dijkstra.VertexInterface; public final class MazeDrawing extends JPanel implements MouseListener { private final MazeApp mazeApp; - private boolean pathDrawingMode; private ArrayList shortestPath; private ArrayList finalPath; private int pathCounter; @@ -29,7 +28,6 @@ public final class MazeDrawing extends JPanel implements MouseListener { public MazeDrawing(MazeApp mazeApp) { this.mazeApp = mazeApp; - this.pathDrawingMode = false; this.shortestPath = new ArrayList(); this.finalPath = null; this.pathCounter = -1; @@ -55,6 +53,7 @@ public final class MazeDrawing extends JPanel implements MouseListener { } } + /** Paint (each box of) the maze in the current JPanel */ private void paintMaze(Graphics2D g, int width, int height) { Maze maze = mazeApp.getModelMaze(); @@ -88,19 +87,31 @@ public final class MazeDrawing extends JPanel implements MouseListener { } } + /** + * This method is responsible for repainting the maze when needed + * @param param + * The message about what happened to the maze. + * The maze will be re-painted only if it changed + * (by user edition or by loading a new maze from file). + * @see model.MazeAppModelMessage + */ public void notifyForUpdates(Object param) { if (param == MazeAppModelMessage.MazeRenewal || param == MazeAppModelMessage.MazeLoaded) { if (this.mazeApp.getModelShortest() != null) { - this.finalPath - = this.mazeApp.getModelShortestShallowCopy(); + finalPath = new ArrayList(mazeApp.getModelShortest()); Collections.reverse(finalPath); } this.repaint(); } } + /** + * This method is reponsible for painting the shortest path on top + * of the maze. It takes all the boxes belonging to the shortest path + * and display on dot on them all at once. + */ private void paintShortestPath(Graphics2D g, int width, int height) { g.setColor(Color.WHITE); @@ -126,7 +137,7 @@ public final class MazeDrawing extends JPanel implements MouseListener { return; } for (VertexInterface vertex : shortest) { - EBox box = (EBox) vertex; + Box box = (Box) vertex; g.fillOval(box.getX() * boxWidth + factorWidth, box.getY()*boxHeight + factorHeight, boxWidth - 2 * factorWidth, @@ -134,6 +145,11 @@ public final class MazeDrawing extends JPanel implements MouseListener { } } + /** + * This method displays the next box in the shortest path at each call. + * When the last box in the shortest path has been displayed, it falls + * back to calling drawShortestPath. + */ private void animateShortestPath(Graphics2D g, int width, int height) { g.setColor(Color.WHITE); @@ -158,8 +174,6 @@ public final class MazeDrawing extends JPanel implements MouseListener { return; } - pathDrawingMode = true; - if (pathCounter == -1) { JOptionPane.showMessageDialog(this, "Click on the maze to see the shortest path evolve"); @@ -167,7 +181,7 @@ public final class MazeDrawing extends JPanel implements MouseListener { } for (VertexInterface vertex : shortestPath) { - EBox box = (EBox) vertex; + Box box = (Box) vertex; g.fillOval(box.getX() * boxWidth + factorWidth, box.getY() * boxHeight + factorHeight, boxWidth - 2 * factorWidth, @@ -175,15 +189,30 @@ public final class MazeDrawing extends JPanel implements MouseListener { } if (pathCounter >= finalPath.size()) { - pathDrawingMode = false; mazeApp.setPathDrawn(2); } } + /** + * The method called when the user clicked on the panel. + * It will trigger events such as box edition or shortest path animation. + * @param e + * If in animation mode, trigger the displaying of the next box + * in the shortest pas. If not, catch right clicks to display a context menu + * and left clicks to toggle boxes type. + */ public void mouseClicked(MouseEvent e) { int boxRow = getBoxRow(e.getX()); int boxLine = getBoxLine(e.getY()); - if (! pathDrawingMode) { + if (mazeApp.isPathDrawn() == 0) { + // Don't do anything if there is no box where the mouse is + if (boxRow >= mazeApp.getModelDimensionX() + || boxLine >= mazeApp.getModelDimensionY()) { + return; + } + // Toggle box's type if the user clicked the left button, + // print a menu allowing to choose between different actions + // if the user clicked the right button. if (e.getButton() == MouseEvent.BUTTON3) { BoxTypeEditionMenu boxTypeEditionMenu = new BoxTypeEditionMenu(mazeApp, boxLine, boxRow); @@ -213,12 +242,20 @@ public final class MazeDrawing extends JPanel implements MouseListener { return; } + /** + * Reponsible for calculating the row number of a box + * based on the panel size and the box's width + */ private int getBoxRow(int x) { int dimensionX = mazeApp.getModelDimensionX(); int boxWidth = getWidth() / dimensionX; return x / boxWidth; } + /** + * Reponsible for calculating the line number of a box + * based on the panel size and the box's height + */ private int getBoxLine(int y) { int dimensionY = mazeApp.getModelDimensionY(); int boxHeight = getHeight() / dimensionY; @@ -237,8 +274,8 @@ public final class MazeDrawing extends JPanel implements MouseListener { return; } + /** Clear the list of boxes in the local shortest path */ public void clearShortestPath() { this.pathCounter = -1; - this.shortestPath = new ArrayList(); } } diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java index f065d58..2ab9dcf 100644 --- a/src/ui/SaveBox.java +++ b/src/ui/SaveBox.java @@ -2,6 +2,7 @@ package ui; import javax.swing.JOptionPane; import java.awt.Component; +import maze.MazeSavingException; public final class SaveBox { private static String message = "Maze not saved. Save it ?"; @@ -20,12 +21,30 @@ public final class SaveBox { return false; case JOptionPane.OK_OPTION: if (mazeApp.getModelFilename() != null) { - mazeApp.saveModelMaze(); + try { + mazeApp.saveModelMaze(); + } + catch (MazeSavingException mse) { + JOptionPane.showMessageDialog(null, + mse.getMessage(), + "Maze saving error", + JOptionPane.ERROR_MESSAGE); + return promptAndContinue(null, mazeApp, title); + } } else { String filename = FileBox.save(mazeApp); if (filename != null) { - mazeApp.saveModelMaze(filename); + try { + mazeApp.saveModelMaze(filename); + } + catch (MazeSavingException mse) { + JOptionPane.showMessageDialog(null, + mse.getMessage(), + "Maze saving error", + JOptionPane.ERROR_MESSAGE); + return promptAndContinue(null, mazeApp, title); + } } else { return promptAndContinue(null, mazeApp, title); -- 2.56.1 From b1891f711811b3179f87b65d0692378da99d8b4f Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 14 Jan 2018 17:20:12 +0100 Subject: [PATCH 4/5] Add message on window opening * On lauch, explain the color of wall/empty boxes and that an example maze can be found in data/labyrinthe.txt * Remove legacy labyOutput.txt * Add a tiny comment to the class Dijkstra --- data/labyOutput.txt | 8 -------- src/dijkstra/Dijkstra.java | 1 + src/ui/MazeApp.java | 8 ++++++++ 3 files changed, 9 insertions(+), 8 deletions(-) delete mode 100644 data/labyOutput.txt diff --git a/data/labyOutput.txt b/data/labyOutput.txt deleted file mode 100644 index c27fe74..0000000 --- a/data/labyOutput.txt +++ /dev/null @@ -1,8 +0,0 @@ -WEEEEEWW -EDWWWEEW -WEEWWWEE -WWEEEEEW -EEEWWWWW -EWWWWAWW -EWWEEEWW -EEEEWWWW diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index 2437f1a..79fe238 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; +/** Litteral implementation of the Dijkstra's algorithm given in the subject */ public final class Dijkstra { private static PreviousInterface dijkstra(GraphInterface g, diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index fa9ae6f..cd555f3 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -82,6 +82,14 @@ public final class MazeApp extends JFrame } public void windowOpened(WindowEvent e) { + JOptionPane.showMessageDialog(this, + "Welcome to this maze solving application propelled by" + + " Dijkstra's algorithm.\n\n" + + "White boxes are walls, black boxes are empty and walkable.\n" + + "Feel free to edit boxes type by right clicking on them.\n\n" + + "You can find an example maze in data/labyrinthe.txt.", + "Welcome", + JOptionPane.INFORMATION_MESSAGE); return; } -- 2.56.1 From a664c399ffbfbcbe4e8565f18df4ad394611056f Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 14 Jan 2018 18:00:45 +0100 Subject: [PATCH 5/5] Fix when path is drawn or before being animated * Fix maze edition when path is drawn * Clear the path before animating it --- src/ui/MazeDrawing.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 3fd2874..16c1fd6 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -178,6 +178,7 @@ public final class MazeDrawing extends JPanel implements MouseListener { JOptionPane.showMessageDialog(this, "Click on the maze to see the shortest path evolve"); pathCounter = 0; + repaint(); } for (VertexInterface vertex : shortestPath) { @@ -204,7 +205,7 @@ public final class MazeDrawing extends JPanel implements MouseListener { public void mouseClicked(MouseEvent e) { int boxRow = getBoxRow(e.getX()); int boxLine = getBoxLine(e.getY()); - if (mazeApp.isPathDrawn() == 0) { + if (mazeApp.isPathDrawn() != 1) { // Don't do anything if there is no box where the mouse is if (boxRow >= mazeApp.getModelDimensionX() || boxLine >= mazeApp.getModelDimensionY()) { -- 2.56.1