diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 6d7ce44..07f8dcb 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -59,6 +59,32 @@ public class Maze return vertexList ; } + /**Method to determine the departure in the maze. Returns null if the departure is non-existent. + * + * @return Departure vertex + */ + public VertexInterface getDeparture() { + + ArrayList vertexes = getVertexes() ; + for (VertexInterface vertex : vertexes) { + if (((Box) vertex).isDeparture()) return vertex; + } + return null; + } + + /**Method to determine the arrival in the maze. Returns null if the arrival is non-existent. + * + * @return Arrival vertex + */ + public VertexInterface getArrival() { + + ArrayList vertexes = getVertexes() ; + for (VertexInterface vertex : vertexes) { + if (((Box) vertex).isArrival()) return vertex; + } + return null; + } + public ArrayList generateNeighbours(VertexInterface vertex) { Box box = (Box)vertex ; diff --git a/src/model/Box.java b/src/model/Box.java new file mode 100644 index 0000000..c5d1975 --- /dev/null +++ b/src/model/Box.java @@ -0,0 +1,11 @@ +package model; + +import maze.Maze; + +public class Box extends maze.Box { + + public Box(String label, int x, int y, Maze maze) { + super(label, x, y, maze); + } + +} diff --git a/src/model/Maze.java b/src/model/Maze.java new file mode 100644 index 0000000..3bd6162 --- /dev/null +++ b/src/model/Maze.java @@ -0,0 +1,9 @@ +package model; + +public class Maze extends maze.Maze { + + public Maze(String filename) { + super(filename); + } + +} diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 1694450..3b6e9fc 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -1,10 +1,20 @@ package model; -import maze.Maze; +import java.util.ArrayList; +import java.util.Observable; -public class MazeAppModel { +import dijkstra.VertexInterface; +import dijkstra.Dijkstra; - private Maze maze = new Maze("data/labyrinthe.txt"); +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 shortest = previous.getShortestPathTo(arrival); + private ArrayList boxList = maze.getVertexes(); + private Boolean modified = false; public void setMaze(Maze maze) { this.maze = maze; diff --git a/src/model/Previous.java b/src/model/Previous.java new file mode 100644 index 0000000..24d42cf --- /dev/null +++ b/src/model/Previous.java @@ -0,0 +1,14 @@ +package model; + +import java.util.ArrayList; +import java.util.Hashtable; + +import dijkstra.VertexInterface; + +public class Previous extends dijkstra.Previous { + + public Previous(Hashtable hashPrevious, ArrayList shortestPath) { + super(hashPrevious, shortestPath); + } + +} diff --git a/src/ui/ArrivalDrawingBox.java b/src/ui/ArrivalDrawingBox.java index 6677e9a..c8ad9a7 100644 --- a/src/ui/ArrivalDrawingBox.java +++ b/src/ui/ArrivalDrawingBox.java @@ -1,11 +1,18 @@ package ui; import java.awt.Color; +import java.awt.Graphics; public class ArrivalDrawingBox extends DrawingBox { public ArrivalDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } + + @Override + protected final void paintComponent(Graphics g) { + super.paintComponent(g); + setBackground(Color.RED); + } } diff --git a/src/ui/DepartureDrawingBox.java b/src/ui/DepartureDrawingBox.java index 3c21ec1..87278ac 100644 --- a/src/ui/DepartureDrawingBox.java +++ b/src/ui/DepartureDrawingBox.java @@ -1,6 +1,7 @@ package ui; import java.awt.Color; +import java.awt.Graphics; public class DepartureDrawingBox extends DrawingBox { @@ -8,4 +9,10 @@ public class DepartureDrawingBox extends DrawingBox { super(mazeApp, color); } + @Override + protected final void paintComponent(Graphics g) { + + super.paintComponent(g); + setBackground(Color.GREEN); + } } diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java index 2397c7e..33c9e8d 100644 --- a/src/ui/DrawingBox.java +++ b/src/ui/DrawingBox.java @@ -2,6 +2,8 @@ package ui; import java.awt.Color; import java.awt.Dimension; +import java.awt.Graphics; + import javax.swing.JPanel; public abstract class DrawingBox extends JPanel { @@ -17,4 +19,16 @@ public abstract class DrawingBox extends JPanel { setBackground(color); setPreferredSize(new Dimension(40,40)); } + + @Override + protected void paintComponent(Graphics g) { + + super.paintComponent(g) ; + setBackground(color); + } + + public void notifyForUpdates(Object param) { + + repaint(); + } } diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index 4671537..fdf8c1e 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -8,17 +8,19 @@ import maze.Maze; public class DrawingPanelGrid extends JPanel { - private final MazeApp mazeApp; - private Maze maze; - private int dimensionX; - private int dimensionY; + private final MazeApp mazeApp; + private Maze maze; + private DrawingBox[][] drawingBoxMatrix; + private int dimensionX; + private int dimensionY; public DrawingPanelGrid(MazeApp mazeApp) { - this.mazeApp = mazeApp; - this.maze = mazeApp.getMazeAppModel().getMaze(); + this.mazeApp = mazeApp; + this.maze = mazeApp.getMazeAppModel().getMaze(); this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); + this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY]; setLayout(new GridLayout(this.dimensionX, this.dimensionY)); initFromMaze(maze); @@ -28,27 +30,49 @@ public class DrawingPanelGrid extends JPanel { private void initFromMaze(Maze maze) { - int dimensionX = maze.getDimensionX(); - int dimensionY = maze.getDimensionY(); - for (int i = 0; i < dimensionX; i++) { for (int j = 0; j < dimensionY; j++) { if (maze.getMaze()[j][i].isWalkable()) { if (maze.getMaze()[j][i].isDeparture()) { add(new DepartureDrawingBox(mazeApp, Color.GREEN)); + drawingBoxMatrix[j][i] = new DepartureDrawingBox(mazeApp, Color.GREEN); } else if (maze.getMaze()[j][i].isArrival()) { add(new ArrivalDrawingBox(mazeApp, Color.RED)); + drawingBoxMatrix[j][i] = new ArrivalDrawingBox(mazeApp, Color.RED); } else { add(new EmptyDrawingBox(mazeApp, Color.BLACK)); + drawingBoxMatrix[j][i] = new EmptyDrawingBox(mazeApp, Color.BLACK); } } else { add(new WallDrawingBox(mazeApp, Color.WHITE)); + drawingBoxMatrix[j][i] = new WallDrawingBox(mazeApp, Color.WHITE); + } + } + } + } + + public void notifyForUpdates(Object param) { + + for (int i = 0; i < dimensionX; i++) { + for (int j = 0; j < dimensionY; j++) { + if (maze.getMaze()[j][i].isWalkable()) { + if (maze.getMaze()[j][i].isDeparture()) { + drawingBoxMatrix[j][i].notifyForUpdates(); + } + else if (maze.getMaze()[j][i].isArrival()) { + drawingBoxMatrix[j][i].notifyForUpdates(); + } + else { + drawingBoxMatrix[j][i].notifyForUpdates(); + } + } + else { + drawingBoxMatrix[j][i].notifyForUpdates(); } } } - } } diff --git a/src/ui/EmptyDrawingBox.java b/src/ui/EmptyDrawingBox.java index 1b649d3..bbb7d62 100644 --- a/src/ui/EmptyDrawingBox.java +++ b/src/ui/EmptyDrawingBox.java @@ -1,6 +1,7 @@ package ui; import java.awt.Color; +import java.awt.Graphics; public class EmptyDrawingBox extends DrawingBox { @@ -8,4 +9,10 @@ public class EmptyDrawingBox extends DrawingBox { super(mazeApp, color); } + @Override + protected final void paintComponent(Graphics g) { + + super.paintComponent(g); + setBackground(Color.WHITE); + } } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 3dbb3e6..84c95e2 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -4,7 +4,11 @@ import javax.swing.*; import maze.Maze; import model.MazeAppModel; -public class MazeApp extends JFrame { +import java.util.Observable; +import java.util.Observer; + +public class MazeApp extends JFrame + implements Observer { private final MazeMenuBar mazeMenuBar; private final WindowPanel windowPanel; @@ -26,4 +30,10 @@ public class MazeApp extends JFrame { public MazeAppModel getMazeAppModel() { return mazeAppModel; } + + @Override + public void update(Observable obs, Object param) { + + windowPanel.notifyForUpdates(param); + } } diff --git a/src/ui/WallDrawingBox.java b/src/ui/WallDrawingBox.java index 207bc7a..59ef424 100644 --- a/src/ui/WallDrawingBox.java +++ b/src/ui/WallDrawingBox.java @@ -1,6 +1,7 @@ package ui; import java.awt.Color; +import java.awt.Graphics; public class WallDrawingBox extends DrawingBox { @@ -8,4 +9,10 @@ public class WallDrawingBox extends DrawingBox { super(mazeApp, color); } + @Override + protected final void paintComponent(Graphics g) { + + super.paintComponent(g); + setBackground(Color.BLACK); + } } diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 55ec5c0..455933a 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -14,5 +14,10 @@ public class WindowPanel extends JPanel add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); } + + public void notifyForUpdates(Object param) { + + drawingPanelGrid.notifyForUpdates(param); + } }