[WIP] Load maze

This commit is contained in:
antograb committed 2017-12-18 11:47:55 +01:00
1 parent 9aa134eb77
commit d9b52c35e2
6 files changed
+54 -22

No files matched your search

-1
View File
@@ -1,5 +1,4 @@
import java.util.ArrayList;
import model.MazeAppModel;
import dijkstra.*;
import maze.*;
import ui.*;
+25 -5
View File
@@ -29,11 +29,31 @@ public class Maze
}
// public Maze(Box[][] maze){
// //Replacement for setMaze()
// //Todo : check if the given array is a proper maze
// //and then set dimensionX and dimensionX
// }
public Maze(Maze maze){
this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY();
this.maze = new Box[dimensionX][dimensionY];
for (int line = 0; line < dimensionX; line++) {
for (int row = 0; row < dimensionY; row++) {
Box box = maze.getMaze()[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
this.maze[line][row] = new DBox(box.getLabel(), box.getX(), box.getY(), this);
}
else if (box.isArrival()) {
this.maze[line][row] = new ABox(box.getLabel(), box.getX(), box.getY(), this);
}
else {
this.maze[line][row] = new EBox(box.getLabel(), box.getX(), box.getY(), this);
}
}
else {
this.maze[line][row] = new WBox(box.getLabel(), box.getX(), box.getY(), this);
}
}
}
}
public int getCost(VertexInterface start, VertexInterface end) {
+4
View File
@@ -5,4 +5,8 @@ public class Maze extends maze.Maze {
public Maze(String filename) {
super(filename);
}
public Maze(Maze maze) {
super(maze);
}
}
+1 -2
View File
@@ -17,10 +17,9 @@ public class MazeAppModel extends Observable {
private ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
private ArrayList<VertexInterface> boxList = maze.getVertexes();
private Boolean modified = false;
private ArrayList<Observer> listObserver = new ArrayList<Observer>();
public void setMaze(Maze maze) {
this.maze = maze;
this.maze = new Maze(maze);
modified = true;
setChanged();
notifyObservers(maze);
+19 -12
View File
@@ -13,6 +13,7 @@ public class DrawingPanelGrid extends JPanel {
private DrawingBox[][] drawingBoxMatrix;
private int dimensionX;
private int dimensionY;
private GridLayout gridLayout;
public DrawingPanelGrid(MazeApp mazeApp) {
@@ -20,9 +21,10 @@ public class DrawingPanelGrid extends JPanel {
this.maze = mazeApp.getMazeAppModel().getMaze();
this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY();
this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY];
this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX];
this.gridLayout = new GridLayout(this.dimensionY, this.dimensionX); //inverted coordinates
setLayout(new GridLayout(this.dimensionY, this.dimensionX)); //inverted coordinates
setLayout(gridLayout);
initFromMaze(maze);
}
@@ -53,23 +55,28 @@ public class DrawingPanelGrid extends JPanel {
public void notifyForUpdates(Object param) {
if (param instanceof Maze) {
maze = (Maze) param;
this.maze = (Maze) param;
this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY();
this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX];
System.out.println("x : "+dimensionX+" y : "+dimensionY);
this.gridLayout = new GridLayout(dimensionY, dimensionX);
for (int j = 0; j < dimensionX; j++) {
for (int i = 0; i < dimensionY; i++) {
if (maze.getMaze()[j][i].isWalkable()) {
if (maze.getMaze()[j][i].isDeparture()) {
drawingBoxMatrix[j][i].setColor(Color.GREEN);
for (int line = 0; line < dimensionX; line++) {
for (int row = 0; row < dimensionY; row++) {
if (maze.getMaze()[line][row].isWalkable()) {
if (maze.getMaze()[line][row].isDeparture()) {
drawingBoxMatrix[row][line].setColor(Color.GREEN);
}
else if (maze.getMaze()[j][i].isArrival()) {
drawingBoxMatrix[j][i].setColor(Color.RED);
else if (maze.getMaze()[line][row].isArrival()) {
drawingBoxMatrix[row][line].setColor(Color.RED);
}
else {
drawingBoxMatrix[j][i].setColor(Color.BLACK);
drawingBoxMatrix[row][line].setColor(Color.BLACK);
}
}
else {
drawingBoxMatrix[j][i].setColor(Color.WHITE);
drawingBoxMatrix[row][line].setColor(Color.WHITE);
}
}
}
+5 -2
View File
@@ -27,14 +27,17 @@ public class LoadMazeMenuItem extends JMenuItem
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text files", "txt");
FileNameExtensionFilter filter =
new FileNameExtensionFilter("Text files", "txt");
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showOpenDialog(mazeApp);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
fileChooser.getSelectedFile().getName());
}
String filename = fileChooser.getSelectedFile().getPath();
mazeApp.getMazeAppModel().setMaze(new Maze(filename));
}