diff --git a/src/MainTest.java b/src/MainTest.java index 66cdb93..06b5dd6 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,4 +1,5 @@ import java.util.ArrayList; + import dijkstra.*; import maze.*; import ui.*; diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index 5dc2371..0b907ce 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -27,10 +27,11 @@ public class Dijkstra { if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) { pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; previous.setPrevious(pivot, vertex) ; + pivot.addCandidate(vertex); } } } - + int piMax = Integer.MAX_VALUE ; for (VertexInterface vertex: vertexList) { if (! a.contains(vertex) && pi.getPi(vertex) < piMax) { diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 9f321e8..15f2280 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -2,6 +2,9 @@ package dijkstra; import java.util.ArrayList; import java.util.Hashtable; +import java.util.List; + +import maze.Box; public class Previous implements PreviousInterface { @@ -17,13 +20,11 @@ public class Previous implements PreviousInterface { public VertexInterface getPrevious(VertexInterface vertex) { return hashPrevious.get(vertex) ; - } public void setPrevious(VertexInterface father, VertexInterface son) { hashPrevious.put(son, father) ; - } public ArrayList getShortestPathTo(VertexInterface vertex) { @@ -31,7 +32,7 @@ public class Previous implements PreviousInterface { VertexInterface father = getPrevious(vertex) ; shortestPath.add(father) ; - if (getPrevious(father) == null) return shortestPath ; + if (((Box) getPrevious(father)).isDeparture()) return shortestPath ; else { return getShortestPathTo(father) ; } diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index 32e1e44..9496ecb 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -18,4 +18,16 @@ public interface VertexInterface { * @return The neighbourList of the element. */ public ArrayList generateNeighbours(); + + /** + * Adds all vertexes not in the ASet and candidate for an eventual path. + * @param vertex + */ + public void addCandidate(VertexInterface vertex); + + /** + * Returns the list of the candidates for an eventual path. + * @return + */ + public ArrayList getCandidates(); } diff --git a/src/maze/Box.java b/src/maze/Box.java index 0c17d17..e3b982e 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -23,6 +23,7 @@ public abstract class Box private String label = null ; private Maze maze ; private ArrayList neighbourList; + private ArrayList candidates; public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation @@ -30,14 +31,22 @@ public abstract class Box this.x = x ; this.y = y ; this.maze = maze ; - + this.candidates = new ArrayList(); } public ArrayList generateNeighbours() { this.neighbourList = maze.generateNeighbours(this); return maze.generateNeighbours(this) ; } - + + public void addCandidate(VertexInterface vertex) { + candidates.add(vertex); + } + + public ArrayList getCandidates() { + return candidates; + } + public boolean isWalkable() { return true ; } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 027064e..fba34aa 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -19,8 +19,16 @@ public class MazeAppModel extends Observable { private Previous previous; private ArrayList shortest; private ArrayList boxList; - private boolean saved = true; - private boolean pathDrawn = false; + private boolean saved = true; + /* + * There are three states for pathDrawn : + * 0 : path isn't drawn + * 1 : path has been animated + * 2 : path has already been animated, but needs to be re-drawn + * because of window resizing. Therefore there is no need to + * animate the path again + */ + private int pathDrawn = 0; public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); @@ -66,11 +74,19 @@ public class MazeAppModel extends Observable { return shortest; } - public boolean isPathDrawn() { + public ArrayList getShortestDeepCopy() { + ArrayList deepCopy = new ArrayList(); + for (VertexInterface vertex : this.shortest) { + deepCopy.add(vertex); + } + return deepCopy; + } + + public int isPathDrawn() { return pathDrawn; } - public void setPathDrawn(boolean pathDrawn) { + public void setPathDrawn(int pathDrawn) { this.pathDrawn = pathDrawn; } @@ -132,6 +148,10 @@ public class MazeAppModel extends Observable { saved = false; } + public VertexInterface getDeparture() { + return departure; + } + public void newClearMaze(int numberOfLines, int numberOfRows) { Box[][] newClearMaze = new Box[numberOfLines][numberOfRows]; diff --git a/src/ui/AnimatePathMenuItem.java b/src/ui/AnimatePathMenuItem.java new file mode 100644 index 0000000..f1f2f08 --- /dev/null +++ b/src/ui/AnimatePathMenuItem.java @@ -0,0 +1,27 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.Action; +import javax.swing.Icon; +import javax.swing.JMenuItem; + +public class AnimatePathMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public AnimatePathMenuItem(MazeApp mazeApp) { + + super("Animate shortest path"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent arg0) { + + mazeApp.animateShortestPath(); + } +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 48c54c5..bf01f0a 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -80,11 +80,18 @@ public class MazeApp extends JFrame public void drawShortestPath() { windowPanel.paintShortestPath(); - mazeAppModel.setPathDrawn(true); + mazeAppModel.setPathDrawn(2); } public void clearShortestPath() { - mazeAppModel.setPathDrawn(false); + windowPanel.clearShortestPath(); + mazeAppModel.setPathDrawn(0); this.repaint(); } + + public void animateShortestPath() { + + windowPanel.animateShortestPath(); + mazeAppModel.setPathDrawn(1); + } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index f32cae6..aea6f9e 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -5,7 +5,10 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseListener; +import java.util.Collections; import javax.swing.JPanel; import javax.swing.JOptionPane; import java.awt.event.MouseListener; @@ -18,10 +21,18 @@ import dijkstra.VertexInterface; public class MazeDrawing extends JPanel implements MouseListener { private final MazeApp mazeApp; + private boolean pathDrawingMode; + private ArrayList shortestPath; + private ArrayList finalPath; + private int pathCounter; public MazeDrawing(MazeApp mazeApp) { this.mazeApp = mazeApp; + this.pathDrawingMode = false; + this.shortestPath = new ArrayList(); + this.finalPath = null; + this.pathCounter = -1; setBackground(Color.BLACK); setPreferredSize(new Dimension(500,500)); addMouseListener(this); @@ -37,7 +48,9 @@ public class MazeDrawing extends JPanel implements MouseListener { int height = getHeight(); paintMaze(g2d, width, height); - if (mazeApp.getMazeAppModel().isPathDrawn()) { + if (mazeApp.getMazeAppModel().isPathDrawn() == 1) { + animateShortestPath(g2d, width, height); + } else if (mazeApp.getMazeAppModel().isPathDrawn() == 2) { paintShortestPath(g2d, width, height); } } @@ -77,20 +90,27 @@ public class MazeDrawing extends JPanel implements MouseListener { public void notifyForUpdates(Object param) { if (param == MazeAppModelMessage.MazeRenewal) { + if (this.mazeApp.getMazeAppModel().getShortest() != null) { + this.finalPath + = this.mazeApp.getMazeAppModel().getShortestDeepCopy(); + Collections.reverse(finalPath); + } this.repaint(); } } private void paintShortestPath(Graphics2D g, int width, int height) { - g.setColor(Color.ORANGE); + g.setColor(Color.WHITE); int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); int boxWidth = width / dimensionX; int boxHeight = height / dimensionY; + int factorWidth = boxWidth / 3; + int factorHeight = boxHeight / 3; ArrayList shortest = mazeApp.getMazeAppModel().getShortest(); - if (shortest == null && ! mazeApp.getMazeAppModel().isPathDrawn()) { + if (shortest == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { mazeApp.clearShortestPath(); JOptionPane.showMessageDialog(this, "Check that your graph contains a departure and an arrival," @@ -105,14 +125,107 @@ public class MazeDrawing extends JPanel implements MouseListener { } for (VertexInterface vertex : shortest) { EBox box = (EBox) vertex; - g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight); + g.fillOval(box.getX() * boxWidth + factorWidth, + box.getY()*boxHeight + factorHeight, + boxWidth - 2 * factorWidth, + boxHeight - 2 * factorHeight); } } + private void animateShortestPath(Graphics2D g, int width, int height) { + + g.setColor(Color.WHITE); + int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); + int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); + int boxWidth = width / dimensionX; + int boxHeight = height / dimensionY; + int factorWidth = boxWidth / 3; + int factorHeight = boxHeight / 3; + + if (this.finalPath == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { + mazeApp.clearShortestPath(); + JOptionPane.showMessageDialog(this, + "Check that your graph contains a departure and an arrival," + + " and that a path exists between them.", + "No shortest path", + JOptionPane.ERROR_MESSAGE); + return; + } + else if (this.finalPath == null) { + mazeApp.clearShortestPath(); + return; + } + + pathDrawingMode = true; + + if (pathCounter == -1) { + JOptionPane.showMessageDialog(this, + "Click on the maze to see the shortest path evolve"); + pathCounter = 0; + } + + for (VertexInterface vertex : shortestPath) { + EBox box = (EBox) vertex; + g.fillOval(box.getX() * boxWidth + factorWidth, + box.getY() * boxHeight + factorHeight, + boxWidth - 2 * factorWidth, + boxHeight - 2 * factorHeight); + } +// Timer timer = new Timer(16, new ActionListener(){ +// int compteur = shortestPath.size()-1; +// VertexInterface vertex; +// @Override +// public void actionPerformed(ActionEvent e) { +// if (compteur < 0) { +// ((Timer)e.getSource()).stop(); +// } else { +// vertex = shortestPath.get(compteur); +// EBox box = (EBox) vertex; +// g.fillOval(box.getX() * boxWidth + factorWidth, +// box.getY()*boxHeight + factorHeight, +// boxWidth - 2 * factorWidth, +// boxHeight - 2 * factorHeight); +// compteur -= 1; +// } +// } +// }); +// timer.start(); + + if (pathCounter >= finalPath.size()) { + pathDrawingMode = false; + mazeApp.getMazeAppModel().setPathDrawn(2); + } + } + + public void mouseClicked(MouseEvent e) { + int boxRow = getBoxRow(e.getX()); + int boxLine = getBoxLine(e.getY()); + if (! pathDrawingMode) { + if (e.getButton() == MouseEvent.BUTTON3) { + BoxTypeEditionMenu boxTypeEditionMenu + = new BoxTypeEditionMenu(mazeApp, boxLine, boxRow); + boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY()); + } + else if (e.getButton() == MouseEvent.BUTTON1) { + mazeApp.getMazeAppModel().toggleBox(boxLine, boxRow); + } + } else { + shortestPath.add(this.finalPath.get(pathCounter)); + repaint(); + pathCounter += 1; + return; + } + return; + } + public void paintShortestPath() { paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); } + public void animateShortestPath() { + animateShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); + } + public void mousePressed(MouseEvent e) { return; } @@ -129,19 +242,6 @@ public class MazeDrawing extends JPanel implements MouseListener { return y / boxHeight; } - public void mouseClicked(MouseEvent e) { - int boxRow = getBoxRow(e.getX()); - int boxLine = getBoxLine(e.getY()); - if (e.getButton() == MouseEvent.BUTTON3) { - BoxTypeEditionMenu boxTypeEditionMenu = new BoxTypeEditionMenu(mazeApp, boxLine, boxRow); - boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY()); - } - else if (e.getButton() == MouseEvent.BUTTON1) { - mazeApp.getMazeAppModel().toggleBox(boxLine, boxRow); - } - return; - } - public void mouseReleased(MouseEvent e) { return; } @@ -153,4 +253,9 @@ public class MazeDrawing extends JPanel implements MouseListener { public void mouseEntered(MouseEvent e) { return; } + + public void clearShortestPath() { + this.pathCounter = -1; + this.shortestPath = new ArrayList(); + } } diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index 91339f1..d6e2bd7 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -11,6 +11,7 @@ public class MazeMenu extends JMenu { private final ClearMazeMenuItem clearMazeMenuItem; private final AddRowMenuItem addRowMenuItem; private final AddLineMenuItem addLineMenuItem; + private final AnimatePathMenuItem animatePathMenuItem; public MazeMenu(MazeApp mazeApp) { @@ -18,6 +19,7 @@ public class MazeMenu extends JMenu { this.mazeApp = mazeApp; add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp)); + add(animatePathMenuItem = new AnimatePathMenuItem(mazeApp)); add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp)); add(newClearMazeMenuItem = new NewClearMazeMenuItem(mazeApp)); add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp)); diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 368c0b2..4219cda 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -19,7 +19,16 @@ public class WindowPanel extends JPanel { drawingPanelGrid.notifyForUpdates(param); } - public void paintShortestPath() { - drawingPanelGrid.paintShortestPath(); - } + public void paintShortestPath() { + drawingPanelGrid.paintShortestPath(); + } + + public void clearShortestPath() { + drawingPanelGrid.clearShortestPath(); + } + + public void animateShortestPath() { + + drawingPanelGrid.animateShortestPath(); + } }