Merge branch 'refactorize' of antograb/TpMazeJava into master

This commit was merged in pull request #31.
This commit is contained in:
regar42 authored and Gogs committed 2018-01-14 18:02:44 +01:00
commit 95520bd1bb
44 files changed
+658 -352

No files matched your search

-8
View File
@@ -1,8 +0,0 @@
WEEEEEWW
EDWWWEEW
WEEWWWEE
WWEEEEEW
EEEWWWWW
EWWWWAWW
EWWEEEWW
EEEEWWWW
+1 -1
View File
@@ -2,7 +2,7 @@ package dijkstra;
import java.util.HashSet; import java.util.HashSet;
public class ASet implements ASetInterface { public final class ASet implements ASetInterface {
private HashSet<VertexInterface> vertexHashSet; private HashSet<VertexInterface> vertexHashSet;
+2 -1
View File
@@ -3,7 +3,8 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Hashtable; import java.util.Hashtable;
public class Dijkstra { /** Litteral implementation of the Dijkstra's algorithm given in the subject */
public final class Dijkstra {
private static PreviousInterface dijkstra(GraphInterface g, private static PreviousInterface dijkstra(GraphInterface g,
VertexInterface r, VertexInterface r,
+1 -1
View File
@@ -2,7 +2,7 @@ package dijkstra;
import java.util.Hashtable; import java.util.Hashtable;
public class Pi implements PiInterface { public final class Pi implements PiInterface {
private Hashtable<VertexInterface, Integer> hashPi; private Hashtable<VertexInterface, Integer> hashPi;
+3 -3
View File
@@ -6,7 +6,7 @@ import java.util.List;
import maze.Box; import maze.Box;
public class Previous implements PreviousInterface { public final class Previous implements PreviousInterface {
private Hashtable<VertexInterface, VertexInterface> hashPrevious; private Hashtable<VertexInterface, VertexInterface> hashPrevious;
private ArrayList<VertexInterface> shortestPath; private ArrayList<VertexInterface> shortestPath;
@@ -32,13 +32,13 @@ public class Previous implements PreviousInterface {
shortestPath.add(vertex); shortestPath.add(vertex);
VertexInterface father = getPrevious(vertex); VertexInterface father = getPrevious(vertex);
if (getPrevious(father) == null) { if (getPrevious(father) == null) {
shortestPath.add(father); shortestPath.add(father);
return shortestPath; return shortestPath;
} }
else { else {
return getShortestPathTo(father); return getShortestPathTo(father);
} }
} }
} }
+3
View File
@@ -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
+2 -2
View File
@@ -5,9 +5,9 @@ package maze;
* It's the last <code>Box</code> to be walked through and * It's the last <code>Box</code> to be walked through and
* it's an exit to the <code>Maze</code>. * it's an exit to the <code>Maze</code>.
*/ */
public class ABox extends EBox{ public final class ABox extends EBox{
public ABox (String label, int y, int x, Maze maze) { public ABox(String label, int y, int x, Maze maze) {
super(label, y, x, maze) ; super(label, y, x, maze) ;
} }
+5 -6
View File
@@ -24,17 +24,16 @@ public abstract class Box
private String label; private String label;
private Maze maze; private Maze maze;
private ArrayList<VertexInterface> neighbourList; private ArrayList<VertexInterface> neighbourList;
private ArrayList<VertexInterface> candidates; private ArrayList<VertexInterface> candidates =
new ArrayList<VertexInterface>();
public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation public Box(String label, int y , int x, Maze maze) { //y first because of graphical maze representation
this.label = label; this.label = label;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.maze = maze; this.maze = maze;
this.candidates = new ArrayList<VertexInterface>();
} }
public ArrayList<VertexInterface> generateNeighbours() { public ArrayList<VertexInterface> generateNeighbours() {
this.neighbourList = maze.generateNeighbours(this); this.neighbourList = maze.generateNeighbours(this);
return this.neighbourList; return this.neighbourList;
@@ -63,7 +62,7 @@ public abstract class Box
public boolean isDeparture() { public boolean isDeparture() {
return false; return false;
} }
public ArrayList<VertexInterface> getNeighbourList() { public ArrayList<VertexInterface> getNeighbourList() {
return neighbourList; return neighbourList;
} }
+2 -2
View File
@@ -5,9 +5,9 @@ package maze;
* is first in the <code>Maze</code>. * is first in the <code>Maze</code>.
*/ */
public class DBox extends EBox{ public final class DBox extends EBox{
public DBox (String label, int y, int x, Maze maze) { public DBox(String label, int y, int x, Maze maze) {
super(label, y, x, maze); super(label, y, x, maze);
} }
+1 -1
View File
@@ -7,7 +7,7 @@ package maze;
public class EBox extends Box{ public class EBox extends Box{
public EBox (String label, int y, int x, Maze maze) { public EBox(String label, int y, int x, Maze maze) {
super(label, y, x, maze); super(label, y, x, maze);
} }
} }
+83 -25
View File
@@ -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;
@@ -16,17 +17,18 @@ import dijkstra.VertexInterface;
* @author Sylvain Rager * @author Sylvain Rager
* *
*/ */
public class Maze public final class Maze
implements GraphInterface { implements GraphInterface {
private int dimensionX; private int dimensionX;
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);
} }
public Maze(Maze maze) { public Maze(Maze maze) {
this.dimensionX = maze.getDimensionX(); this.dimensionX = maze.getDimensionX();
@@ -51,6 +53,12 @@ public 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 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 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 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 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 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,6 +309,47 @@ public class Maze
dimensionY = dimensionY - 1; dimensionY = dimensionY - 1;
} }
/** Return the box at given coordinates */
public Box getBox(int boxLine, int boxRow) {
return this.maze[boxLine][boxRow];
}
/**
* Set the box at given coordinates to an arrival box. If an arrival box
* was already in the maze, set it to an empty box.
*/
public void setABox(int boxLine, int boxRow) {
if (getArrival() != null) {
setEBox(((Box) getArrival()).getY(), ((Box) getArrival()).getX());
}
this.maze[boxLine][boxRow] = new ABox("A", boxLine, boxRow, this);
resetNeighbourLists();
}
/**
* Set the box at given coordinates to a departure box. If a departure box
* was already in the maze, set it to an empty box.
*/
public void setDBox(int boxLine, int boxRow) {
if (getDeparture() != null) {
setEBox(((Box) getDeparture()).getY(), ((Box) getDeparture()).getX());
}
this.maze[boxLine][boxRow] = new DBox("D", boxLine, boxRow, this);
resetNeighbourLists();
}
/** Set the box at given coordinates to an empty box */
public void setEBox(int boxLine, int boxRow) {
this.maze[boxLine][boxRow] = new EBox("E", boxLine, boxRow, this);
resetNeighbourLists();
}
/** Set the box at given coordinates to a wall box */
public void setWBox(int boxLine, int boxRow) {
this.maze[boxLine][boxRow] = new WBox("W", boxLine, boxRow, this);
resetNeighbourLists();
}
public int getCost(VertexInterface start, VertexInterface end) { public int getCost(VertexInterface start, VertexInterface end) {
Box startBox = (Box) start; Box startBox = (Box) start;
@@ -349,7 +403,7 @@ public class Maze
} }
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) { public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) {
Box box = (Box) vertex; Box box = (Box) vertex;
int x = box.getX(); int x = box.getX();
int y = box.getY(); int y = box.getY();
@@ -363,28 +417,28 @@ public class Maze
if (x > 0 && maze[y][x-1].isWalkable()) { if (x > 0 && maze[y][x-1].isWalkable()) {
neighbourList.add(maze[y][x-1]); neighbourList.add(maze[y][x-1]);
} }
if (x < dimensionX-1 && maze[y][x+1].isWalkable()) { if (x < dimensionX-1 && maze[y][x+1].isWalkable()) {
neighbourList.add(maze[y][x+1]); neighbourList.add(maze[y][x+1]);
} }
if (y > 0 && maze[y-1][x].isWalkable()) { if (y > 0 && maze[y-1][x].isWalkable()) {
neighbourList.add(maze[y-1][x]); neighbourList.add(maze[y-1][x]);
} }
if (y < dimensionY-1 && maze[y+1][x].isWalkable()) { if (y < dimensionY-1 && maze[y+1][x].isWalkable()) {
neighbourList.add(maze[y+1][x]); neighbourList.add(maze[y+1][x]);
} }
return neighbourList; return neighbourList;
} }
/**Constructs the maze from a text file, according to each single character on each line. /**Constructs the maze from a text file, according to each single character on each line.
* @param filename * @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 final void initFromTextFile(String filename) { throws MazeReadingException {
FileReader fr = null; FileReader fr = null;
BufferedReader br = null; BufferedReader br = null;
@@ -402,9 +456,9 @@ public class Maze
try { //generates the maze try { //generates the maze
fr = new FileReader(filename); fr = new FileReader(filename);
br = new BufferedReader(fr); br = new BufferedReader(fr);
String currentLine = null; String currentLine = null;
currentLine = br.readLine(); currentLine = br.readLine();
dimensionX = currentLine.length(); //maze's width dimensionX = currentLine.length(); //maze's width
int currentLineLength; int currentLineLength;
@@ -423,7 +477,7 @@ public class Maze
char currentChar = currentLine.charAt(compteurColonne); char currentChar = currentLine.charAt(compteurColonne);
switch(currentChar) { switch(currentChar) {
case 'W': case 'W':
maze[compteurLigne][compteurColonne] = maze[compteurLigne][compteurColonne] =
new WBox("W", compteurLigne, compteurColonne, this) ; new WBox("W", compteurLigne, compteurColonne, this) ;
@@ -454,16 +508,19 @@ public 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 final 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;
@@ -479,9 +536,8 @@ public 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) {}
} }
@@ -499,19 +555,21 @@ public 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;
} }
public Box[][] getMaze() { protected Box[][] getMaze() {
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();
+3 -6
View File
@@ -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 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.");
} }
} }
+16
View 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 + ".");
}
}
+2 -2
View File
@@ -5,9 +5,9 @@ package maze;
* that can't be walked through (a wall). * that can't be walked through (a wall).
*/ */
public class WBox extends Box { public final class WBox extends Box {
public WBox (String label, int y, int x, Maze maze) { public WBox(String label, int y, int x, Maze maze) {
super(label, y, x, maze); super(label, y, x, maze);
} }
+96 -44
View File
@@ -8,17 +8,21 @@ import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import dijkstra.*; import dijkstra.*;
import maze.*; import maze.Maze;
import maze.Box;
public class MazeAppModel extends Observable { import maze.MazeReadingException;
import maze.MazeSavingException;
public class MazeAppModel
extends Observable
implements MazeModelInterface {
/** The file into which the maze is to be saved */
private String filename; private String filename;
private Maze maze; private Maze maze;
private VertexInterface departure; private VertexInterface departure;
private VertexInterface arrival; private VertexInterface arrival;
private Previous previous; private Previous previous;
private ArrayList<VertexInterface> shortest; private ArrayList<VertexInterface> shortest;
private ArrayList<VertexInterface> boxList;
private boolean saved = true; private boolean saved = true;
/* /*
* There are three states for pathDrawn : * There are three states for pathDrawn :
@@ -38,78 +42,93 @@ public class MazeAppModel extends Observable {
private void initModelFromMaze() { private void initModelFromMaze() {
departure = maze.getDeparture(); departure = maze.getDeparture();
arrival = maze.getArrival(); arrival = maze.getArrival();
boxList = maze.getVertexes();
if (departure == null || arrival == null) { if (departure == null || arrival == null) {
previous = null; previous = null;
shortest = null; shortest = null;
} }
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 {
shortest = previous.getShortestPathTo(arrival); shortest = previous.getShortestPathTo(arrival);
} }
} }
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
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();
notifyObservers(MazeAppModelMessage.MazeLoaded);
} }
public Maze getMaze() { public Maze getMaze() {
return maze; return maze;
} }
/** Return the box at given coordinates */
public Box getBox(int boxLine, int boxRow) {
return this.maze.getBox(boxLine, boxRow);
}
/** Return the name of the file where the maze is currently being saved */
public String getFilename() { 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> getShortestDeepCopy() { /** Indicate whether the path is drawn, currently being drawn, or neither */
ArrayList<VertexInterface> deepCopy = new ArrayList<VertexInterface>();
for (VertexInterface vertex : this.shortest) {
deepCopy.add(vertex);
}
return deepCopy;
}
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();
notifyObservers(MazeAppModelMessage.MazeSaved);
} }
public void saveToFile(String filename) { /** Save the maze to the given file and set it to be the current file */
this.filename = new String(filename); public void saveToFile(String filename) throws MazeSavingException {
maze.saveToTextFile(this.filename); maze.saveToTextFile(filename);
this.filename = 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.getMaze()[boxLine][boxRow].isWalkable()) { if (maze.getBox(boxLine, boxRow).isWalkable()) {
setWBox(boxLine, boxRow); setWBox(boxLine, boxRow);
} }
else { else {
@@ -117,98 +136,131 @@ 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.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze); maze.setWBox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
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.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze); maze.setEBox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
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) { maze.setDBox(boxLine, boxRow);
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(); initModelFromMaze();
saved = false; saved = false;
setChanged();
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) { maze.setABox(boxLine, boxRow);
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(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
/**
* Set the maze to be a new clear (full of empty boxes) maze of given
* dimensions
*/
public void newClearMaze(int numberOfLines, int numberOfRows) { public void newClearMaze(int numberOfLines, int numberOfRows) {
maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows)); maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows));
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
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();
saved = false; saved = false;
setChanged();
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();
saved = false; saved = false;
setChanged();
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();
saved = false; saved = false;
setChanged();
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();
saved = false; saved = false;
setChanged();
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() {
return maze.getDimensionX();
}
/** Get the number of lines in the maze */
public int getDimensionY() {
return maze.getDimensionY();
}
} }
+16 -3
View File
@@ -1,6 +1,19 @@
package model; package model;
public class MazeAppModelMessage { /**
public final static String MazeRenewal = "MazeRenewal"; * This class is used to transport messages about an event happening
public final static String FileChange = "FileChange"; * to the model
*/
public abstract class MazeAppModelMessage {
/** Indicates that the maze has been renewed
*(a replacement of the maze -resizing, new maze, ...-
* or a change on any box)
*/
public final static String MazeRenewal = "Maze renewed";
/** Indicates a new file has been selected for the maze to be saved in */
public final static String FileChange = "File changed";
/** Indicates the maze has just been saved */
public final static String MazeSaved = "Maze saved";
/** Indicates a maze has been loaded from file */
public final static String MazeLoaded = "Maze loaded";
} }
+30
View File
@@ -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();
}
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class AddLineMenuItem extends JMenuItem public final class AddLineMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class AddRowMenuItem extends JMenuItem public final class AddRowMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -7,7 +7,7 @@ import javax.swing.Action;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class AnimatePathMenuItem extends JMenuItem public final class AnimatePathMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+4 -2
View File
@@ -4,7 +4,8 @@ import javax.swing.JPopupMenu;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import maze.Box; import maze.Box;
public class BoxTypeEditionMenu extends JPopupMenu { public final class BoxTypeEditionMenu extends JPopupMenu {
JMenuItem setWBox; JMenuItem setWBox;
JMenuItem setEBox; JMenuItem setEBox;
JMenuItem setDBox; JMenuItem setDBox;
@@ -15,11 +16,12 @@ public class BoxTypeEditionMenu extends JPopupMenu {
JMenuItem delLine; JMenuItem delLine;
public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) { public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) {
add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow)); add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow));
add(setWBox = new SetWBoxMenuItem(mazeApp, boxLine, boxRow)); add(setWBox = new SetWBoxMenuItem(mazeApp, boxLine, boxRow));
add(setDBox = new SetDBoxMenuItem(mazeApp, boxLine, boxRow)); add(setDBox = new SetDBoxMenuItem(mazeApp, boxLine, boxRow));
add(setABox = new SetABoxMenuItem(mazeApp, boxLine, boxRow)); add(setABox = new SetABoxMenuItem(mazeApp, boxLine, boxRow));
Box box = (Box) mazeApp.getMazeAppModel().getMaze().getMaze()[boxLine][boxRow]; Box box = (Box) mazeApp.getModelBox(boxLine, boxRow);
setEBox.setEnabled((box.isWalkable() && (box.isDeparture() || setEBox.setEnabled((box.isWalkable() && (box.isDeparture() ||
box.isArrival())) || ! box.isWalkable()); box.isArrival())) || ! box.isWalkable());
setWBox.setEnabled(box.isWalkable()); setWBox.setEnabled(box.isWalkable());
+1 -1
View File
@@ -6,7 +6,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
public class ClearMazeMenuItem extends JMenuItem public final class ClearMazeMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -7,7 +7,7 @@ import model.MazeAppModel;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
public class ClearShortestPathMenuItem extends JMenuItem public final class ClearShortestPathMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class DelLineMenuItem extends JMenuItem public final class DelLineMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class DelRowMenuItem extends JMenuItem public final class DelRowMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -1
View File
@@ -7,7 +7,7 @@ import model.MazeAppModel;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
public class DrawShortestPathMenuItem extends JMenuItem public final class DrawShortestPathMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
+1 -5
View File
@@ -3,7 +3,7 @@ package ui;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
public class FileBox { public final class FileBox {
private static JFileChooser fileChooser = new JFileChooser(); private static JFileChooser fileChooser = new JFileChooser();
private static FileNameExtensionFilter filter = private static FileNameExtensionFilter filter =
new FileNameExtensionFilter("Text files", "txt"); new FileNameExtensionFilter("Text files", "txt");
@@ -13,8 +13,6 @@ public 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 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;
} }
+149 -1
View File
@@ -1,8 +1,19 @@
package ui; package ui;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observer;
import java.util.Observable;
import javax.swing.JOptionPane;
public class FileMenu extends JMenu { import model.MazeAppModelMessage;
import maze.MazeReadingException;
import maze.MazeSavingException;
public final class FileMenu extends JMenu {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final LoadMazeMenuItem loadMazeMenuItem; private final LoadMazeMenuItem loadMazeMenuItem;
@@ -20,4 +31,141 @@ public class FileMenu extends JMenu {
add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp)); add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp));
add(quitMenuItem = new QuitMenuItem(mazeApp)); add(quitMenuItem = new QuitMenuItem(mazeApp));
} }
public final class LoadMazeMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public LoadMazeMenuItem(MazeApp mazeApp) {
super("Load maze");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control O"));
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
if (! mazeApp.isModelSaved()) {
if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){
return;
}
}
String filename = FileBox.load(mazeApp);
if (filename != null) {
try {
mazeApp.loadModelMaze(filename);
}
catch (MazeReadingException mre) {
JOptionPane.showMessageDialog(this,
"Please check you have sufficient permissions to"
+ " read the file and that the file has a correct"
+ " maze format.",
"Error loading maze",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
public final class SaveMazeMenuItem extends JMenuItem
implements ActionListener, Observer {
private final MazeApp mazeApp;
public SaveMazeMenuItem(MazeApp mazeApp) {
super("Save maze");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control S"));
mazeApp.getMazeAppModel().addObserver(this);
if (mazeApp.getModelFilename() == null) {
this.setEnabled(false);
}
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
if (mazeApp.getModelFilename() != null) {
try {
mazeApp.saveModelMaze();
}
catch (MazeSavingException mse) {
JOptionPane.showMessageDialog(this,
mse.getMessage(),
"Maze saving error",
JOptionPane.ERROR_MESSAGE);
}
}
}
@Override
public void update(Observable obs, Object param) {
if (param == MazeAppModelMessage.MazeRenewal &&
mazeApp.getModelFilename() != null) {
this.setEnabled(true);
}
else {
this.setEnabled(false);
}
}
}
public final class SaveMazeAsMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public SaveMazeAsMenuItem(MazeApp mazeApp) {
super("Save maze as");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control shift S"));
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
String filename = FileBox.save(mazeApp);
if (filename != null) {
try {
mazeApp.saveModelMaze(filename);
}
catch (MazeSavingException mse) {
JOptionPane.showMessageDialog(this,
mse.getMessage(),
"Maze saving error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
public final class QuitMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public QuitMenuItem(MazeApp mazeApp) {
super("Quit");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control Q"));
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
if (! mazeApp.isModelSaved()) {
if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) {
return;
}
}
System.exit(0);
}
}
} }
-36
View File
@@ -1,36 +0,0 @@
package ui;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import model.MazeAppModel;
public class LoadMazeMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public LoadMazeMenuItem(MazeApp mazeApp) {
super("Load maze");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control O"));
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
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);
}
}
}
+120 -6
View File
@@ -1,16 +1,21 @@
package ui; package ui;
import javax.swing.*; import javax.swing.*;
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.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.awt.event.WindowListener; import java.awt.event.WindowListener;
public class MazeApp extends JFrame public final class MazeApp extends JFrame
implements Observer, WindowListener { implements Observer, WindowListener {
private final MazeMenuBar mazeMenuBar; private final MazeMenuBar mazeMenuBar;
@@ -40,11 +45,13 @@ public 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")) {
@@ -75,60 +82,167 @@ public class MazeApp extends JFrame
} }
public void windowOpened(WindowEvent e) { public void windowOpened(WindowEvent e) {
JOptionPane.showMessageDialog(this,
"Welcome to this maze solving application propelled by"
+ " Dijkstra's algorithm.\n\n"
+ "White boxes are walls, black boxes are empty and walkable.\n"
+ "Feel free to edit boxes type by right clicking on them.\n\n"
+ "You can find an example maze in data/labyrinthe.txt.",
"Welcome",
JOptionPane.INFORMATION_MESSAGE);
return; 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() {
windowPanel.animateShortestPath();
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();
} }
public void animateShortestPath() { /** @see model.MazeAppModel */
public boolean isModelSaved( ) {
return this.mazeAppModel.isSaved();
}
windowPanel.animateShortestPath(); /** @see model.MazeAppModel */
mazeAppModel.setPathDrawn(1); public void loadModelMaze(String filename) throws MazeReadingException {
this.mazeAppModel.loadMaze(filename);
}
/** @see model.MazeAppModel */
public maze.Box getModelBox(int boxLine, int boxRow) {
return this.mazeAppModel.getBox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public void saveModelMaze(String filename) throws MazeSavingException {
mazeAppModel.saveToFile(filename);
}
/** @see model.MazeAppModel */
public void saveModelMaze() throws MazeSavingException {
mazeAppModel.saveToFile();
}
/** @see model.MazeAppModel */
public String getModelFilename() {
return mazeAppModel.getFilename();
}
/** @see model.MazeAppModel */
public void setModelABox(int boxLine, int boxRow) {
this.mazeAppModel.setABox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public void setModelDBox(int boxLine, int boxRow) {
this.mazeAppModel.setDBox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public void setModelEBox(int boxLine, int boxRow) {
this.mazeAppModel.setEBox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public void setModelWBox(int boxLine, int boxRow) {
this.mazeAppModel.setWBox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public int isPathDrawn() {
return this.mazeAppModel.isPathDrawn();
}
/** @see model.MazeAppModel */
public Maze getModelMaze() {
return this.mazeAppModel.getMaze();
}
/** @see model.MazeAppModel */
public ArrayList<VertexInterface> getModelShortest() {
return this.mazeAppModel.getShortest();
}
/** @see model.MazeAppModel */
public int getModelDimensionX() {
return this.mazeAppModel.getDimensionX();
}
/** @see model.MazeAppModel */
public int getModelDimensionY() {
return this.mazeAppModel.getDimensionY();
}
/** @see model.MazeAppModel */
public void setPathDrawn(int i) {
this.mazeAppModel.setPathDrawn(i);
}
/** @see model.MazeAppModel */
public void toggleModelBox(int boxLine, int boxRow) {
this.mazeAppModel.toggleBox(boxLine, boxRow);
} }
} }
+73 -52
View File
@@ -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 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 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;
@@ -39,7 +37,7 @@ public class MazeDrawing extends JPanel implements MouseListener {
} }
@Override @Override
protected final void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d); super.paintComponent(g2d);
@@ -48,15 +46,17 @@ public class MazeDrawing extends JPanel implements MouseListener {
int height = getHeight(); int height = getHeight();
paintMaze(g2d, width, height); paintMaze(g2d, width, height);
if (mazeApp.getMazeAppModel().isPathDrawn() == 1) { if (mazeApp.isPathDrawn() == 1) {
animateShortestPath(g2d, width, height); animateShortestPath(g2d, width, height);
} else if (mazeApp.getMazeAppModel().isPathDrawn() == 2) { } else if (mazeApp.isPathDrawn() == 2) {
paintShortestPath(g2d, width, height); paintShortestPath(g2d, width, height);
} }
} }
/** 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.getMazeAppModel().getMaze();
Maze maze = mazeApp.getModelMaze();
int dimensionX = maze.getDimensionX(); int dimensionX = maze.getDimensionX();
int dimensionY = maze.getDimensionY(); int dimensionY = maze.getDimensionY();
int boxWidth = width / dimensionX; int boxWidth = width / dimensionX;
@@ -64,11 +64,11 @@ public class MazeDrawing extends JPanel implements MouseListener {
for (int line = 0; line < dimensionY; line++) { for (int line = 0; line < dimensionY; line++) {
for (int row = 0; row < dimensionX; row++) { for (int row = 0; row < dimensionX; row++) {
if (maze.getMaze()[line][row].isWalkable()) { if (maze.getBox(line, row).isWalkable()) {
if (maze.getMaze()[line][row].isDeparture()) { if (maze.getBox(line, row).isDeparture()) {
g.setColor(Color.GREEN); g.setColor(Color.GREEN);
} }
else if (maze.getMaze()[line][row].isArrival()) { else if (maze.getBox(line, row).isArrival()) {
g.setColor(Color.RED); g.setColor(Color.RED);
} }
else { else {
@@ -87,30 +87,43 @@ public 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 ||
if (this.mazeApp.getMazeAppModel().getShortest() != null) { param == MazeAppModelMessage.MazeLoaded) {
this.finalPath if (this.mazeApp.getModelShortest() != null) {
= this.mazeApp.getMazeAppModel().getShortestDeepCopy(); finalPath = new ArrayList<VertexInterface>(mazeApp.getModelShortest());
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);
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); int dimensionX = mazeApp.getModelDimensionX();
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); int dimensionY = mazeApp.getModelDimensionY();
int boxWidth = width / dimensionX; int boxWidth = width / dimensionX;
int boxHeight = height / dimensionY; int boxHeight = height / dimensionY;
int factorWidth = boxWidth / 3; int factorWidth = boxWidth / 3;
int factorHeight = boxHeight / 3; int factorHeight = boxHeight / 3;
ArrayList<VertexInterface> shortest = ArrayList<VertexInterface> shortest =
mazeApp.getMazeAppModel().getShortest(); mazeApp.getModelShortest();
if (shortest == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { if (shortest == null && mazeApp.isPathDrawn() == 0) {
mazeApp.clearShortestPath(); mazeApp.clearShortestPath();
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
"Check that your graph contains a departure and an arrival," "Check that your graph contains a departure and an arrival,"
@@ -124,7 +137,7 @@ public 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,
@@ -132,17 +145,22 @@ public 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);
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); int dimensionX = mazeApp.getModelDimensionX();
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); int dimensionY = mazeApp.getModelDimensionY();
int boxWidth = width / dimensionX; int boxWidth = width / dimensionX;
int boxHeight = height / dimensionY; int boxHeight = height / dimensionY;
int factorWidth = boxWidth / 3; int factorWidth = boxWidth / 3;
int factorHeight = boxHeight / 3; int factorHeight = boxHeight / 3;
if (this.finalPath == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { if (this.finalPath == null && mazeApp.isPathDrawn() == 0) {
mazeApp.clearShortestPath(); mazeApp.clearShortestPath();
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
"Check that your graph contains a departure and an arrival," "Check that your graph contains a departure and an arrival,"
@@ -156,58 +174,53 @@ public 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");
pathCounter = 0; pathCounter = 0;
repaint();
} }
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,
boxHeight - 2 * factorHeight); 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()) { if (pathCounter >= finalPath.size()) {
pathDrawingMode = false; mazeApp.setPathDrawn(2);
mazeApp.getMazeAppModel().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() != 1) {
// 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);
boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY()); boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY());
} }
else if (e.getButton() == MouseEvent.BUTTON1) { else if (e.getButton() == MouseEvent.BUTTON1) {
mazeApp.getMazeAppModel().toggleBox(boxLine, boxRow); mazeApp.toggleModelBox(boxLine, boxRow);
} }
} else { } else {
shortestPath.add(this.finalPath.get(pathCounter)); shortestPath.add(this.finalPath.get(pathCounter));
@@ -230,14 +243,22 @@ public 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.getMazeAppModel().getMaze().getDimensionX(); 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.getMazeAppModel().getMaze().getDimensionY(); int dimensionY = mazeApp.getModelDimensionY();
int boxHeight = getHeight() / dimensionY; int boxHeight = getHeight() / dimensionY;
return y / boxHeight; return y / boxHeight;
} }
@@ -254,8 +275,8 @@ public 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>();
} }
} }
+1 -1
View File
@@ -2,7 +2,7 @@ package ui;
import javax.swing.JMenu; import javax.swing.JMenu;
public class MazeMenu extends JMenu { public final class MazeMenu extends JMenu {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final DrawShortestPathMenuItem drawMenuItem; private final DrawShortestPathMenuItem drawMenuItem;
+1 -1
View File
@@ -2,7 +2,7 @@ package ui;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
public class MazeMenuBar extends JMenuBar { public final class MazeMenuBar extends JMenuBar {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final FileMenu fileMenu; private final FileMenu fileMenu;
+1 -2
View File
@@ -7,7 +7,7 @@ import javax.swing.JMenuItem;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
public class NewClearMazeMenuItem extends JMenuItem public final class NewClearMazeMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -24,7 +24,6 @@ public class NewClearMazeMenuItem extends JMenuItem
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
spinnerBox.createAndShowGUI(); spinnerBox.createAndShowGUI();
} }
} }
+2 -2
View File
@@ -16,7 +16,7 @@ import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel; import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout; import javax.swing.SpringLayout;
public class NewMazeSpinnerBox extends JPanel public final class NewMazeSpinnerBox extends JPanel
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -48,7 +48,7 @@ public class NewMazeSpinnerBox extends JPanel
JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel); JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel);
} }
static protected JSpinner addLabeledSpinner(Container c, protected static JSpinner addLabeledSpinner(Container c,
String label, String label,
SpinnerModel model) { SpinnerModel model) {
-36
View File
@@ -1,36 +0,0 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import model.MazeAppModel;
public class QuitMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public QuitMenuItem(MazeApp mazeApp) {
super("Quit");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control Q"));
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
if (! mazeAppModel.isSaved()) {
if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) {
return;
}
}
System.exit(0);
}
}
+23 -7
View File
@@ -2,16 +2,14 @@ package ui;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import java.awt.Component; import java.awt.Component;
import maze.MazeSavingException;
import model.MazeAppModel; public final class SaveBox {
public class SaveBox {
private static String message = "Maze not saved. Save it ?"; private static String message = "Maze not saved. Save it ?";
public static boolean promptAndContinue(Component parent, public static boolean promptAndContinue(Component parent,
MazeApp mazeApp, MazeApp mazeApp,
String title) { String title) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
int response = JOptionPane.showOptionDialog(parent, int response = JOptionPane.showOptionDialog(parent,
message, message,
title, title,
@@ -22,13 +20,31 @@ public class SaveBox {
case JOptionPane.CANCEL_OPTION: case JOptionPane.CANCEL_OPTION:
return false; return false;
case JOptionPane.OK_OPTION: case JOptionPane.OK_OPTION:
if (mazeAppModel.getFilename() != null) { if (mazeApp.getModelFilename() != null) {
mazeAppModel.saveToFile(); try {
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) {
mazeApp.getMazeAppModel().saveToFile(filename); try {
mazeApp.saveModelMaze(filename);
}
catch (MazeSavingException mse) {
JOptionPane.showMessageDialog(null,
mse.getMessage(),
"Maze saving error",
JOptionPane.ERROR_MESSAGE);
return promptAndContinue(null, mazeApp, title);
}
} }
else { else {
return promptAndContinue(null, mazeApp, title); return promptAndContinue(null, mazeApp, title);
-32
View File
@@ -1,32 +0,0 @@
package ui;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import model.MazeAppModel;
public class SaveMazeAsMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public SaveMazeAsMenuItem(MazeApp mazeApp) {
super("Save maze as");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control shift S"));
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
String filename = FileBox.save(mazeApp);
if (filename != null) {
mazeApp.getMazeAppModel().saveToFile(filename);
}
}
}
-47
View File
@@ -1,47 +0,0 @@
package ui;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observer;
import java.util.Observable;
import model.MazeAppModel;
public class SaveMazeMenuItem extends JMenuItem
implements ActionListener, Observer {
private final MazeApp mazeApp;
public SaveMazeMenuItem(MazeApp mazeApp) {
super("Save maze");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control S"));
mazeApp.getMazeAppModel().addObserver(this);
if (mazeApp.getMazeAppModel().getFilename() == null) {
this.setEnabled(false);
}
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
if (mazeAppModel.getFilename() != null) {
mazeAppModel.saveToFile();
}
}
@Override
public void update(Observable obs, Object param) {
if (mazeApp.getMazeAppModel().getFilename() != null) {
this.setEnabled(true);
}
else {
this.setEnabled(false);
}
}
}
+2 -2
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class SetABoxMenuItem extends JMenuItem public final class SetABoxMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -21,6 +21,6 @@ public class SetABoxMenuItem extends JMenuItem
} }
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setABox(boxLine, boxRow); mazeApp.setModelABox(boxLine, boxRow);
} }
} }
+2 -2
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class SetDBoxMenuItem extends JMenuItem public final class SetDBoxMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -21,6 +21,6 @@ public class SetDBoxMenuItem extends JMenuItem
} }
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setDBox(boxLine, boxRow); mazeApp.setModelDBox(boxLine, boxRow);
} }
} }
+2 -2
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class SetEBoxMenuItem extends JMenuItem public final class SetEBoxMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -21,6 +21,6 @@ public class SetEBoxMenuItem extends JMenuItem
} }
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setEBox(boxLine, boxRow); mazeApp.setModelEBox(boxLine, boxRow);
} }
} }
+2 -2
View File
@@ -5,7 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class SetWBoxMenuItem extends JMenuItem public final class SetWBoxMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -21,6 +21,6 @@ public class SetWBoxMenuItem extends JMenuItem
} }
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setWBox(boxLine, boxRow); mazeApp.setModelWBox(boxLine, boxRow);
} }
} }
+1 -1
View File
@@ -4,7 +4,7 @@ import javax.swing.*;
import maze.Maze; import maze.Maze;
import java.awt.*; import java.awt.*;
public class WindowPanel extends JPanel { public final class WindowPanel extends JPanel {
private final MazeDrawing drawingPanelGrid; private final MazeDrawing drawingPanelGrid;