diff --git a/src/MainTest.java b/src/MainTest.java index 61a3012..26010af 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,5 +1,4 @@ import java.util.ArrayList; -import model.MazeAppModel; import dijkstra.*; import maze.*; import ui.*; diff --git a/src/maze/Maze.java b/src/maze/Maze.java index e08996e..b81d89c 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -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) { diff --git a/src/model/Maze.java b/src/model/Maze.java index adbc938..9afdc55 100644 --- a/src/model/Maze.java +++ b/src/model/Maze.java @@ -5,4 +5,8 @@ public class Maze extends maze.Maze { public Maze(String filename) { super(filename); } + + public Maze(Maze maze) { + super(maze); + } } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 3ac826e..a347261 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -17,10 +17,9 @@ public class MazeAppModel extends Observable { private ArrayList shortest = previous.getShortestPathTo(arrival); private ArrayList boxList = maze.getVertexes(); private Boolean modified = false; - private ArrayList listObserver = new ArrayList(); public void setMaze(Maze maze) { - this.maze = maze; + this.maze = new Maze(maze); modified = true; setChanged(); notifyObservers(maze); diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index f37d0ea..0bb3bdb 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -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); } } } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 9724fde..71bfc89 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -27,15 +27,18 @@ 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)); + 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)); } }