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
This commit is contained in:
1 parent
e76db727c7
commit
dc8de8c707
11 files changed
+294
-73
No files matched your search
@@ -19,6 +19,9 @@ public interface VertexInterface {
|
|||||||
*/
|
*/
|
||||||
public ArrayList<VertexInterface> generateNeighbours();
|
public ArrayList<VertexInterface> generateNeighbours();
|
||||||
|
|
||||||
|
/** Returns the neighbourList of the element. */
|
||||||
|
public ArrayList<VertexInterface> getNeighbourList();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds all vertexes not in the ASet and candidate for an eventual path.
|
* Adds all vertexes not in the ASet and candidate for an eventual path.
|
||||||
* @param vertex
|
* @param vertex
|
||||||
|
|||||||
+50
-12
@@ -3,6 +3,7 @@ import java.io.BufferedReader;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import dijkstra.GraphInterface;
|
import dijkstra.GraphInterface;
|
||||||
@@ -23,7 +24,8 @@ public final class Maze
|
|||||||
private int dimensionY;
|
private int dimensionY;
|
||||||
private Box[][] maze;
|
private Box[][] maze;
|
||||||
|
|
||||||
public Maze(String filename) {
|
public Maze(String filename)
|
||||||
|
throws MazeReadingException {
|
||||||
initFromTextFile(filename);
|
initFromTextFile(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +53,12 @@ public final class Maze
|
|||||||
this.maze = matrixDeepCopy(newMaze);
|
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,
|
private Box[][] matrixDeepCopy(Box[][] original,
|
||||||
Box[][] destination,
|
Box[][] destination,
|
||||||
int numberOfLines,
|
int numberOfLines,
|
||||||
@@ -126,6 +134,7 @@ public final class Maze
|
|||||||
return matrixDeepCopy(original, original.length, original[0].length);
|
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) {
|
public static Maze emptyMaze(int dimension) {
|
||||||
Box[][] maze = new Box[dimension][dimension];
|
Box[][] maze = new Box[dimension][dimension];
|
||||||
for (int line = 0; line < dimension; line++) {
|
for (int line = 0; line < dimension; line++) {
|
||||||
@@ -136,6 +145,7 @@ public final class Maze
|
|||||||
return new Maze(dimension, dimension, 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) {
|
public static Maze emptyMaze(int numberOfLines, int numberOfRows) {
|
||||||
Box[][] maze = new Box[numberOfLines][numberOfRows];
|
Box[][] maze = new Box[numberOfLines][numberOfRows];
|
||||||
for (int line = 0; line < numberOfLines; line++) {
|
for (int line = 0; line < numberOfLines; line++) {
|
||||||
@@ -146,6 +156,7 @@ public final class Maze
|
|||||||
return new Maze(numberOfLines, numberOfRows, maze);
|
return new Maze(numberOfLines, numberOfRows, maze);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Create a square maze of wall boxes of the given dimension */
|
||||||
public static Maze wallMaze(int dimension) {
|
public static Maze wallMaze(int dimension) {
|
||||||
Box[][] maze = new Box[dimension][dimension];
|
Box[][] maze = new Box[dimension][dimension];
|
||||||
for (int line = 0; line < dimension; line++) {
|
for (int line = 0; line < dimension; line++) {
|
||||||
@@ -156,6 +167,7 @@ public final class Maze
|
|||||||
return new Maze(dimension, dimension, 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) {
|
public static Maze wallMaze(int numberOfLines, int numberOfRows) {
|
||||||
Box[][] maze = new Box[numberOfLines][numberOfRows];
|
Box[][] maze = new Box[numberOfLines][numberOfRows];
|
||||||
for (int line = 0; line < numberOfLines; line++) {
|
for (int line = 0; line < numberOfLines; line++) {
|
||||||
@@ -166,6 +178,7 @@ public final class Maze
|
|||||||
return new Maze(numberOfLines, numberOfRows, maze);
|
return new Maze(numberOfLines, numberOfRows, maze);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Add a column of empty boxes at the given position */
|
||||||
public void addEmptyColumn(int posColumn) {
|
public void addEmptyColumn(int posColumn) {
|
||||||
Box[][] newMaze = matrixDeepCopy(maze,
|
Box[][] newMaze = matrixDeepCopy(maze,
|
||||||
dimensionY,
|
dimensionY,
|
||||||
@@ -296,24 +309,45 @@ public final class Maze
|
|||||||
dimensionY = dimensionY - 1;
|
dimensionY = dimensionY - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return the box at given coordinates */
|
||||||
public Box getBox(int boxLine, int boxRow) {
|
public Box getBox(int boxLine, int boxRow) {
|
||||||
return this.maze[boxLine][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) {
|
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);
|
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) {
|
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);
|
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) {
|
public void setEBox(int boxLine, int boxRow) {
|
||||||
this.maze[boxLine][boxRow] = new EBox("E", boxLine, boxRow, this);
|
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) {
|
public void setWBox(int boxLine, int boxRow) {
|
||||||
this.maze[boxLine][boxRow] = new WBox("W", boxLine, boxRow, this);
|
this.maze[boxLine][boxRow] = new WBox("W", boxLine, boxRow, this);
|
||||||
|
resetNeighbourLists();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCost(VertexInterface start, VertexInterface end) {
|
public int getCost(VertexInterface start, VertexInterface end) {
|
||||||
@@ -403,8 +437,8 @@ public final class Maze
|
|||||||
* @param filename
|
* @param filename
|
||||||
* The path to the file containing the representation of the maze.
|
* 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;
|
FileReader fr = null;
|
||||||
BufferedReader br = null;
|
BufferedReader br = null;
|
||||||
@@ -474,16 +508,19 @@ public final class Maze
|
|||||||
currentLine = br.readLine();
|
currentLine = br.readLine();
|
||||||
compteurLigne++;
|
compteurLigne++;
|
||||||
}
|
}
|
||||||
} catch (MazeReadingException mre) {
|
} catch (IOException e) {
|
||||||
mre.printStackTrace();
|
throw new MazeReadingException(filename);
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
} finally {
|
||||||
try { br.close(); } catch (Exception e) {}
|
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;
|
FileOutputStream fos = null;
|
||||||
PrintWriter pw = null;
|
PrintWriter pw = null;
|
||||||
@@ -499,9 +536,8 @@ public final class Maze
|
|||||||
}
|
}
|
||||||
pw.print("\n");
|
pw.print("\n");
|
||||||
}
|
}
|
||||||
|
} catch(IOException e) {
|
||||||
} catch(Exception e) {
|
throw new MazeSavingException(fileName);
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
} finally {
|
||||||
try { pw.close(); } catch(Exception e) {}
|
try { pw.close(); } catch(Exception e) {}
|
||||||
}
|
}
|
||||||
@@ -519,10 +555,12 @@ public final class Maze
|
|||||||
return mazeStr;
|
return mazeStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gives the number of rows in the maze */
|
||||||
public int getDimensionX() {
|
public int getDimensionX() {
|
||||||
return dimensionX;
|
return dimensionX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gives the number of lines in the maze */
|
||||||
public int getDimensionY() {
|
public int getDimensionY() {
|
||||||
return dimensionY;
|
return dimensionY;
|
||||||
}
|
}
|
||||||
@@ -531,7 +569,7 @@ public final class Maze
|
|||||||
return maze;
|
return maze;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetNeighbourLists() {
|
private void resetNeighbourLists() {
|
||||||
for (Box[] line: maze) {
|
for (Box[] line: maze) {
|
||||||
for (Box box: line) {
|
for (Box box: line) {
|
||||||
box.resetNeighbourList();
|
box.resetNeighbourList();
|
||||||
|
|||||||
@@ -4,16 +4,13 @@ package maze;
|
|||||||
* <p>
|
* <p>
|
||||||
* This class is used to throw exception when reading and
|
* This class is used to throw exception when reading and
|
||||||
* initiating the maze from a text file.
|
* initiating the maze from a text file.
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public final class MazeReadingException
|
public final class MazeReadingException
|
||||||
extends Exception {
|
extends Exception {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public MazeReadingException (String filename) {
|
public MazeReadingException(String filename) {
|
||||||
super("Invalid maze format : " + filename);
|
super("Invalid maze format : " + filename + " or can't open file.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package maze;
|
||||||
|
|
||||||
|
/**<h1> MazeSavingException exception </h1>
|
||||||
|
* <p>
|
||||||
|
* 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 + ".");
|
||||||
|
}
|
||||||
|
}
|
||||||
+55
-31
@@ -8,9 +8,14 @@ import java.util.Observable;
|
|||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
|
|
||||||
import dijkstra.*;
|
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 */
|
/** The file into which the maze is to be saved */
|
||||||
private String filename;
|
private String filename;
|
||||||
private Maze maze;
|
private Maze maze;
|
||||||
@@ -43,7 +48,7 @@ public class MazeAppModel extends Observable {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
||||||
if (((Box) arrival).getNeighbourList() == null) {
|
if (arrival.getNeighbourList() == null) {
|
||||||
shortest = null;
|
shortest = null;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -52,9 +57,11 @@ public class MazeAppModel extends Observable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadMaze(String filename) {
|
/** Load a new maze from a file and send a notification to the view */
|
||||||
this.filename = new String(filename);
|
public void loadMaze(String filename)
|
||||||
maze = new Maze(this.filename);
|
throws MazeReadingException {
|
||||||
|
maze = new Maze(filename);
|
||||||
|
this.filename = filename;
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = true;
|
saved = true;
|
||||||
setChanged();
|
setChanged();
|
||||||
@@ -65,53 +72,61 @@ public class MazeAppModel extends Observable {
|
|||||||
return maze;
|
return maze;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return the box at given coordinates */
|
||||||
public Box getBox(int boxLine, int boxRow) {
|
public Box getBox(int boxLine, int boxRow) {
|
||||||
return this.maze.getBox(boxLine, boxRow);
|
return this.maze.getBox(boxLine, boxRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return the name of the file where the maze is currently being saved */
|
||||||
public String getFilename() {
|
public String getFilename() {
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Tell whether the current maze has been saved or not */
|
||||||
public boolean isSaved() {
|
public boolean isSaved() {
|
||||||
return saved;
|
return saved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return the Dijkstra shortest path from departure to arrival */
|
||||||
public ArrayList<VertexInterface> getShortest() {
|
public ArrayList<VertexInterface> getShortest() {
|
||||||
return shortest;
|
return shortest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<VertexInterface> getShortestShallowCopy() {
|
/** Indicate whether the path is drawn, currently being drawn, or neither */
|
||||||
ArrayList<VertexInterface> shallowCopy = new ArrayList<VertexInterface>();
|
|
||||||
for (VertexInterface vertex : this.shortest) {
|
|
||||||
shallowCopy.add(vertex);
|
|
||||||
}
|
|
||||||
return shallowCopy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int isPathDrawn() {
|
public int isPathDrawn() {
|
||||||
return pathDrawn;
|
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) {
|
public void setPathDrawn(int pathDrawn) {
|
||||||
this.pathDrawn = 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);
|
maze.saveToTextFile(filename);
|
||||||
saved = true;
|
saved = true;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.MazeSaved);
|
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;
|
this.filename = filename;
|
||||||
maze.saveToTextFile(this.filename);
|
|
||||||
saved = true;
|
saved = true;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.FileChange);
|
notifyObservers(MazeAppModelMessage.FileChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Expose a way to toggle a box's type given its coordinates */
|
||||||
public void toggleBox(int boxLine, int boxRow) {
|
public void toggleBox(int boxLine, int boxRow) {
|
||||||
if (maze.getBox(boxLine, boxRow).isWalkable()) {
|
if (maze.getBox(boxLine, boxRow).isWalkable()) {
|
||||||
setWBox(boxLine, boxRow);
|
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) {
|
public void setWBox(int boxLine, int boxRow) {
|
||||||
maze.setWBox(boxLine, boxRow);
|
maze.setWBox(boxLine, boxRow);
|
||||||
maze.resetNeighbourLists();
|
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = false;
|
saved = false;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Set the box at given coordinates to be an empty box */
|
||||||
public void setEBox(int boxLine, int boxRow) {
|
public void setEBox(int boxLine, int boxRow) {
|
||||||
maze.setEBox(boxLine, boxRow);
|
maze.setEBox(boxLine, boxRow);
|
||||||
maze.resetNeighbourLists();
|
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = false;
|
saved = false;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Set the box at given coordinates to be a departure box */
|
||||||
public void setDBox(int boxLine, int boxRow) {
|
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.setDBox(boxLine, boxRow);
|
||||||
maze.resetNeighbourLists();
|
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = false;
|
saved = false;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Set the box at given coordinates to be an arrival box */
|
||||||
public void setABox(int boxLine, int boxRow) {
|
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.setABox(boxLine, boxRow);
|
||||||
maze.resetNeighbourLists();
|
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
saved = false;
|
saved = false;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
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) {
|
public void newClearMaze(int numberOfLines, int numberOfRows) {
|
||||||
|
|
||||||
maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows));
|
maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows));
|
||||||
@@ -176,10 +185,12 @@ public class MazeAppModel extends Observable {
|
|||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Add an empty column at the end of the maze */
|
||||||
public void addEmptyColumn() {
|
public void addEmptyColumn() {
|
||||||
addEmptyColumn(maze.getDimensionX());
|
addEmptyColumn(maze.getDimensionX());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Add an empty column to the maze at the given position */
|
||||||
public void addEmptyColumn(int posColumn) {
|
public void addEmptyColumn(int posColumn) {
|
||||||
maze.addEmptyColumn(posColumn);
|
maze.addEmptyColumn(posColumn);
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
@@ -188,6 +199,7 @@ public class MazeAppModel extends Observable {
|
|||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Delete a column from the maze at the given position */
|
||||||
public void delColumn(int posColumn) {
|
public void delColumn(int posColumn) {
|
||||||
maze.delColumn(posColumn);
|
maze.delColumn(posColumn);
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
@@ -196,14 +208,17 @@ public class MazeAppModel extends Observable {
|
|||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Delete the last column of the maze */
|
||||||
public void delColumn() {
|
public void delColumn() {
|
||||||
delColumn(maze.getDimensionX() - 1);
|
delColumn(maze.getDimensionX() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Add an empty line at the end of the maze */
|
||||||
public void addEmptyLine() {
|
public void addEmptyLine() {
|
||||||
addEmptyLine(maze.getDimensionY());
|
addEmptyLine(maze.getDimensionY());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Add an empty line at the given position */
|
||||||
public void addEmptyLine(int posLine) {
|
public void addEmptyLine(int posLine) {
|
||||||
maze.addEmptyLine(posLine);
|
maze.addEmptyLine(posLine);
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
@@ -212,6 +227,7 @@ public class MazeAppModel extends Observable {
|
|||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Delete a line from the maze at the given position */
|
||||||
public void delLine(int posLine) {
|
public void delLine(int posLine) {
|
||||||
maze.delLine(posLine);
|
maze.delLine(posLine);
|
||||||
initModelFromMaze();
|
initModelFromMaze();
|
||||||
@@ -220,22 +236,30 @@ public class MazeAppModel extends Observable {
|
|||||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Delete the last line of the maze */
|
||||||
public void delLine() {
|
public void delLine() {
|
||||||
delLine(maze.getDimensionY() - 1);
|
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() {
|
public VertexInterface getDeparture() {
|
||||||
return departure;
|
return departure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Clear the maze : create a new one of the current dimensions */
|
||||||
public void clearMaze() {
|
public void clearMaze() {
|
||||||
newClearMaze(maze.getDimensionY(), maze.getDimensionX());
|
newClearMaze(maze.getDimensionY(), maze.getDimensionX());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get the number of rows in the maze */
|
||||||
public int getDimensionX() {
|
public int getDimensionX() {
|
||||||
return maze.getDimensionX();
|
return maze.getDimensionX();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get the number of lines in the maze */
|
||||||
public int getDimensionY() {
|
public int getDimensionY() {
|
||||||
return maze.getDimensionY();
|
return maze.getDimensionY();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<VertexInterface> 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();
|
||||||
|
}
|
||||||
@@ -13,8 +13,6 @@ public final class FileBox {
|
|||||||
|
|
||||||
int returnVal = fileChooser.showOpenDialog(mazeApp);
|
int returnVal = fileChooser.showOpenDialog(mazeApp);
|
||||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
System.out.println("You chose to open this file: " +
|
|
||||||
fileChooser.getSelectedFile().getName());
|
|
||||||
String filename = fileChooser.getSelectedFile().getPath();
|
String filename = fileChooser.getSelectedFile().getPath();
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
@@ -26,8 +24,6 @@ public final class FileBox {
|
|||||||
|
|
||||||
int returnVal = fileChooser.showSaveDialog(mazeApp);
|
int returnVal = fileChooser.showSaveDialog(mazeApp);
|
||||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
System.out.println("You chose to save to this file: " +
|
|
||||||
fileChooser.getSelectedFile().getName());
|
|
||||||
String filename = fileChooser.getSelectedFile().getPath();
|
String filename = fileChooser.getSelectedFile().getPath();
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-1
@@ -10,6 +10,8 @@ import java.util.Observable;
|
|||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
import model.MazeAppModelMessage;
|
import model.MazeAppModelMessage;
|
||||||
|
import maze.MazeReadingException;
|
||||||
|
import maze.MazeSavingException;
|
||||||
|
|
||||||
public final class FileMenu extends JMenu {
|
public final class FileMenu extends JMenu {
|
||||||
|
|
||||||
@@ -53,8 +55,18 @@ public final class FileMenu extends JMenu {
|
|||||||
}
|
}
|
||||||
String filename = FileBox.load(mazeApp);
|
String filename = FileBox.load(mazeApp);
|
||||||
if (filename != null) {
|
if (filename != null) {
|
||||||
|
try {
|
||||||
mazeApp.loadModelMaze(filename);
|
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.mazeApp = mazeApp;
|
||||||
this.setAccelerator(KeyStroke.getKeyStroke("control S"));
|
this.setAccelerator(KeyStroke.getKeyStroke("control S"));
|
||||||
mazeApp.getMazeAppModel().addObserver(this);
|
mazeApp.getMazeAppModel().addObserver(this);
|
||||||
if (mazeApp.getMazeAppModel().getFilename() == null) {
|
if (mazeApp.getModelFilename() == null) {
|
||||||
this.setEnabled(false);
|
this.setEnabled(false);
|
||||||
}
|
}
|
||||||
addActionListener(this);
|
addActionListener(this);
|
||||||
@@ -78,8 +90,16 @@ public final class FileMenu extends JMenu {
|
|||||||
|
|
||||||
public void actionPerformed(ActionEvent evt) {
|
public void actionPerformed(ActionEvent evt) {
|
||||||
if (mazeApp.getModelFilename() != null) {
|
if (mazeApp.getModelFilename() != null) {
|
||||||
|
try {
|
||||||
mazeApp.saveModelMaze();
|
mazeApp.saveModelMaze();
|
||||||
}
|
}
|
||||||
|
catch (MazeSavingException mse) {
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
mse.getMessage(),
|
||||||
|
"Maze saving error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -111,8 +131,16 @@ public final class FileMenu extends JMenu {
|
|||||||
public void actionPerformed(ActionEvent evt) {
|
public void actionPerformed(ActionEvent evt) {
|
||||||
String filename = FileBox.save(mazeApp);
|
String filename = FileBox.save(mazeApp);
|
||||||
if (filename != null) {
|
if (filename != null) {
|
||||||
|
try {
|
||||||
mazeApp.saveModelMaze(filename);
|
mazeApp.saveModelMaze(filename);
|
||||||
}
|
}
|
||||||
|
catch (MazeSavingException mse) {
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
mse.getMessage(),
|
||||||
|
"Maze saving error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+41
-8
@@ -6,6 +6,8 @@ import dijkstra.VertexInterface;
|
|||||||
import maze.Maze;
|
import maze.Maze;
|
||||||
import model.MazeAppModel;
|
import model.MazeAppModel;
|
||||||
import maze.EBox;
|
import maze.EBox;
|
||||||
|
import maze.MazeReadingException;
|
||||||
|
import maze.MazeSavingException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Observable;
|
import java.util.Observable;
|
||||||
@@ -43,10 +45,13 @@ public final class MazeApp extends JFrame
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Observable obs, Object param) {
|
public void update(Observable obs, Object param) {
|
||||||
System.out.println("New maze detected");
|
|
||||||
windowPanel.notifyForUpdates(param);
|
windowPanel.notifyForUpdates(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exit the application, asking if the user wants to save an unsaved
|
||||||
|
* maze or not
|
||||||
|
*/
|
||||||
public void windowClosing(WindowEvent e) {
|
public void windowClosing(WindowEvent e) {
|
||||||
if (! mazeAppModel.isSaved()) {
|
if (! mazeAppModel.isSaved()) {
|
||||||
if (! SaveBox.promptAndContinue(null, this, "Quit application")) {
|
if (! SaveBox.promptAndContinue(null, this, "Quit application")) {
|
||||||
@@ -80,127 +85,155 @@ public final class MazeApp extends JFrame
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Draw the full path and update its state in the model */
|
||||||
public void drawShortestPath() {
|
public void drawShortestPath() {
|
||||||
windowPanel.paintShortestPath();
|
windowPanel.paintShortestPath();
|
||||||
mazeAppModel.setPathDrawn(2);
|
mazeAppModel.setPathDrawn(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger the animation of path drawing and update its state in
|
||||||
|
* the model
|
||||||
|
*/
|
||||||
public void animateShortestPath() {
|
public void animateShortestPath() {
|
||||||
windowPanel.animateShortestPath();
|
windowPanel.animateShortestPath();
|
||||||
mazeAppModel.setPathDrawn(1);
|
mazeAppModel.setPathDrawn(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Clear the shortest path drawn and update its state in the model */
|
||||||
public void clearShortestPath() {
|
public void clearShortestPath() {
|
||||||
windowPanel.clearShortestPath();
|
windowPanel.clearShortestPath();
|
||||||
mazeAppModel.setPathDrawn(0);
|
mazeAppModel.setPathDrawn(0);
|
||||||
this.repaint();
|
this.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void addEmptyColumn(int posColumn) {
|
public void addEmptyColumn(int posColumn) {
|
||||||
mazeAppModel.addEmptyColumn(posColumn);
|
mazeAppModel.addEmptyColumn(posColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void addEmptyColumn() {
|
public void addEmptyColumn() {
|
||||||
mazeAppModel.addEmptyColumn();
|
mazeAppModel.addEmptyColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void delColumn(int posColumn) {
|
public void delColumn(int posColumn) {
|
||||||
mazeAppModel.delColumn(posColumn);
|
mazeAppModel.delColumn(posColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void delColumn() {
|
public void delColumn() {
|
||||||
mazeAppModel.delColumn();
|
mazeAppModel.delColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void addEmptyLine(int posLine) {
|
public void addEmptyLine(int posLine) {
|
||||||
mazeAppModel.addEmptyLine(posLine);
|
mazeAppModel.addEmptyLine(posLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void addEmptyLine() {
|
public void addEmptyLine() {
|
||||||
mazeAppModel.addEmptyLine();
|
mazeAppModel.addEmptyLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void delLine(int posLine) {
|
public void delLine(int posLine) {
|
||||||
mazeAppModel.delLine(posLine);
|
mazeAppModel.delLine(posLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void delLine() {
|
public void delLine() {
|
||||||
mazeAppModel.delLine();
|
mazeAppModel.delLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void clearMaze() {
|
public void clearMaze() {
|
||||||
mazeAppModel.clearMaze();
|
mazeAppModel.clearMaze();
|
||||||
this.repaint();
|
this.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public boolean isModelSaved( ) {
|
public boolean isModelSaved( ) {
|
||||||
return this.mazeAppModel.isSaved();
|
return this.mazeAppModel.isSaved();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadModelMaze(String filename) {
|
/** @see model.MazeAppModel */
|
||||||
|
public void loadModelMaze(String filename) throws MazeReadingException {
|
||||||
this.mazeAppModel.loadMaze(filename);
|
this.mazeAppModel.loadMaze(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public maze.Box getModelBox(int boxLine, int boxRow) {
|
public maze.Box getModelBox(int boxLine, int boxRow) {
|
||||||
return this.mazeAppModel.getBox(boxLine, 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);
|
mazeAppModel.saveToFile(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveModelMaze() {
|
/** @see model.MazeAppModel */
|
||||||
|
public void saveModelMaze() throws MazeSavingException {
|
||||||
mazeAppModel.saveToFile();
|
mazeAppModel.saveToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public String getModelFilename() {
|
public String getModelFilename() {
|
||||||
return mazeAppModel.getFilename();
|
return mazeAppModel.getFilename();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void setModelABox(int boxLine, int boxRow) {
|
public void setModelABox(int boxLine, int boxRow) {
|
||||||
this.mazeAppModel.setABox(boxLine, boxRow);
|
this.mazeAppModel.setABox(boxLine, boxRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void setModelDBox(int boxLine, int boxRow) {
|
public void setModelDBox(int boxLine, int boxRow) {
|
||||||
this.mazeAppModel.setDBox(boxLine, boxRow);
|
this.mazeAppModel.setDBox(boxLine, boxRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void setModelEBox(int boxLine, int boxRow) {
|
public void setModelEBox(int boxLine, int boxRow) {
|
||||||
this.mazeAppModel.setEBox(boxLine, boxRow);
|
this.mazeAppModel.setEBox(boxLine, boxRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void setModelWBox(int boxLine, int boxRow) {
|
public void setModelWBox(int boxLine, int boxRow) {
|
||||||
this.mazeAppModel.setWBox(boxLine, boxRow);
|
this.mazeAppModel.setWBox(boxLine, boxRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public int isPathDrawn() {
|
public int isPathDrawn() {
|
||||||
return this.mazeAppModel.isPathDrawn();
|
return this.mazeAppModel.isPathDrawn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public Maze getModelMaze() {
|
public Maze getModelMaze() {
|
||||||
return this.mazeAppModel.getMaze();
|
return this.mazeAppModel.getMaze();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public ArrayList<VertexInterface> getModelShortest() {
|
public ArrayList<VertexInterface> getModelShortest() {
|
||||||
return this.mazeAppModel.getShortest();
|
return this.mazeAppModel.getShortest();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<VertexInterface> getModelShortestShallowCopy() {
|
/** @see model.MazeAppModel */
|
||||||
return this.mazeAppModel.getShortestShallowCopy();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getModelDimensionX() {
|
public int getModelDimensionX() {
|
||||||
return this.mazeAppModel.getDimensionX();
|
return this.mazeAppModel.getDimensionX();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public int getModelDimensionY() {
|
public int getModelDimensionY() {
|
||||||
return this.mazeAppModel.getDimensionY();
|
return this.mazeAppModel.getDimensionY();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void setPathDrawn(int i) {
|
public void setPathDrawn(int i) {
|
||||||
this.mazeAppModel.setPathDrawn(i);
|
this.mazeAppModel.setPathDrawn(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @see model.MazeAppModel */
|
||||||
public void toggleModelBox(int boxLine, int boxRow) {
|
public void toggleModelBox(int boxLine, int boxRow) {
|
||||||
this.mazeAppModel.toggleBox(boxLine, boxRow);
|
this.mazeAppModel.toggleBox(boxLine, boxRow);
|
||||||
}
|
}
|
||||||
|
|||||||
+49
-12
@@ -15,13 +15,12 @@ import java.awt.event.MouseListener;
|
|||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import maze.Maze;
|
import maze.Maze;
|
||||||
import model.MazeAppModelMessage;
|
import model.MazeAppModelMessage;
|
||||||
import maze.EBox;
|
import maze.Box;
|
||||||
import dijkstra.VertexInterface;
|
import dijkstra.VertexInterface;
|
||||||
|
|
||||||
public final class MazeDrawing extends JPanel implements MouseListener {
|
public final class MazeDrawing extends JPanel implements MouseListener {
|
||||||
|
|
||||||
private final MazeApp mazeApp;
|
private final MazeApp mazeApp;
|
||||||
private boolean pathDrawingMode;
|
|
||||||
private ArrayList<VertexInterface> shortestPath;
|
private ArrayList<VertexInterface> shortestPath;
|
||||||
private ArrayList<VertexInterface> finalPath;
|
private ArrayList<VertexInterface> finalPath;
|
||||||
private int pathCounter;
|
private int pathCounter;
|
||||||
@@ -29,7 +28,6 @@ public final class MazeDrawing extends JPanel implements MouseListener {
|
|||||||
public MazeDrawing(MazeApp mazeApp) {
|
public MazeDrawing(MazeApp mazeApp) {
|
||||||
|
|
||||||
this.mazeApp = mazeApp;
|
this.mazeApp = mazeApp;
|
||||||
this.pathDrawingMode = false;
|
|
||||||
this.shortestPath = new ArrayList<VertexInterface>();
|
this.shortestPath = new ArrayList<VertexInterface>();
|
||||||
this.finalPath = null;
|
this.finalPath = null;
|
||||||
this.pathCounter = -1;
|
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) {
|
private void paintMaze(Graphics2D g, int width, int height) {
|
||||||
|
|
||||||
Maze maze = mazeApp.getModelMaze();
|
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) {
|
public void notifyForUpdates(Object param) {
|
||||||
|
|
||||||
if (param == MazeAppModelMessage.MazeRenewal ||
|
if (param == MazeAppModelMessage.MazeRenewal ||
|
||||||
param == MazeAppModelMessage.MazeLoaded) {
|
param == MazeAppModelMessage.MazeLoaded) {
|
||||||
if (this.mazeApp.getModelShortest() != null) {
|
if (this.mazeApp.getModelShortest() != null) {
|
||||||
this.finalPath
|
finalPath = new ArrayList<VertexInterface>(mazeApp.getModelShortest());
|
||||||
= this.mazeApp.getModelShortestShallowCopy();
|
|
||||||
Collections.reverse(finalPath);
|
Collections.reverse(finalPath);
|
||||||
}
|
}
|
||||||
this.repaint();
|
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) {
|
private void paintShortestPath(Graphics2D g, int width, int height) {
|
||||||
|
|
||||||
g.setColor(Color.WHITE);
|
g.setColor(Color.WHITE);
|
||||||
@@ -126,7 +137,7 @@ public final class MazeDrawing extends JPanel implements MouseListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (VertexInterface vertex : shortest) {
|
for (VertexInterface vertex : shortest) {
|
||||||
EBox box = (EBox) vertex;
|
Box box = (Box) vertex;
|
||||||
g.fillOval(box.getX() * boxWidth + factorWidth,
|
g.fillOval(box.getX() * boxWidth + factorWidth,
|
||||||
box.getY()*boxHeight + factorHeight,
|
box.getY()*boxHeight + factorHeight,
|
||||||
boxWidth - 2 * factorWidth,
|
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) {
|
private void animateShortestPath(Graphics2D g, int width, int height) {
|
||||||
|
|
||||||
g.setColor(Color.WHITE);
|
g.setColor(Color.WHITE);
|
||||||
@@ -158,8 +174,6 @@ public final class MazeDrawing extends JPanel implements MouseListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pathDrawingMode = true;
|
|
||||||
|
|
||||||
if (pathCounter == -1) {
|
if (pathCounter == -1) {
|
||||||
JOptionPane.showMessageDialog(this,
|
JOptionPane.showMessageDialog(this,
|
||||||
"Click on the maze to see the shortest path evolve");
|
"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) {
|
for (VertexInterface vertex : shortestPath) {
|
||||||
EBox box = (EBox) vertex;
|
Box box = (Box) vertex;
|
||||||
g.fillOval(box.getX() * boxWidth + factorWidth,
|
g.fillOval(box.getX() * boxWidth + factorWidth,
|
||||||
box.getY() * boxHeight + factorHeight,
|
box.getY() * boxHeight + factorHeight,
|
||||||
boxWidth - 2 * factorWidth,
|
boxWidth - 2 * factorWidth,
|
||||||
@@ -175,15 +189,30 @@ public final class MazeDrawing extends JPanel implements MouseListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pathCounter >= finalPath.size()) {
|
if (pathCounter >= finalPath.size()) {
|
||||||
pathDrawingMode = false;
|
|
||||||
mazeApp.setPathDrawn(2);
|
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) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
int boxRow = getBoxRow(e.getX());
|
int boxRow = getBoxRow(e.getX());
|
||||||
int boxLine = getBoxLine(e.getY());
|
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) {
|
if (e.getButton() == MouseEvent.BUTTON3) {
|
||||||
BoxTypeEditionMenu boxTypeEditionMenu
|
BoxTypeEditionMenu boxTypeEditionMenu
|
||||||
= new BoxTypeEditionMenu(mazeApp, boxLine, boxRow);
|
= new BoxTypeEditionMenu(mazeApp, boxLine, boxRow);
|
||||||
@@ -213,12 +242,20 @@ public final class MazeDrawing extends JPanel implements MouseListener {
|
|||||||
return;
|
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) {
|
private int getBoxRow(int x) {
|
||||||
int dimensionX = mazeApp.getModelDimensionX();
|
int dimensionX = mazeApp.getModelDimensionX();
|
||||||
int boxWidth = getWidth() / dimensionX;
|
int boxWidth = getWidth() / dimensionX;
|
||||||
return x / boxWidth;
|
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) {
|
private int getBoxLine(int y) {
|
||||||
int dimensionY = mazeApp.getModelDimensionY();
|
int dimensionY = mazeApp.getModelDimensionY();
|
||||||
int boxHeight = getHeight() / dimensionY;
|
int boxHeight = getHeight() / dimensionY;
|
||||||
@@ -237,8 +274,8 @@ public final class MazeDrawing extends JPanel implements MouseListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Clear the list of boxes in the local shortest path */
|
||||||
public void clearShortestPath() {
|
public void clearShortestPath() {
|
||||||
this.pathCounter = -1;
|
this.pathCounter = -1;
|
||||||
this.shortestPath = new ArrayList<VertexInterface>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package ui;
|
|||||||
|
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
|
import maze.MazeSavingException;
|
||||||
|
|
||||||
public final class SaveBox {
|
public final class SaveBox {
|
||||||
private static String message = "Maze not saved. Save it ?";
|
private static String message = "Maze not saved. Save it ?";
|
||||||
@@ -20,13 +21,31 @@ public final class SaveBox {
|
|||||||
return false;
|
return false;
|
||||||
case JOptionPane.OK_OPTION:
|
case JOptionPane.OK_OPTION:
|
||||||
if (mazeApp.getModelFilename() != null) {
|
if (mazeApp.getModelFilename() != null) {
|
||||||
|
try {
|
||||||
mazeApp.saveModelMaze();
|
mazeApp.saveModelMaze();
|
||||||
}
|
}
|
||||||
|
catch (MazeSavingException mse) {
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
mse.getMessage(),
|
||||||
|
"Maze saving error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
return promptAndContinue(null, mazeApp, title);
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
String filename = FileBox.save(mazeApp);
|
String filename = FileBox.save(mazeApp);
|
||||||
if (filename != null) {
|
if (filename != null) {
|
||||||
|
try {
|
||||||
mazeApp.saveModelMaze(filename);
|
mazeApp.saveModelMaze(filename);
|
||||||
}
|
}
|
||||||
|
catch (MazeSavingException mse) {
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
mse.getMessage(),
|
||||||
|
"Maze saving error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
return promptAndContinue(null, mazeApp, title);
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
return promptAndContinue(null, mazeApp, title);
|
return promptAndContinue(null, mazeApp, title);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user