Open app on empty maze and improve load/save
model: * Add a filename attribute to MazeAppModel to keep track of the file currently being used by the app * Add saveToFile(filename) to save the maze into a specific file * Rewrite saveToFile() to use the stored filename attribute to know into which file is to be used to save the maze, instead of the hardcoded path * Remove unused models * MazeAppModel is now initialized with an empty maze, instead of a maze read from a hardcoded path (see #13) * Add a MazeAppModelMessage class to pass proper messages to observers * Replace setMaze(maze) by loadMaze(filename) for now * Move paintMaze into ui/MazeApp ui: * Add a Save As button (just a new name for the previous Save button) and implement an actual Save button (which saves into the file currently being used), disabled when no file is currently in use. (see #16) * Create the FileBox class to handle file choices for the load, save, save as buttons * Add keystrokes to trigger Load, Save or Save As actions from the menu maze: * Add a method to Maze to perform a deep copy of a matrix of boxes * Add a static method to generate an empty maze of a fixed size (10*10) This behavior might change in the future * Add a constructor to make a Maze from a matrix of boxes
This commit is contained in:
12 files changed
+232
-119
No files matched your search
+34
-41
@@ -10,67 +10,60 @@ import java.util.Observer;
|
||||
import dijkstra.VertexInterface;
|
||||
import dijkstra.Dijkstra;
|
||||
import dijkstra.Previous;
|
||||
import maze.Maze;
|
||||
|
||||
public class MazeAppModel extends Observable {
|
||||
|
||||
private Maze maze = new Maze("data/labyrinthe.txt");
|
||||
private VertexInterface departure = maze.getDeparture();
|
||||
private VertexInterface arrival = maze.getArrival();
|
||||
private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
||||
private ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
|
||||
private ArrayList<VertexInterface> boxList = maze.getVertexes();
|
||||
private String filename;
|
||||
private Maze maze;
|
||||
private VertexInterface departure;
|
||||
private VertexInterface arrival;
|
||||
private Previous previous;
|
||||
private ArrayList<VertexInterface> shortest;
|
||||
private ArrayList<VertexInterface> boxList;
|
||||
private Boolean modified = false;
|
||||
|
||||
public void setMaze(Maze maze) {
|
||||
this.maze = new Maze(maze);
|
||||
public MazeAppModel() {
|
||||
maze = new Maze(Maze.emptySquareMaze());
|
||||
departure = maze.getDeparture();
|
||||
arrival = maze.getArrival();
|
||||
boxList = maze.getVertexes();
|
||||
if (departure == null || arrival == null) {
|
||||
return;
|
||||
}
|
||||
previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
||||
shortest = previous.getShortestPathTo(arrival);
|
||||
}
|
||||
|
||||
public void loadMaze(String filename) {
|
||||
this.filename = new String(filename);
|
||||
maze = new Maze(this.filename);
|
||||
modified = true;
|
||||
setChanged();
|
||||
notifyObservers(maze);
|
||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||
}
|
||||
|
||||
public Maze getMaze() {
|
||||
return maze;
|
||||
}
|
||||
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
public boolean isModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
public void saveToFile() {
|
||||
maze.saveToTextFile("data/labyOutput.txt");
|
||||
}
|
||||
|
||||
public void saveToFile(String filename) {
|
||||
maze.saveToTextFile(filename);
|
||||
}
|
||||
|
||||
public void paintMaze(Graphics2D g, int width, int height) {
|
||||
|
||||
int dimensionX = maze.getDimensionX();
|
||||
int dimensionY = maze.getDimensionY();
|
||||
int boxWidth = width/dimensionX;
|
||||
int boxHeight = height/dimensionY;
|
||||
|
||||
for (int line = 0; line < dimensionY; line++) {
|
||||
for (int row = 0; row < dimensionX; row++) {
|
||||
if (maze.getMaze()[line][row].isWalkable()) {
|
||||
if (maze.getMaze()[line][row].isDeparture()) {
|
||||
g.setColor(Color.GREEN);
|
||||
}
|
||||
else if (maze.getMaze()[line][row].isArrival()) {
|
||||
g.setColor(Color.RED);
|
||||
}
|
||||
else {
|
||||
g.setColor(Color.BLACK);
|
||||
}
|
||||
}
|
||||
else {
|
||||
g.setColor(Color.WHITE);
|
||||
}
|
||||
g.fill3DRect(row*boxWidth, line*boxHeight,
|
||||
boxWidth, boxHeight,
|
||||
true);
|
||||
}
|
||||
}
|
||||
public void saveToFile(String filename) {
|
||||
this.filename = new String(filename);
|
||||
maze.saveToTextFile(this.filename);
|
||||
modified = true;
|
||||
setChanged();
|
||||
notifyObservers(MazeAppModelMessage.FileChange);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user