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:
regar42 committed 2018-01-14 17:05:42 +01:00
1 parent e76db727c7
commit dc8de8c707
11 files changed
+300 -79

No files matched your search

+3
View File
@@ -19,6 +19,9 @@ public interface VertexInterface {
*/
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.
* @param vertex
+51 -13
View File
@@ -3,6 +3,7 @@ import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.ArrayList;
import dijkstra.GraphInterface;
@@ -17,13 +18,14 @@ import dijkstra.VertexInterface;
*
*/
public final class Maze
implements GraphInterface {
implements GraphInterface {
private int dimensionX;
private int dimensionY;
private Box[][] maze;
public Maze(String filename) {
public Maze(String filename)
throws MazeReadingException {
initFromTextFile(filename);
}
@@ -51,6 +53,12 @@ public final class Maze
this.maze = matrixDeepCopy(newMaze);
}
/**
* matrixDeepCopy methods allow to deep copy a matrix of Box
* (as stored in a Maze) or only a part of it inside a new matrix of Box.
* The new matrix is therefore a truly new matrix with new boxes, which
* are not references to a previously used object.
*/
private Box[][] matrixDeepCopy(Box[][] original,
Box[][] destination,
int numberOfLines,
@@ -126,6 +134,7 @@ public final class Maze
return matrixDeepCopy(original, original.length, original[0].length);
}
/** Create a square maze of empty boxes of the given dimension */
public static Maze emptyMaze(int dimension) {
Box[][] maze = new Box[dimension][dimension];
for (int line = 0; line < dimension; line++) {
@@ -136,6 +145,7 @@ public final class Maze
return new Maze(dimension, dimension, maze);
}
/** Create a rectangle maze of empty boxes of the given dimensions */
public static Maze emptyMaze(int numberOfLines, int numberOfRows) {
Box[][] maze = new Box[numberOfLines][numberOfRows];
for (int line = 0; line < numberOfLines; line++) {
@@ -146,6 +156,7 @@ public final class Maze
return new Maze(numberOfLines, numberOfRows, maze);
}
/** Create a square maze of wall boxes of the given dimension */
public static Maze wallMaze(int dimension) {
Box[][] maze = new Box[dimension][dimension];
for (int line = 0; line < dimension; line++) {
@@ -156,6 +167,7 @@ public final class Maze
return new Maze(dimension, dimension, maze);
}
/** Create a rectangle maze of wall boxes of the given dimensions */
public static Maze wallMaze(int numberOfLines, int numberOfRows) {
Box[][] maze = new Box[numberOfLines][numberOfRows];
for (int line = 0; line < numberOfLines; line++) {
@@ -166,6 +178,7 @@ public final class Maze
return new Maze(numberOfLines, numberOfRows, maze);
}
/** Add a column of empty boxes at the given position */
public void addEmptyColumn(int posColumn) {
Box[][] newMaze = matrixDeepCopy(maze,
dimensionY,
@@ -296,24 +309,45 @@ public final class Maze
dimensionY = dimensionY - 1;
}
/** Return the box at given coordinates */
public Box getBox(int boxLine, int boxRow) {
return this.maze[boxLine][boxRow];
}
/**
* Set the box at given coordinates to an arrival box. If an arrival box
* was already in the maze, set it to an empty box.
*/
public void setABox(int boxLine, int boxRow) {
if (getArrival() != null) {
setEBox(((Box) getArrival()).getY(), ((Box) getArrival()).getX());
}
this.maze[boxLine][boxRow] = new ABox("A", boxLine, boxRow, this);
resetNeighbourLists();
}
/**
* Set the box at given coordinates to a departure box. If a departure box
* was already in the maze, set it to an empty box.
*/
public void setDBox(int boxLine, int boxRow) {
if (getDeparture() != null) {
setEBox(((Box) getDeparture()).getY(), ((Box) getDeparture()).getX());
}
this.maze[boxLine][boxRow] = new DBox("D", boxLine, boxRow, this);
resetNeighbourLists();
}
/** Set the box at given coordinates to an empty box */
public void setEBox(int boxLine, int boxRow) {
this.maze[boxLine][boxRow] = new EBox("E", boxLine, boxRow, this);
resetNeighbourLists();
}
/** Set the box at given coordinates to a wall box */
public void setWBox(int boxLine, int boxRow) {
this.maze[boxLine][boxRow] = new WBox("W", boxLine, boxRow, this);
resetNeighbourLists();
}
public int getCost(VertexInterface start, VertexInterface end) {
@@ -403,8 +437,8 @@ public final class Maze
* @param filename
* The path to the file containing the representation of the maze.
*/
private void initFromTextFile(String filename) {
private void initFromTextFile(String filename)
throws MazeReadingException {
FileReader fr = null;
BufferedReader br = null;
@@ -474,16 +508,19 @@ public final class Maze
currentLine = br.readLine();
compteurLigne++;
}
} catch (MazeReadingException mre) {
mre.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} catch (IOException e) {
throw new MazeReadingException(filename);
} finally {
try { br.close(); } catch (Exception e) {}
}
}
public void saveToTextFile(String fileName) {
/**
* Save the current maze to a text file.
* @param fileName The name of the file in which the maze will be saved
*/
public void saveToTextFile(String fileName)
throws MazeSavingException {
FileOutputStream fos = null;
PrintWriter pw = null;
@@ -499,9 +536,8 @@ public final class Maze
}
pw.print("\n");
}
} catch(Exception e) {
e.printStackTrace();
} catch(IOException e) {
throw new MazeSavingException(fileName);
} finally {
try { pw.close(); } catch(Exception e) {}
}
@@ -519,10 +555,12 @@ public final class Maze
return mazeStr;
}
/** Gives the number of rows in the maze */
public int getDimensionX() {
return dimensionX;
}
/** Gives the number of lines in the maze */
public int getDimensionY() {
return dimensionY;
}
@@ -531,7 +569,7 @@ public final class Maze
return maze;
}
public void resetNeighbourLists() {
private void resetNeighbourLists() {
for (Box[] line: maze) {
for (Box box: line) {
box.resetNeighbourList();
+2 -5
View File
@@ -4,16 +4,13 @@ package maze;
* <p>
* This class is used to throw exception when reading and
* initiating the maze from a text file.
*
*
*/
public final class MazeReadingException
extends Exception {
private static final long serialVersionUID = 1L;
public MazeReadingException (String filename) {
super("Invalid maze format : " + filename);
public MazeReadingException(String filename) {
super("Invalid maze format : " + filename + " or can't open file.");
}
}
+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 + ".");
}
}
+55 -31
View File
@@ -8,9 +8,14 @@ import java.util.Observable;
import java.util.Observer;
import dijkstra.*;
import maze.*;
import maze.Maze;
import maze.Box;
import maze.MazeReadingException;
import maze.MazeSavingException;
public class MazeAppModel extends Observable {
public class MazeAppModel
extends Observable
implements MazeModelInterface {
/** The file into which the maze is to be saved */
private String filename;
private Maze maze;
@@ -43,7 +48,7 @@ public class MazeAppModel extends Observable {
}
else {
previous = (Previous) Dijkstra.dijkstra(maze, departure);
if (((Box) arrival).getNeighbourList() == null) {
if (arrival.getNeighbourList() == null) {
shortest = null;
}
else {
@@ -52,9 +57,11 @@ public class MazeAppModel extends Observable {
}
}
public void loadMaze(String filename) {
this.filename = new String(filename);
maze = new Maze(this.filename);
/** Load a new maze from a file and send a notification to the view */
public void loadMaze(String filename)
throws MazeReadingException {
maze = new Maze(filename);
this.filename = filename;
initModelFromMaze();
saved = true;
setChanged();
@@ -65,53 +72,61 @@ public class MazeAppModel extends Observable {
return maze;
}
/** Return the box at given coordinates */
public Box getBox(int boxLine, int boxRow) {
return this.maze.getBox(boxLine, boxRow);
}
/** Return the name of the file where the maze is currently being saved */
public String getFilename() {
return filename;
}
/** Tell whether the current maze has been saved or not */
public boolean isSaved() {
return saved;
}
/** Return the Dijkstra shortest path from departure to arrival */
public ArrayList<VertexInterface> getShortest() {
return shortest;
}
public ArrayList<VertexInterface> getShortestShallowCopy() {
ArrayList<VertexInterface> shallowCopy = new ArrayList<VertexInterface>();
for (VertexInterface vertex : this.shortest) {
shallowCopy.add(vertex);
}
return shallowCopy;
}
/** Indicate whether the path is drawn, currently being drawn, or neither */
public int isPathDrawn() {
return pathDrawn;
}
/** Set the state of the path drawing :
* 0 if not drawn
* 1 if being animated
* 2 if completely drawn
*/
public void setPathDrawn(int pathDrawn) {
this.pathDrawn = pathDrawn;
}
public void saveToFile() {
/**
* Save the maze to the current file being used and notify observers
* (such as the "Save" button)
*/
public void saveToFile() throws MazeSavingException {
maze.saveToTextFile(filename);
saved = true;
setChanged();
notifyObservers(MazeAppModelMessage.MazeSaved);
}
public void saveToFile(String filename) {
/** Save the maze to the given file and set it to be the current file */
public void saveToFile(String filename) throws MazeSavingException {
maze.saveToTextFile(filename);
this.filename = filename;
maze.saveToTextFile(this.filename);
saved = true;
setChanged();
notifyObservers(MazeAppModelMessage.FileChange);
}
/** Expose a way to toggle a box's type given its coordinates */
public void toggleBox(int boxLine, int boxRow) {
if (maze.getBox(boxLine, boxRow).isWalkable()) {
setWBox(boxLine, boxRow);
@@ -121,52 +136,46 @@ public class MazeAppModel extends Observable {
}
}
/** Set the box at given coordinates to be a wall box */
public void setWBox(int boxLine, int boxRow) {
maze.setWBox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/** Set the box at given coordinates to be an empty box */
public void setEBox(int boxLine, int boxRow) {
maze.setEBox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/** Set the box at given coordinates to be a departure box */
public void setDBox(int boxLine, int boxRow) {
if (departure != null) {
int departureRow = ((EBox) departure).getX();
int departureLine = ((EBox) departure).getY();
maze.setEBox(departureLine, departureRow);
}
maze.setDBox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/** Set the box at given coordinates to be an arrival box */
public void setABox(int boxLine, int boxRow) {
if (arrival != null) {
int arrivalRow = ((EBox) arrival).getX();
int arrivalLine = ((EBox) arrival).getY();
maze.setEBox(arrivalLine, arrivalRow);
}
maze.setABox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/**
* Set the maze to be a new clear (full of empty boxes) maze of given
* dimensions
*/
public void newClearMaze(int numberOfLines, int numberOfRows) {
maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows));
@@ -176,10 +185,12 @@ public class MazeAppModel extends Observable {
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/** Add an empty column at the end of the maze */
public void addEmptyColumn() {
addEmptyColumn(maze.getDimensionX());
}
/** Add an empty column to the maze at the given position */
public void addEmptyColumn(int posColumn) {
maze.addEmptyColumn(posColumn);
initModelFromMaze();
@@ -188,6 +199,7 @@ public class MazeAppModel extends Observable {
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/** Delete a column from the maze at the given position */
public void delColumn(int posColumn) {
maze.delColumn(posColumn);
initModelFromMaze();
@@ -196,14 +208,17 @@ public class MazeAppModel extends Observable {
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/** Delete the last column of the maze */
public void delColumn() {
delColumn(maze.getDimensionX() - 1);
}
/** Add an empty line at the end of the maze */
public void addEmptyLine() {
addEmptyLine(maze.getDimensionY());
}
/** Add an empty line at the given position */
public void addEmptyLine(int posLine) {
maze.addEmptyLine(posLine);
initModelFromMaze();
@@ -212,6 +227,7 @@ public class MazeAppModel extends Observable {
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/** Delete a line from the maze at the given position */
public void delLine(int posLine) {
maze.delLine(posLine);
initModelFromMaze();
@@ -220,22 +236,30 @@ public class MazeAppModel extends Observable {
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
/** Delete the last line of the maze */
public void delLine() {
delLine(maze.getDimensionY() - 1);
}
/**
* Get the departure box from the maze
* @return The departure box from the maze, if one exists, else null
*/
public VertexInterface getDeparture() {
return departure;
}
/** Clear the maze : create a new one of the current dimensions */
public void clearMaze() {
newClearMaze(maze.getDimensionY(), maze.getDimensionX());
}
/** Get the number of rows in the maze */
public int getDimensionX() {
return maze.getDimensionX();
}
/** Get the number of lines in the maze */
public int getDimensionY() {
return maze.getDimensionY();
}
+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();
}
-4
View File
@@ -13,8 +13,6 @@ public final class FileBox {
int returnVal = fileChooser.showOpenDialog(mazeApp);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
fileChooser.getSelectedFile().getName());
String filename = fileChooser.getSelectedFile().getPath();
return filename;
}
@@ -26,8 +24,6 @@ public final class FileBox {
int returnVal = fileChooser.showSaveDialog(mazeApp);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to save to this file: " +
fileChooser.getSelectedFile().getName());
String filename = fileChooser.getSelectedFile().getPath();
return filename;
}
+32 -4
View File
@@ -10,6 +10,8 @@ import java.util.Observable;
import javax.swing.JOptionPane;
import model.MazeAppModelMessage;
import maze.MazeReadingException;
import maze.MazeSavingException;
public final class FileMenu extends JMenu {
@@ -53,7 +55,17 @@ public final class FileMenu extends JMenu {
}
String filename = FileBox.load(mazeApp);
if (filename != null) {
mazeApp.loadModelMaze(filename);
try {
mazeApp.loadModelMaze(filename);
}
catch (MazeReadingException mre) {
JOptionPane.showMessageDialog(this,
"Please check you have sufficient permissions to"
+ " read the file and that the file has a correct"
+ " maze format.",
"Error loading maze",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
@@ -70,7 +82,7 @@ public final class FileMenu extends JMenu {
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control S"));
mazeApp.getMazeAppModel().addObserver(this);
if (mazeApp.getMazeAppModel().getFilename() == null) {
if (mazeApp.getModelFilename() == null) {
this.setEnabled(false);
}
addActionListener(this);
@@ -78,7 +90,15 @@ public final class FileMenu extends JMenu {
public void actionPerformed(ActionEvent evt) {
if (mazeApp.getModelFilename() != null) {
mazeApp.saveModelMaze();
try {
mazeApp.saveModelMaze();
}
catch (MazeSavingException mse) {
JOptionPane.showMessageDialog(this,
mse.getMessage(),
"Maze saving error",
JOptionPane.ERROR_MESSAGE);
}
}
}
@@ -111,7 +131,15 @@ public final class FileMenu extends JMenu {
public void actionPerformed(ActionEvent evt) {
String filename = FileBox.save(mazeApp);
if (filename != null) {
mazeApp.saveModelMaze(filename);
try {
mazeApp.saveModelMaze(filename);
}
catch (MazeSavingException mse) {
JOptionPane.showMessageDialog(this,
mse.getMessage(),
"Maze saving error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
+41 -8
View File
@@ -6,6 +6,8 @@ import dijkstra.VertexInterface;
import maze.Maze;
import model.MazeAppModel;
import maze.EBox;
import maze.MazeReadingException;
import maze.MazeSavingException;
import java.util.ArrayList;
import java.util.Observable;
@@ -43,10 +45,13 @@ public final class MazeApp extends JFrame
@Override
public void update(Observable obs, Object param) {
System.out.println("New maze detected");
windowPanel.notifyForUpdates(param);
}
/**
* Exit the application, asking if the user wants to save an unsaved
* maze or not
*/
public void windowClosing(WindowEvent e) {
if (! mazeAppModel.isSaved()) {
if (! SaveBox.promptAndContinue(null, this, "Quit application")) {
@@ -80,127 +85,155 @@ public final class MazeApp extends JFrame
return;
}
/** Draw the full path and update its state in the model */
public void drawShortestPath() {
windowPanel.paintShortestPath();
mazeAppModel.setPathDrawn(2);
}
/**
* Trigger the animation of path drawing and update its state in
* the model
*/
public void animateShortestPath() {
windowPanel.animateShortestPath();
mazeAppModel.setPathDrawn(1);
}
/** Clear the shortest path drawn and update its state in the model */
public void clearShortestPath() {
windowPanel.clearShortestPath();
mazeAppModel.setPathDrawn(0);
this.repaint();
}
/** @see model.MazeAppModel */
public void addEmptyColumn(int posColumn) {
mazeAppModel.addEmptyColumn(posColumn);
}
/** @see model.MazeAppModel */
public void addEmptyColumn() {
mazeAppModel.addEmptyColumn();
}
/** @see model.MazeAppModel */
public void delColumn(int posColumn) {
mazeAppModel.delColumn(posColumn);
}
/** @see model.MazeAppModel */
public void delColumn() {
mazeAppModel.delColumn();
}
/** @see model.MazeAppModel */
public void addEmptyLine(int posLine) {
mazeAppModel.addEmptyLine(posLine);
}
/** @see model.MazeAppModel */
public void addEmptyLine() {
mazeAppModel.addEmptyLine();
}
/** @see model.MazeAppModel */
public void delLine(int posLine) {
mazeAppModel.delLine(posLine);
}
/** @see model.MazeAppModel */
public void delLine() {
mazeAppModel.delLine();
}
/** @see model.MazeAppModel */
public void clearMaze() {
mazeAppModel.clearMaze();
this.repaint();
}
/** @see model.MazeAppModel */
public boolean isModelSaved( ) {
return this.mazeAppModel.isSaved();
}
public void loadModelMaze(String filename) {
/** @see model.MazeAppModel */
public void loadModelMaze(String filename) throws MazeReadingException {
this.mazeAppModel.loadMaze(filename);
}
/** @see model.MazeAppModel */
public maze.Box getModelBox(int boxLine, int boxRow) {
return this.mazeAppModel.getBox(boxLine, boxRow);
}
public void saveModelMaze(String filename) {
/** @see model.MazeAppModel */
public void saveModelMaze(String filename) throws MazeSavingException {
mazeAppModel.saveToFile(filename);
}
public void saveModelMaze() {
/** @see model.MazeAppModel */
public void saveModelMaze() throws MazeSavingException {
mazeAppModel.saveToFile();
}
/** @see model.MazeAppModel */
public String getModelFilename() {
return mazeAppModel.getFilename();
}
/** @see model.MazeAppModel */
public void setModelABox(int boxLine, int boxRow) {
this.mazeAppModel.setABox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public void setModelDBox(int boxLine, int boxRow) {
this.mazeAppModel.setDBox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public void setModelEBox(int boxLine, int boxRow) {
this.mazeAppModel.setEBox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public void setModelWBox(int boxLine, int boxRow) {
this.mazeAppModel.setWBox(boxLine, boxRow);
}
/** @see model.MazeAppModel */
public int isPathDrawn() {
return this.mazeAppModel.isPathDrawn();
}
/** @see model.MazeAppModel */
public Maze getModelMaze() {
return this.mazeAppModel.getMaze();
}
/** @see model.MazeAppModel */
public ArrayList<VertexInterface> getModelShortest() {
return this.mazeAppModel.getShortest();
}
public ArrayList<VertexInterface> getModelShortestShallowCopy() {
return this.mazeAppModel.getShortestShallowCopy();
}
/** @see model.MazeAppModel */
public int getModelDimensionX() {
return this.mazeAppModel.getDimensionX();
}
/** @see model.MazeAppModel */
public int getModelDimensionY() {
return this.mazeAppModel.getDimensionY();
}
/** @see model.MazeAppModel */
public void setPathDrawn(int i) {
this.mazeAppModel.setPathDrawn(i);
}
/** @see model.MazeAppModel */
public void toggleModelBox(int boxLine, int boxRow) {
this.mazeAppModel.toggleBox(boxLine, boxRow);
}
+49 -12
View File
@@ -15,13 +15,12 @@ import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import maze.Maze;
import model.MazeAppModelMessage;
import maze.EBox;
import maze.Box;
import dijkstra.VertexInterface;
public final class MazeDrawing extends JPanel implements MouseListener {
private final MazeApp mazeApp;
private boolean pathDrawingMode;
private ArrayList<VertexInterface> shortestPath;
private ArrayList<VertexInterface> finalPath;
private int pathCounter;
@@ -29,7 +28,6 @@ public final class MazeDrawing extends JPanel implements MouseListener {
public MazeDrawing(MazeApp mazeApp) {
this.mazeApp = mazeApp;
this.pathDrawingMode = false;
this.shortestPath = new ArrayList<VertexInterface>();
this.finalPath = null;
this.pathCounter = -1;
@@ -55,6 +53,7 @@ public final class MazeDrawing extends JPanel implements MouseListener {
}
}
/** Paint (each box of) the maze in the current JPanel */
private void paintMaze(Graphics2D g, int width, int height) {
Maze maze = mazeApp.getModelMaze();
@@ -88,19 +87,31 @@ public final class MazeDrawing extends JPanel implements MouseListener {
}
}
/**
* This method is responsible for repainting the maze when needed
* @param param
* The message about what happened to the maze.
* The maze will be re-painted only if it changed
* (by user edition or by loading a new maze from file).
* @see model.MazeAppModelMessage
*/
public void notifyForUpdates(Object param) {
if (param == MazeAppModelMessage.MazeRenewal ||
param == MazeAppModelMessage.MazeLoaded) {
if (this.mazeApp.getModelShortest() != null) {
this.finalPath
= this.mazeApp.getModelShortestShallowCopy();
finalPath = new ArrayList<VertexInterface>(mazeApp.getModelShortest());
Collections.reverse(finalPath);
}
this.repaint();
}
}
/**
* This method is reponsible for painting the shortest path on top
* of the maze. It takes all the boxes belonging to the shortest path
* and display on dot on them all at once.
*/
private void paintShortestPath(Graphics2D g, int width, int height) {
g.setColor(Color.WHITE);
@@ -126,7 +137,7 @@ public final class MazeDrawing extends JPanel implements MouseListener {
return;
}
for (VertexInterface vertex : shortest) {
EBox box = (EBox) vertex;
Box box = (Box) vertex;
g.fillOval(box.getX() * boxWidth + factorWidth,
box.getY()*boxHeight + factorHeight,
boxWidth - 2 * factorWidth,
@@ -134,6 +145,11 @@ public final class MazeDrawing extends JPanel implements MouseListener {
}
}
/**
* This method displays the next box in the shortest path at each call.
* When the last box in the shortest path has been displayed, it falls
* back to calling drawShortestPath.
*/
private void animateShortestPath(Graphics2D g, int width, int height) {
g.setColor(Color.WHITE);
@@ -158,8 +174,6 @@ public final class MazeDrawing extends JPanel implements MouseListener {
return;
}
pathDrawingMode = true;
if (pathCounter == -1) {
JOptionPane.showMessageDialog(this,
"Click on the maze to see the shortest path evolve");
@@ -167,7 +181,7 @@ public final class MazeDrawing extends JPanel implements MouseListener {
}
for (VertexInterface vertex : shortestPath) {
EBox box = (EBox) vertex;
Box box = (Box) vertex;
g.fillOval(box.getX() * boxWidth + factorWidth,
box.getY() * boxHeight + factorHeight,
boxWidth - 2 * factorWidth,
@@ -175,15 +189,30 @@ public final class MazeDrawing extends JPanel implements MouseListener {
}
if (pathCounter >= finalPath.size()) {
pathDrawingMode = false;
mazeApp.setPathDrawn(2);
}
}
/**
* The method called when the user clicked on the panel.
* It will trigger events such as box edition or shortest path animation.
* @param e
* If in animation mode, trigger the displaying of the next box
* in the shortest pas. If not, catch right clicks to display a context menu
* and left clicks to toggle boxes type.
*/
public void mouseClicked(MouseEvent e) {
int boxRow = getBoxRow(e.getX());
int boxLine = getBoxLine(e.getY());
if (! pathDrawingMode) {
if (mazeApp.isPathDrawn() == 0) {
// Don't do anything if there is no box where the mouse is
if (boxRow >= mazeApp.getModelDimensionX()
|| boxLine >= mazeApp.getModelDimensionY()) {
return;
}
// Toggle box's type if the user clicked the left button,
// print a menu allowing to choose between different actions
// if the user clicked the right button.
if (e.getButton() == MouseEvent.BUTTON3) {
BoxTypeEditionMenu boxTypeEditionMenu
= new BoxTypeEditionMenu(mazeApp, boxLine, boxRow);
@@ -213,12 +242,20 @@ public final class MazeDrawing extends JPanel implements MouseListener {
return;
}
/**
* Reponsible for calculating the row number of a box
* based on the panel size and the box's width
*/
private int getBoxRow(int x) {
int dimensionX = mazeApp.getModelDimensionX();
int boxWidth = getWidth() / dimensionX;
return x / boxWidth;
}
/**
* Reponsible for calculating the line number of a box
* based on the panel size and the box's height
*/
private int getBoxLine(int y) {
int dimensionY = mazeApp.getModelDimensionY();
int boxHeight = getHeight() / dimensionY;
@@ -237,8 +274,8 @@ public final class MazeDrawing extends JPanel implements MouseListener {
return;
}
/** Clear the list of boxes in the local shortest path */
public void clearShortestPath() {
this.pathCounter = -1;
this.shortestPath = new ArrayList<VertexInterface>();
}
}
+21 -2
View File
@@ -2,6 +2,7 @@ package ui;
import javax.swing.JOptionPane;
import java.awt.Component;
import maze.MazeSavingException;
public final class SaveBox {
private static String message = "Maze not saved. Save it ?";
@@ -20,12 +21,30 @@ public final class SaveBox {
return false;
case JOptionPane.OK_OPTION:
if (mazeApp.getModelFilename() != null) {
mazeApp.saveModelMaze();
try {
mazeApp.saveModelMaze();
}
catch (MazeSavingException mse) {
JOptionPane.showMessageDialog(null,
mse.getMessage(),
"Maze saving error",
JOptionPane.ERROR_MESSAGE);
return promptAndContinue(null, mazeApp, title);
}
}
else {
String filename = FileBox.save(mazeApp);
if (filename != null) {
mazeApp.saveModelMaze(filename);
try {
mazeApp.saveModelMaze(filename);
}
catch (MazeSavingException mse) {
JOptionPane.showMessageDialog(null,
mse.getMessage(),
"Maze saving error",
JOptionPane.ERROR_MESSAGE);
return promptAndContinue(null, mazeApp, title);
}
}
else {
return promptAndContinue(null, mazeApp, title);