diff --git a/.gitignore b/.gitignore index 449a8dd..29c9a7f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ src/package-list src/script.js *.class +/bin/ +*.classpath +*.project diff --git a/data/labyrinthe.txt b/data/labyrinthe.txt index 6e7752a..e4bd637 100644 --- a/data/labyrinthe.txt +++ b/data/labyrinthe.txt @@ -5,4 +5,5 @@ WWEEEEEW EEEWWWWW EWWWWAWW EWWEEEWW +EEEEWWWW EEEEWWWW \ No newline at end of file diff --git a/src/MainTest.java b/src/MainTest.java index b581dc1..5cb5c94 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,26 +1,8 @@ -import java.util.ArrayList; - -import dijkstra.*; -import maze.*; +import ui.*; public class MainTest { public static void main(String[] args) { - - Maze mazeTest = new Maze("data/labyrinthe.txt"); - - Previous previous = - (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]); - - ArrayList shortest = - previous.getShortestPathTo(mazeTest.getMaze()[5][5]); - - for (VertexInterface vertex : shortest) { - System.out.println(vertex); - } - - mazeTest.saveToTextFile("data/labyOutput.txt"); - + MazeApp app = new MazeApp(); } - } diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index 5368f02..d709f32 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -24,11 +24,12 @@ public class Dijkstra { } for (int j = 0; j < g.getVertexNumber(); 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); previous.setPrevious(pivot, vertex); + pivot.addCandidate(vertex); } } } diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 0a9c5e7..0b5c613 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -2,6 +2,9 @@ package dijkstra; import java.util.ArrayList; import java.util.Hashtable; +import java.util.List; + +import maze.Box; public class Previous implements PreviousInterface { @@ -27,14 +30,15 @@ public class Previous implements PreviousInterface { public ArrayList getShortestPathTo(VertexInterface vertex) { + shortestPath.add(vertex); VertexInterface father = getPrevious(vertex); - shortestPath.add(father); - + if (getPrevious(father) == null) { + shortestPath.add(father); return shortestPath; } else { return getShortestPathTo(father); - } + } } } diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index b020bd2..4061a20 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,4 +12,22 @@ 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(); + + /** + * Adds all vertexes not in the ASet and candidate for an eventual path. + * @param vertex + */ + public void addCandidate(VertexInterface vertex); + + /** + * Returns the list of the candidates for an eventual path. + * @return + */ + public ArrayList getCandidates(); } diff --git a/src/maze/ABox.java b/src/maze/ABox.java index 9a8b607..66b5a08 100644 --- a/src/maze/ABox.java +++ b/src/maze/ABox.java @@ -7,8 +7,8 @@ package maze; */ public class ABox extends EBox{ - public ABox (String label, int x, int y, Maze maze) { - super(label, x, y, maze); + public ABox (String label, int y, int x, Maze maze) { + super(label, y, x, maze) ; } @Override diff --git a/src/maze/Box.java b/src/maze/Box.java index 91a96a4..db1871f 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -23,13 +23,33 @@ public abstract class Box private int y; private String label; private Maze maze; + private ArrayList neighbourList; + private ArrayList candidates; - public Box (String label, int x, int y, Maze maze) { - - this.label = new String(label); + 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; + } + + public void resetNeighbourList() { + neighbourList = null; + } + + public void addCandidate(VertexInterface vertex) { + candidates.add(vertex); + } + + public ArrayList getCandidates() { + return candidates; } public boolean isWalkable() { @@ -43,6 +63,10 @@ public abstract class Box public boolean isDeparture() { return false; } + + public ArrayList getNeighbourList() { + return neighbourList; + } public int getX() { return x; @@ -59,5 +83,4 @@ public abstract class Box public String toString() { return "Label: " + label + ", x: " + x + ", y: " + y; } - } diff --git a/src/maze/DBox.java b/src/maze/DBox.java index ddc5a08..aacfc1d 100644 --- a/src/maze/DBox.java +++ b/src/maze/DBox.java @@ -7,8 +7,8 @@ package maze; public class DBox extends EBox{ - public DBox (String label, int x, int y, Maze maze) { - super(label, x, y, maze); + public DBox (String label, int y, int x, Maze maze) { + super(label, y, x, maze); } @Override diff --git a/src/maze/EBox.java b/src/maze/EBox.java index 1648dd8..8c50e0c 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 x, int y, Maze maze) { - super(label, x, y, 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 0faef52..a9b4246 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -26,12 +26,275 @@ public class Maze public Maze(String filename) { initFromTextFile(filename); } + + public Maze(Maze maze) { - // public Maze(Box[][] maze){ - // //Replacement for setMaze() - // //Todo : check if the given array is a proper maze - // //and then set dimensionX and dimensionX - // } + this.dimensionX = maze.getDimensionX(); + this.dimensionY = maze.getDimensionY(); + this.maze = matrixDeepCopy(maze.getMaze()); + } + + public Maze(int numberOfLines, int numberOfRows, Box[][] boxes) { + this.dimensionY = numberOfLines; + this.dimensionX = numberOfRows; + this.maze = matrixDeepCopy(boxes); + } + + public Maze(Box[][] newMaze) { + this.dimensionY = newMaze.length; + if (dimensionY > 0) { + this.dimensionX = newMaze[0].length; + } + else { + this.dimensionX = 0; + } + this.maze = matrixDeepCopy(newMaze); + } + + private Box[][] matrixDeepCopy(Box[][] original, + Box[][] destination, + int numberOfLines, + int numberOfRows, + int lineOffset, + int rowOffset) + { + if (destination == null) { + destination = new Box[numberOfLines][numberOfRows]; + } + if (original.length < 1) { + return destination; + } + for (int line = lineOffset; line < lineOffset + numberOfLines; line++) { + for (int row = rowOffset; row < rowOffset + numberOfRows; row++) { + Box box = original[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + destination[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); + } + else if (box.isArrival()) { + destination[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); + } + else { + destination[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); + } + } + else { + destination[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); + } + } + } + return destination; + } + + private Box[][] matrixDeepCopy(Box[][] original, + int numberOfLines, + int numberOfRows, + int lineOffset, + int rowOffset, + int numberOfDestLines, + int numberOfDestRows) { + return matrixDeepCopy(original, + new Box[numberOfDestLines][numberOfDestRows], + numberOfLines, + numberOfRows, + lineOffset, + rowOffset); + } + + private Box[][] matrixDeepCopy(Box [][] original, + int numberOfLines, + int numberOfRows, + int lineOffset, + int rowOffset) { + return matrixDeepCopy(original, + numberOfLines, + numberOfRows, + lineOffset, + rowOffset, + numberOfLines, + numberOfRows); + } + + private Box[][] matrixDeepCopy(Box[][] original, int numberOfLines, int numberOfRows) { + return matrixDeepCopy(original, numberOfLines, numberOfRows, 0, 0); + } + + private Box[][] matrixDeepCopy(Box[][] original) { + if (original.length < 1) { + return null; + } + return matrixDeepCopy(original, original.length, original[0].length); + } + + public static Maze emptyMaze(int dimension) { + Box[][] maze = new Box[dimension][dimension]; + for (int line = 0; line < dimension; line++) { + for (int row = 0; row < dimension; row++) { + maze[line][row] = new EBox("E", line, row, null); + } + } + return new Maze(dimension, dimension, maze); + } + + public static Maze emptyMaze(int numberOfLines, int numberOfRows) { + Box[][] maze = new Box[numberOfLines][numberOfRows]; + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + maze[line][row] = new EBox("E", line, row, null); + } + } + return new Maze(numberOfLines, numberOfRows, maze); + } + + public static Maze wallMaze(int dimension) { + Box[][] maze = new Box[dimension][dimension]; + for (int line = 0; line < dimension; line++) { + for (int row = 0; row < dimension; row++) { + maze[line][row] = new WBox("W", line, row, null); + } + } + return new Maze(dimension, dimension, maze); + } + + public static Maze wallMaze(int numberOfLines, int numberOfRows) { + Box[][] maze = new Box[numberOfLines][numberOfRows]; + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + maze[line][row] = new WBox("W", line, row, null); + } + } + return new Maze(numberOfLines, numberOfRows, maze); + } + + public void addEmptyColumn(int posColumn) { + Box[][] newMaze = matrixDeepCopy(maze, + dimensionY, + posColumn, + 0, + 0, + dimensionY, + dimensionX + 1); + for (int line = 0; line < dimensionY; line++) { + newMaze[line][posColumn] = new EBox("E", line, posColumn, this); + } + for (int line = 0; line < dimensionY; line++) { + for (int row = posColumn; row < dimensionX; row++) { + Box box = maze[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + newMaze[line][row + 1] = new DBox(box.getLabel(), box.getY(), box.getX() + 1, this); + } + else if (box.isArrival()) { + newMaze[line][row + 1] = new ABox(box.getLabel(), box.getY(), box.getX() + 1, this); + } + else { + newMaze[line][row + 1] = new EBox(box.getLabel(), box.getY(), box.getX() + 1, this); + } + } + else { + newMaze[line][row + 1] = new WBox(box.getLabel(), box.getY(), box.getX() + 1, this); + } + } + } + maze = newMaze; + dimensionX = dimensionX + 1; + } + + public void delColumn(int posColumn) { + Box[][] newMaze = matrixDeepCopy(maze, + dimensionY, + posColumn, + 0, + 0, + dimensionY, + dimensionX - 1); + for (int line = 0; line < dimensionY; line++) { + for (int row = posColumn + 1; row < dimensionX; row++) { + Box box = maze[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + newMaze[line][row - 1] = new DBox(box.getLabel(), box.getY(), box.getX() - 1, this); + } + else if (box.isArrival()) { + newMaze[line][row - 1] = new ABox(box.getLabel(), box.getY(), box.getX() - 1, this); + } + else { + newMaze[line][row - 1] = new EBox(box.getLabel(), box.getY(), box.getX() - 1, this); + } + } + else { + newMaze[line][row - 1] = new WBox(box.getLabel(), box.getY(), box.getX() - 1, this); + } + } + } + maze = newMaze; + dimensionX = dimensionX - 1; + } + + public void addEmptyLine(int posLine) { + Box[][] newMaze = matrixDeepCopy(maze, + posLine, + dimensionX, + 0, + 0, + dimensionY + 1, + dimensionX); + for (int row = 0; row < dimensionX; row++) { + newMaze[posLine][row] = new EBox("E", posLine, row, this); + } + for (int line = posLine; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + Box box = maze[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + newMaze[line + 1][row] = new DBox(box.getLabel(), box.getY() + 1, box.getX(), this); + } + else if (box.isArrival()) { + newMaze[line + 1][row] = new ABox(box.getLabel(), box.getY() + 1, box.getX(), this); + } + else { + newMaze[line + 1][row] = new EBox(box.getLabel(), box.getY() + 1, box.getX(), this); + } + } + else { + newMaze[line + 1][row] = new WBox(box.getLabel(), box.getY() + 1, box.getX(), this); + } + } + } + dimensionY = dimensionY + 1; + maze = newMaze; + } + + public void delLine(int posLine) { + Box[][] newMaze = matrixDeepCopy(maze, + posLine, + dimensionX, + 0, + 0, + dimensionY - 1, + dimensionX); + for (int line = posLine + 1; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + Box box = maze[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + newMaze[line - 1][row] = new DBox(box.getLabel(), box.getY() - 1, box.getX(), this); + } + else if (box.isArrival()) { + newMaze[line - 1][row] = new ABox(box.getLabel(), box.getY() - 1, box.getX(), this); + } + else { + newMaze[line - 1][row] = new EBox(box.getLabel(), box.getY() - 1, box.getX(), this); + } + } + else { + newMaze[line - 1][row] = new WBox(box.getLabel(), box.getY() - 1, box.getX(), this); + } + } + } + maze = newMaze; + dimensionY = dimensionY - 1; + } public int getCost(VertexInterface start, VertexInterface end) { @@ -47,18 +310,46 @@ public class Maze public ArrayList getVertexes() { ArrayList vertexList = new ArrayList(); - for (int i = 0; i < dimensionX; i++) { - for (int j = 0; j < dimensionY; j++) { - if (maze[i][j].isWalkable()) { - vertexList.add(maze[i][j]); + for (int line = 0 ; line < dimensionY; line++) { + for (int row = 0 ; row < dimensionX; row++) { + if (maze[line][row].isWalkable()) { + vertexList.add(maze[line][row]); } } } return vertexList; } - public ArrayList generateNeighbours(VertexInterface vertex) { + /**Method to determine the departure in the maze. Returns null if the departure is non-existent. + * @return Departure vertex + */ + public VertexInterface getDeparture() { + ArrayList vertexes = getVertexes(); + for (VertexInterface vertex: vertexes) { + if (((Box) vertex).isDeparture()) { + return vertex; + } + } + return null; + } + + /**Method to determine the arrival in the maze. Returns null if the arrival is non-existent. + * @return Arrival vertex + */ + public VertexInterface getArrival() { + + ArrayList vertexes = getVertexes(); + for (VertexInterface vertex: vertexes) { + if (((Box) vertex).isArrival()) { + return vertex; + } + } + return null; + } + + public ArrayList generateNeighbours(VertexInterface vertex) { + Box box = (Box) vertex; int x = box.getX(); int y = box.getY(); @@ -69,28 +360,27 @@ public class Maze return neighbourList; } - if (x > 0 && maze[x-1][y].isWalkable()) { - neighbourList.add(maze[x-1][y]); + if (x > 0 && maze[y][x-1].isWalkable()) { + neighbourList.add(maze[y][x-1]); } - - if (x < dimensionX-1 && maze[x+1][y].isWalkable()) { - neighbourList.add(maze[x+1][y]); + + if (x < dimensionX-1 && maze[y][x+1].isWalkable()) { + neighbourList.add(maze[y][x+1]); } - - if (y > 0 && maze[x][y-1].isWalkable()) { - neighbourList.add(maze[x][y-1]); + + if (y > 0 && maze[y-1][x].isWalkable()) { + neighbourList.add(maze[y-1][x]); } - - if (y < dimensionY-1 && maze[x][y+1].isWalkable()) { - neighbourList.add(maze[x][y+1]); + + 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. - * @param filename + /**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. */ @@ -112,16 +402,16 @@ 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; + + this.maze = new Box[dimensionY][dimensionX] ; + int compteurLigne = 0; - - this.maze = new Box[dimensionX][dimensionY]; - while (currentLine != null) { currentLineLength = currentLine.length(); @@ -133,25 +423,22 @@ public class Maze char currentChar = currentLine.charAt(compteurColonne); switch(currentChar) { - + case 'W': - maze[compteurColonne][compteurLigne] = - new WBox("W", compteurColonne, compteurLigne, this); + maze[compteurLigne][compteurColonne] = + new WBox("W", compteurLigne, compteurColonne, this) ; break; - case 'E': - maze[compteurColonne][compteurLigne] = - new EBox("E", compteurColonne, compteurLigne, this); + maze[compteurLigne][compteurColonne] = + new EBox("E", compteurLigne, compteurColonne, this) ; break; - case 'D': - maze[compteurColonne][compteurLigne] = - new DBox("D", compteurColonne, compteurLigne, this); + maze[compteurLigne][compteurColonne] = + new DBox("D", compteurLigne, compteurColonne, this) ; break; - case 'A': - maze[compteurColonne][compteurLigne] = - new ABox("A", compteurColonne, compteurLigne, this); + maze[compteurLigne][compteurColonne] = + new ABox("A", compteurLigne, compteurColonne, this) ; break; default: @@ -174,7 +461,6 @@ public class Maze } finally { try { br.close(); } catch (Exception e) {} } - } public final void saveToTextFile(String fileName) { @@ -187,9 +473,9 @@ public class Maze fos = new FileOutputStream(fileName); pw = new PrintWriter(fos); - for (int i = 0; i < dimensionY; i++) { - for (int j = 0; j < dimensionX; j++) { - pw.print(maze[j][i].getLabel()); + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + pw.print(maze[line][row].getLabel()); } pw.print("\n"); } @@ -201,6 +487,18 @@ public class Maze } } + public String toString() { + + String mazeStr = ""; + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + mazeStr += maze[line][row].getLabel(); + } + mazeStr += "\n"; + } + return mazeStr; + } + public int getDimensionX() { return dimensionX; } @@ -212,4 +510,12 @@ public class Maze public Box[][] getMaze() { return maze; } + + public void resetNeighbourLists() { + for (Box[] line: maze) { + for (Box box: line) { + box.resetNeighbourList(); + } + } + } } diff --git a/src/maze/WBox.java b/src/maze/WBox.java index 8174c5d..cb17318 100644 --- a/src/maze/WBox.java +++ b/src/maze/WBox.java @@ -7,8 +7,8 @@ package maze; public class WBox extends Box { - public WBox (String label, int x, int y, Maze maze) { - super(label, x, y, maze); + public WBox (String label, int y, int x, Maze maze) { + super(label, y, x, maze); } @Override diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java new file mode 100644 index 0000000..e4bf63d --- /dev/null +++ b/src/model/MazeAppModel.java @@ -0,0 +1,214 @@ +package model; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.util.ArrayList; +import java.util.Observable; +import java.util.Observer; + +import dijkstra.*; +import maze.*; + +public class MazeAppModel extends Observable { + + 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 : + * 0 : path isn't drawn + * 1 : path has been animated + * 2 : path has already been animated, but needs to be re-drawn + * because of window resizing. Therefore there is no need to + * animate the path again + */ + private int pathDrawn = 0; + + public MazeAppModel() { + maze = new Maze(Maze.emptyMaze(10)); + initModelFromMaze(); + } + + private void initModelFromMaze() { + departure = maze.getDeparture(); + arrival = maze.getArrival(); + boxList = maze.getVertexes(); + if (departure == null || arrival == null) { + previous = null; + shortest = null; + } + else { + previous = (Previous) Dijkstra.dijkstra(maze, departure); + if (((Box) arrival).getNeighbourList() == null) { + shortest = null; + } + else { + shortest = previous.getShortestPathTo(arrival); + } + } + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); + } + + public void loadMaze(String filename) { + this.filename = new String(filename); + maze = new Maze(this.filename); + initModelFromMaze(); + saved = true; + } + + public Maze getMaze() { + return maze; + } + + public String getFilename() { + return filename; + } + + public boolean isSaved() { + return saved; + } + + public ArrayList getShortest() { + return shortest; + } + + public ArrayList getShortestDeepCopy() { + ArrayList deepCopy = new ArrayList(); + for (VertexInterface vertex : this.shortest) { + deepCopy.add(vertex); + } + return deepCopy; + } + + public int isPathDrawn() { + return pathDrawn; + } + + public void setPathDrawn(int pathDrawn) { + this.pathDrawn = pathDrawn; + } + + public void saveToFile() { + maze.saveToTextFile(filename); + saved = true; + } + + public void saveToFile(String filename) { + this.filename = new String(filename); + maze.saveToTextFile(this.filename); + saved = true; + setChanged(); + notifyObservers(MazeAppModelMessage.FileChange); + } + + public void toggleBox(int boxLine, int boxRow) { + if (maze.getMaze()[boxLine][boxRow].isWalkable()) { + setWBox(boxLine, boxRow); + } + else { + setEBox(boxLine, boxRow); + } + } + + public void setWBox(int boxLine, int boxRow) { + maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze); + maze.resetNeighbourLists(); + initModelFromMaze(); + saved = false; + } + + public void setEBox(int boxLine, int boxRow) { + maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze); + maze.resetNeighbourLists(); + initModelFromMaze(); + saved = false; + } + + public void setDBox(int boxLine, int boxRow) { + if (departure != null) { + int departureRow = ((EBox) departure).getX(); + int departureLine = ((EBox) departure).getY(); + maze.getMaze()[departureLine][departureRow] = + new EBox("E", departureLine, departureRow, maze); + } + maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze); + maze.resetNeighbourLists(); + initModelFromMaze(); + saved = false; + } + + public void setABox(int boxLine, int boxRow) { + if (arrival != null) { + int arrivalRow = ((EBox) arrival).getX(); + int arrivalLine = ((EBox) arrival).getY(); + maze.getMaze()[arrivalLine][arrivalRow] = + new EBox("E", arrivalLine, arrivalRow, maze); + } + maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze); + maze.resetNeighbourLists(); + initModelFromMaze(); + saved = false; + } + + public void newClearMaze(int numberOfLines, int numberOfRows) { + + maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows)); + initModelFromMaze(); + saved = false; + } + + public void addEmptyColumn() { + addEmptyColumn(maze.getDimensionX()); + } + + public void addEmptyColumn(int posColumn) { + maze.addEmptyColumn(posColumn); + initModelFromMaze(); + saved = false; + } + + public void delColumn(int posColumn) { + maze.delColumn(posColumn); + initModelFromMaze(); + saved = false; + } + + public void delColumn() { + delColumn(maze.getDimensionX() - 1); + } + + public void addEmptyLine() { + addEmptyLine(maze.getDimensionY()); + } + + public void addEmptyLine(int posLine) { + maze.addEmptyLine(posLine); + initModelFromMaze(); + saved = false; + } + + public void delLine(int posLine) { + maze.delLine(posLine); + initModelFromMaze(); + saved = false; + } + + public void delLine() { + delLine(maze.getDimensionY() - 1); + } + + public VertexInterface getDeparture() { + return departure; + } + + public void clearMaze() { + newClearMaze(maze.getDimensionY(), maze.getDimensionX()); + } +} diff --git a/src/model/MazeAppModelMessage.java b/src/model/MazeAppModelMessage.java new file mode 100644 index 0000000..af9cf63 --- /dev/null +++ b/src/model/MazeAppModelMessage.java @@ -0,0 +1,6 @@ +package model; + +public class MazeAppModelMessage { + public final static String MazeRenewal = "MazeRenewal"; + public final static String FileChange = "FileChange"; +} diff --git a/src/ui/AddLineMenuItem.java b/src/ui/AddLineMenuItem.java new file mode 100644 index 0000000..095760d --- /dev/null +++ b/src/ui/AddLineMenuItem.java @@ -0,0 +1,38 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class AddLineMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int lineNum; + + public AddLineMenuItem(MazeApp mazeApp) { + + super("Add line"); + this.mazeApp = mazeApp; + lineNum = -1; + addActionListener(this); + } + + public AddLineMenuItem(MazeApp mazeApp, int lineNum) { + super("Add line"); + this.mazeApp = mazeApp; + this.lineNum = lineNum; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (lineNum == -1) { + mazeApp.addEmptyLine(); + } + else { + mazeApp.addEmptyLine(lineNum); + } + } +} diff --git a/src/ui/AddRowMenuItem.java b/src/ui/AddRowMenuItem.java new file mode 100644 index 0000000..b49dfd7 --- /dev/null +++ b/src/ui/AddRowMenuItem.java @@ -0,0 +1,39 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class AddRowMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int rowNum; + + public AddRowMenuItem(MazeApp mazeApp) { + + super("Add row"); + this.mazeApp = mazeApp; + rowNum = -1; + addActionListener(this); + } + + public AddRowMenuItem(MazeApp mazeApp, int rowNum) { + + super("Add row"); + this.mazeApp = mazeApp; + this.rowNum = rowNum; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (rowNum == -1) { + mazeApp.addEmptyColumn(); + } + else { + mazeApp.addEmptyColumn(rowNum); + } + } +} diff --git a/src/ui/AnimatePathMenuItem.java b/src/ui/AnimatePathMenuItem.java new file mode 100644 index 0000000..f1f2f08 --- /dev/null +++ b/src/ui/AnimatePathMenuItem.java @@ -0,0 +1,27 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.Action; +import javax.swing.Icon; +import javax.swing.JMenuItem; + +public class AnimatePathMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public AnimatePathMenuItem(MazeApp mazeApp) { + + super("Animate shortest path"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent arg0) { + + mazeApp.animateShortestPath(); + } +} diff --git a/src/ui/BoxTypeEditionMenu.java b/src/ui/BoxTypeEditionMenu.java new file mode 100644 index 0000000..3f07272 --- /dev/null +++ b/src/ui/BoxTypeEditionMenu.java @@ -0,0 +1,34 @@ +package ui; + +import javax.swing.JPopupMenu; +import javax.swing.JMenuItem; +import maze.Box; + +public class BoxTypeEditionMenu extends JPopupMenu { + JMenuItem setWBox; + JMenuItem setEBox; + JMenuItem setDBox; + JMenuItem setABox; + JMenuItem addRow; + JMenuItem addLine; + JMenuItem delRow; + 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]; + setEBox.setEnabled((box.isWalkable() && (box.isDeparture() || + box.isArrival())) || ! box.isWalkable()); + setWBox.setEnabled(box.isWalkable()); + setDBox.setEnabled(! box.isDeparture()); + setABox.setEnabled(! box.isArrival()); + + add(addRow = new AddRowMenuItem(mazeApp, boxRow)); + add(addLine = new AddLineMenuItem(mazeApp, boxLine)); + add(delRow = new DelRowMenuItem(mazeApp, boxRow)); + add(delLine = new DelLineMenuItem(mazeApp, boxLine)); + } +} diff --git a/src/ui/ClearMazeMenuItem.java b/src/ui/ClearMazeMenuItem.java new file mode 100644 index 0000000..aeed823 --- /dev/null +++ b/src/ui/ClearMazeMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; +import javax.swing.KeyStroke; + +public class ClearMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public ClearMazeMenuItem(MazeApp mazeApp) { + + super("Clear maze"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control D")); + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + mazeApp.clearMaze(); + } +} 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/DelLineMenuItem.java b/src/ui/DelLineMenuItem.java new file mode 100644 index 0000000..165ac6e --- /dev/null +++ b/src/ui/DelLineMenuItem.java @@ -0,0 +1,38 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class DelLineMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int lineNum; + + public DelLineMenuItem(MazeApp mazeApp) { + + super("Delete line"); + this.mazeApp = mazeApp; + lineNum = -1; + addActionListener(this); + } + + public DelLineMenuItem(MazeApp mazeApp, int lineNum) { + super("Delete line"); + this.mazeApp = mazeApp; + this.lineNum = lineNum; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (lineNum == -1) { + mazeApp.delLine(); + } + else { + mazeApp.delLine(lineNum); + } + } +} diff --git a/src/ui/DelRowMenuItem.java b/src/ui/DelRowMenuItem.java new file mode 100644 index 0000000..243b129 --- /dev/null +++ b/src/ui/DelRowMenuItem.java @@ -0,0 +1,37 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class DelRowMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int rowNum; + + public DelRowMenuItem(MazeApp mazeApp) { + super("Delete row"); + this.mazeApp = mazeApp; + rowNum = -1; + addActionListener(this); + } + + public DelRowMenuItem(MazeApp mazeApp, int rowNum) { + super("Delete row"); + this.mazeApp = mazeApp; + this.rowNum = rowNum; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (rowNum == -1) { + mazeApp.delColumn(); + } + else { + mazeApp.delColumn(rowNum); + } + } +} 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/FileBox.java b/src/ui/FileBox.java new file mode 100644 index 0000000..fc651c2 --- /dev/null +++ b/src/ui/FileBox.java @@ -0,0 +1,36 @@ +package ui; + +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileNameExtensionFilter; + +public class FileBox { + private static JFileChooser fileChooser = new JFileChooser(); + private static FileNameExtensionFilter filter = + new FileNameExtensionFilter("Text files", "txt"); + + public static String load(MazeApp mazeApp) { + fileChooser.setFileFilter(filter); + + 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; + } + return null; + } + + public static String save(MazeApp mazeApp) { + fileChooser.setFileFilter(filter); + + 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; + } + return null; + } +} diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java new file mode 100644 index 0000000..baadb8e --- /dev/null +++ b/src/ui/FileMenu.java @@ -0,0 +1,23 @@ +package ui; + +import javax.swing.JMenu; + +public class FileMenu extends JMenu { + + private final MazeApp mazeApp; + private final LoadMazeMenuItem loadMazeMenuItem; + private final SaveMazeMenuItem saveMazeMenuItem; + private final SaveMazeAsMenuItem saveMazeAsMenuItem; + private final QuitMenuItem quitMenuItem; + + public FileMenu(MazeApp mazeApp) { + + super("File"); + this.mazeApp = mazeApp; + + add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp)); + add(saveMazeMenuItem = new SaveMazeMenuItem(mazeApp)); + add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp)); + add(quitMenuItem = new QuitMenuItem(mazeApp)); + } +} diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java new file mode 100644 index 0000000..05cf17e --- /dev/null +++ b/src/ui/LoadMazeMenuItem.java @@ -0,0 +1,36 @@ +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) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ + return; + } + } + String filename = FileBox.load(mazeApp); + if (filename != null) { + mazeApp.getMazeAppModel().loadMaze(filename); + } + } +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java new file mode 100644 index 0000000..bbfaee6 --- /dev/null +++ b/src/ui/MazeApp.java @@ -0,0 +1,134 @@ +package ui; + +import javax.swing.*; +import maze.Maze; +import model.MazeAppModel; +import maze.EBox; + +import java.util.Observable; +import java.util.Observer; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; + +public class MazeApp extends JFrame + implements Observer, WindowListener { + + private final MazeMenuBar mazeMenuBar; + private final WindowPanel windowPanel; + private MazeAppModel mazeAppModel; + + public MazeApp() { + + super("Maze Solving Application"); + + this.mazeAppModel = new MazeAppModel(); + mazeAppModel.addObserver(this); + + addWindowListener(this); + setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); + setContentPane(windowPanel = new WindowPanel(this)); + + setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + + pack(); + setVisible(true); + } + + public MazeAppModel getMazeAppModel() { + return mazeAppModel; + } + + @Override + public void update(Observable obs, Object param) { + + System.out.println("New maze detected"); + windowPanel.notifyForUpdates(param); + } + + public void windowClosing(WindowEvent e) { + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(null, this, "Quit application")) { + return; + } + } + System.exit(0); + } + + public void windowDeactivated(WindowEvent e) { + return; + } + + public void windowActivated(WindowEvent e) { + return; + } + + public void windowDeiconified(WindowEvent e) { + return; + } + + public void windowIconified(WindowEvent e) { + return; + } + + public void windowClosed(WindowEvent e) { + return; + } + + public void windowOpened(WindowEvent e) { + return; + } + + public void drawShortestPath() { + windowPanel.paintShortestPath(); + mazeAppModel.setPathDrawn(2); + } + + public void clearShortestPath() { + windowPanel.clearShortestPath(); + mazeAppModel.setPathDrawn(0); + this.repaint(); + } + + public void addEmptyColumn(int posColumn) { + mazeAppModel.addEmptyColumn(posColumn); + } + + public void addEmptyColumn() { + mazeAppModel.addEmptyColumn(); + } + + public void delColumn(int posColumn) { + mazeAppModel.delColumn(posColumn); + } + + public void delColumn() { + mazeAppModel.delColumn(); + } + + public void addEmptyLine(int posLine) { + mazeAppModel.addEmptyLine(posLine); + } + + public void addEmptyLine() { + mazeAppModel.addEmptyLine(); + } + + public void delLine(int posLine) { + mazeAppModel.delLine(posLine); + } + + public void delLine() { + mazeAppModel.delLine(); + } + + public void clearMaze() { + mazeAppModel.clearMaze(); + this.repaint(); + } + + public void animateShortestPath() { + + windowPanel.animateShortestPath(); + mazeAppModel.setPathDrawn(1); + } +} diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java new file mode 100644 index 0000000..aea6f9e --- /dev/null +++ b/src/ui/MazeDrawing.java @@ -0,0 +1,261 @@ +package ui; + +import java.util.ArrayList; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseListener; +import java.util.Collections; +import javax.swing.JPanel; +import javax.swing.JOptionPane; +import java.awt.event.MouseListener; +import java.awt.event.MouseEvent; +import maze.Maze; +import model.MazeAppModelMessage; +import maze.EBox; +import dijkstra.VertexInterface; + +public class MazeDrawing extends JPanel implements MouseListener { + + private final MazeApp mazeApp; + private boolean pathDrawingMode; + private ArrayList shortestPath; + private ArrayList finalPath; + private int pathCounter; + + public MazeDrawing(MazeApp mazeApp) { + + this.mazeApp = mazeApp; + this.pathDrawingMode = false; + this.shortestPath = new ArrayList(); + this.finalPath = null; + this.pathCounter = -1; + setBackground(Color.BLACK); + setPreferredSize(new Dimension(500,500)); + addMouseListener(this); + } + + @Override + protected final void paintComponent(Graphics g) { + + Graphics2D g2d = (Graphics2D) g; + super.paintComponent(g2d); + + int width = getWidth(); + int height = getHeight(); + + paintMaze(g2d, width, height); + if (mazeApp.getMazeAppModel().isPathDrawn() == 1) { + animateShortestPath(g2d, width, height); + } else if (mazeApp.getMazeAppModel().isPathDrawn() == 2) { + paintShortestPath(g2d, width, height); + } + } + + private void paintMaze(Graphics2D g, int width, int height) { + Maze maze = mazeApp.getMazeAppModel().getMaze(); + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + int boxWidth = width / dimensionX; + int boxHeight = height / dimensionY; + + 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()) { + g.setColor(Color.GREEN); + } + else if (maze.getMaze()[line][row].isArrival()) { + g.setColor(Color.RED); + } + else { + g.setColor(Color.BLACK); + } + } + else { + g.setColor(Color.WHITE); + } + g.fill3DRect(row * boxWidth, + line * boxHeight, + boxWidth, + boxHeight, + true); + } + } + } + + public void notifyForUpdates(Object param) { + + if (param == MazeAppModelMessage.MazeRenewal) { + if (this.mazeApp.getMazeAppModel().getShortest() != null) { + this.finalPath + = this.mazeApp.getMazeAppModel().getShortestDeepCopy(); + Collections.reverse(finalPath); + } + this.repaint(); + } + } + + 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 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.clearShortestPath(); + JOptionPane.showMessageDialog(this, + "Check that your graph contains a departure and an arrival," + + " and that a path exists between them.", + "No shortest path", + JOptionPane.ERROR_MESSAGE); + return; + } + else if (shortest == null) { + mazeApp.clearShortestPath(); + return; + } + for (VertexInterface vertex : shortest) { + EBox box = (EBox) vertex; + g.fillOval(box.getX() * boxWidth + factorWidth, + box.getY()*boxHeight + factorHeight, + boxWidth - 2 * factorWidth, + boxHeight - 2 * factorHeight); + } + } + + 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 boxWidth = width / dimensionX; + int boxHeight = height / dimensionY; + int factorWidth = boxWidth / 3; + int factorHeight = boxHeight / 3; + + if (this.finalPath == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { + mazeApp.clearShortestPath(); + JOptionPane.showMessageDialog(this, + "Check that your graph contains a departure and an arrival," + + " and that a path exists between them.", + "No shortest path", + JOptionPane.ERROR_MESSAGE); + return; + } + else if (this.finalPath == null) { + mazeApp.clearShortestPath(); + return; + } + + pathDrawingMode = true; + + if (pathCounter == -1) { + JOptionPane.showMessageDialog(this, + "Click on the maze to see the shortest path evolve"); + pathCounter = 0; + } + + for (VertexInterface vertex : shortestPath) { + EBox box = (EBox) vertex; + g.fillOval(box.getX() * boxWidth + factorWidth, + box.getY() * boxHeight + factorHeight, + 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; + mazeApp.getMazeAppModel().setPathDrawn(2); + } + } + + public void mouseClicked(MouseEvent e) { + int boxRow = getBoxRow(e.getX()); + int boxLine = getBoxLine(e.getY()); + if (! pathDrawingMode) { + if (e.getButton() == MouseEvent.BUTTON3) { + BoxTypeEditionMenu boxTypeEditionMenu + = new BoxTypeEditionMenu(mazeApp, boxLine, boxRow); + boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY()); + } + else if (e.getButton() == MouseEvent.BUTTON1) { + mazeApp.getMazeAppModel().toggleBox(boxLine, boxRow); + } + } else { + shortestPath.add(this.finalPath.get(pathCounter)); + repaint(); + pathCounter += 1; + return; + } + return; + } + + public void paintShortestPath() { + paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); + } + + public void animateShortestPath() { + animateShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); + } + + public void mousePressed(MouseEvent e) { + return; + } + + private int getBoxRow(int x) { + int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); + int boxWidth = getWidth() / dimensionX; + return x / boxWidth; + } + + private int getBoxLine(int y) { + int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); + int boxHeight = getHeight() / dimensionY; + return y / boxHeight; + } + + public void mouseReleased(MouseEvent e) { + return; + } + + public void mouseExited(MouseEvent e) { + return; + } + + public void mouseEntered(MouseEvent e) { + return; + } + + public void clearShortestPath() { + this.pathCounter = -1; + this.shortestPath = new ArrayList(); + } +} diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java new file mode 100644 index 0000000..e3840c1 --- /dev/null +++ b/src/ui/MazeMenu.java @@ -0,0 +1,33 @@ +package ui; + +import javax.swing.JMenu; + +public class MazeMenu extends JMenu { + + private final MazeApp mazeApp; + private final DrawShortestPathMenuItem drawMenuItem; + private final ClearShortestPathMenuItem clearMenuItem; + private final NewClearMazeMenuItem newClearMazeMenuItem; + private final ClearMazeMenuItem clearMazeMenuItem; + private final AddRowMenuItem addRowMenuItem; + private final AddLineMenuItem addLineMenuItem; + private final DelRowMenuItem delRowMenuItem; + private final DelLineMenuItem delLineMenuItem; + private final AnimatePathMenuItem animatePathMenuItem; + + public MazeMenu(MazeApp mazeApp) { + + super("Maze"); + this.mazeApp = mazeApp; + + add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp)); + add(animatePathMenuItem = new AnimatePathMenuItem(mazeApp)); + add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp)); + add(newClearMazeMenuItem = new NewClearMazeMenuItem(mazeApp)); + add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp)); + add(addRowMenuItem = new AddRowMenuItem(mazeApp)); + add(addLineMenuItem = new AddLineMenuItem(mazeApp)); + add(delRowMenuItem = new DelRowMenuItem(mazeApp)); + add(delLineMenuItem = new DelLineMenuItem(mazeApp)); + } +} diff --git a/src/ui/MazeMenuBar.java b/src/ui/MazeMenuBar.java new file mode 100644 index 0000000..afe76da --- /dev/null +++ b/src/ui/MazeMenuBar.java @@ -0,0 +1,19 @@ +package ui; + +import javax.swing.JMenuBar; + +public class MazeMenuBar extends JMenuBar { + + private final MazeApp mazeApp; + private final FileMenu fileMenu; + private final MazeMenu mazeMenu; + + public MazeMenuBar(MazeApp mazeApp) { + + super(); + this.mazeApp = mazeApp; + + add(fileMenu = new FileMenu(mazeApp)); + add(mazeMenu = new MazeMenu(mazeApp)); + } +} diff --git a/src/ui/NewClearMazeMenuItem.java b/src/ui/NewClearMazeMenuItem.java new file mode 100644 index 0000000..19cc68a --- /dev/null +++ b/src/ui/NewClearMazeMenuItem.java @@ -0,0 +1,30 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.KeyStroke; + +public class NewClearMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private NewMazeSpinnerBox spinnerBox; + + public NewClearMazeMenuItem(MazeApp mazeApp) { + + super("New clear maze"); + this.mazeApp = mazeApp; + this.spinnerBox = new NewMazeSpinnerBox(mazeApp); + this.setAccelerator(KeyStroke.getKeyStroke("control N")); + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent arg0) { + + spinnerBox.createAndShowGUI(); + } +} diff --git a/src/ui/NewMazeSpinnerBox.java b/src/ui/NewMazeSpinnerBox.java new file mode 100644 index 0000000..2e5397c --- /dev/null +++ b/src/ui/NewMazeSpinnerBox.java @@ -0,0 +1,89 @@ +package ui; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Container; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.SpinnerModel; +import javax.swing.SpinnerNumberModel; +import javax.swing.SpringLayout; + +public class NewMazeSpinnerBox extends JPanel + implements ActionListener { + + private final MazeApp mazeApp; + private JDialog spinnerDialog; + private SpinnerModel lineSpinnerModel; + private SpinnerModel rowSpinnerModel; + + public NewMazeSpinnerBox(MazeApp mazeApp) { + + this.mazeApp = mazeApp; + JPanel spinnerDialog = new JPanel(new SpringLayout()); + String[] labels = {"Number of lines :", "Number of rows :"}; + int len = labels.length; + int initialValue = 3; + int maximumValue = 200; + + this.lineSpinnerModel = + new SpinnerNumberModel(initialValue, //initial value + initialValue, //min + maximumValue, //max + 1); + JSpinner lineSpinner = addLabeledSpinner(this, labels[0], lineSpinnerModel); + + this.rowSpinnerModel = + new SpinnerNumberModel(initialValue, //initial value + initialValue, //min + maximumValue, //max + 1); + JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel); + } + + static protected JSpinner addLabeledSpinner(Container c, + String label, + SpinnerModel model) { + + JLabel l = new JLabel(label); + c.add(l); + + JSpinner spinner = new JSpinner(model); + l.setLabelFor(spinner); + c.add(spinner); + + return spinner; + } + + public void createAndShowGUI() { + + spinnerDialog = new JDialog(spinnerDialog, "Create a new empty maze"); + spinnerDialog.setLayout(new BorderLayout()); + spinnerDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + + spinnerDialog.add(this, + BorderLayout.CENTER); + + JButton OKButton = new JButton("OK"); + OKButton.addActionListener(this); + spinnerDialog.add(OKButton, BorderLayout.SOUTH); + + spinnerDialog.pack(); + spinnerDialog.setVisible(true); + } + + @Override + public void actionPerformed(ActionEvent e) { + mazeApp.getMazeAppModel( + ).newClearMaze((int) lineSpinnerModel.getValue(), + (int) rowSpinnerModel.getValue()); + spinnerDialog.dispose(); + } +} diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java new file mode 100644 index 0000000..c8632ce --- /dev/null +++ b/src/ui/QuitMenuItem.java @@ -0,0 +1,36 @@ +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) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { + return; + } + } + System.exit(0); + } +} diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java new file mode 100644 index 0000000..ae354f2 --- /dev/null +++ b/src/ui/SaveBox.java @@ -0,0 +1,43 @@ +package ui; + +import javax.swing.JOptionPane; +import java.awt.Component; + +import model.MazeAppModel; + +public 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, + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.WARNING_MESSAGE, + null, null, null); + switch (response) { + case JOptionPane.CANCEL_OPTION: + return false; + case JOptionPane.OK_OPTION: + if (mazeAppModel.getFilename() != null) { + mazeAppModel.saveToFile(); + } + else { + String filename = FileBox.save(mazeApp); + if (filename != null) { + mazeApp.getMazeAppModel().saveToFile(filename); + } + else { + return promptAndContinue(null, mazeApp, title); + } + } + break; + case JOptionPane.NO_OPTION: + break; + } + return true; + } +} diff --git a/src/ui/SaveMazeAsMenuItem.java b/src/ui/SaveMazeAsMenuItem.java new file mode 100644 index 0000000..22195f1 --- /dev/null +++ b/src/ui/SaveMazeAsMenuItem.java @@ -0,0 +1,32 @@ +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 new file mode 100644 index 0000000..13b8c8e --- /dev/null +++ b/src/ui/SaveMazeMenuItem.java @@ -0,0 +1,47 @@ +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 new file mode 100644 index 0000000..2449a0c --- /dev/null +++ b/src/ui/SetABoxMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class SetABoxMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int boxLine; + private final int boxRow; + + public SetABoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) { + super("Set Arrival Box"); + this.mazeApp = mazeApp; + this.boxLine = boxLine; + this.boxRow = boxRow; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.getMazeAppModel().setABox(boxLine, boxRow); + } +} diff --git a/src/ui/SetDBoxMenuItem.java b/src/ui/SetDBoxMenuItem.java new file mode 100644 index 0000000..fd9c5fd --- /dev/null +++ b/src/ui/SetDBoxMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class SetDBoxMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int boxLine; + private final int boxRow; + + public SetDBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) { + super("Set Departure Box"); + this.mazeApp = mazeApp; + this.boxLine = boxLine; + this.boxRow = boxRow; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.getMazeAppModel().setDBox(boxLine, boxRow); + } +} diff --git a/src/ui/SetEBoxMenuItem.java b/src/ui/SetEBoxMenuItem.java new file mode 100644 index 0000000..85a41fd --- /dev/null +++ b/src/ui/SetEBoxMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class SetEBoxMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int boxLine; + private final int boxRow; + + public SetEBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) { + super("Set Empty Box"); + this.mazeApp = mazeApp; + this.boxLine = boxLine; + this.boxRow = boxRow; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.getMazeAppModel().setEBox(boxLine, boxRow); + } +} diff --git a/src/ui/SetWBoxMenuItem.java b/src/ui/SetWBoxMenuItem.java new file mode 100644 index 0000000..3cc032b --- /dev/null +++ b/src/ui/SetWBoxMenuItem.java @@ -0,0 +1,26 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class SetWBoxMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int boxLine; + private final int boxRow; + + public SetWBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) { + super("Set Wall Box"); + this.mazeApp = mazeApp; + this.boxLine = boxLine; + this.boxRow = boxRow; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.getMazeAppModel().setWBox(boxLine, boxRow); + } +} diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java new file mode 100644 index 0000000..4219cda --- /dev/null +++ b/src/ui/WindowPanel.java @@ -0,0 +1,34 @@ +package ui; + +import javax.swing.*; +import maze.Maze; +import java.awt.*; + +public class WindowPanel extends JPanel { + + private final MazeDrawing drawingPanelGrid; + + public WindowPanel(MazeApp mazeApp) { + + setLayout(new BorderLayout()); + + add(drawingPanelGrid = new MazeDrawing(mazeApp), BorderLayout.CENTER); + } + + public void notifyForUpdates(Object param) { + drawingPanelGrid.notifyForUpdates(param); + } + + public void paintShortestPath() { + drawingPanelGrid.paintShortestPath(); + } + + public void clearShortestPath() { + drawingPanelGrid.clearShortestPath(); + } + + public void animateShortestPath() { + + drawingPanelGrid.animateShortestPath(); + } +}