From 43feeaf93f7186f26cfde78df5dbe9d0f0348779 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 30 Dec 2017 00:19:41 +0100 Subject: [PATCH 1/2] Add editing functionalities --- src/maze/Maze.java | 6 ++ src/model/MazeAppModel.java | 76 +++++++++++++++++++---- src/ui/AddLineMenuItem.java | 25 ++++++++ src/ui/AddRowMenuItem.java | 27 +++++++++ src/ui/ClearMazeMenuItem.java | 35 +++++++++++ src/ui/MazeMenu.java | 8 +++ src/ui/NewClearMazeMenuItem.java | 30 +++++++++ src/ui/NewMazeSpinnerBox.java | 101 +++++++++++++++++++++++++++++++ src/ui/WindowPanel.java | 6 +- 9 files changed, 298 insertions(+), 16 deletions(-) create mode 100644 src/ui/AddLineMenuItem.java create mode 100644 src/ui/AddRowMenuItem.java create mode 100644 src/ui/ClearMazeMenuItem.java create mode 100644 src/ui/NewClearMazeMenuItem.java create mode 100644 src/ui/NewMazeSpinnerBox.java diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 9f85bad..3290166 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -42,6 +42,12 @@ public class Maze this.maze = matrixDeepCopy(boxes); } + public Maze(Box[][] newMaze) { + this.dimensionX = newMaze[0].length; + this.dimensionY = newMaze.length; + this.maze = matrixDeepCopy(newMaze); + } + private Box[][] matrixDeepCopy (Box[][] original) { int numberOfLines = 0; int numberOfRows = 0; diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index b75cb4f..027064e 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -12,15 +12,15 @@ import maze.*; public class MazeAppModel extends Observable { - private String filename; + private String filename; private Maze maze; private VertexInterface departure; private VertexInterface arrival; private Previous previous; private ArrayList shortest; private ArrayList boxList; - private boolean saved = true; - private boolean pathDrawn = false; + private boolean saved = true; + private boolean pathDrawn = false; public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); @@ -89,18 +89,10 @@ public class MazeAppModel extends Observable { public void toggleBox(int boxLine, int boxRow) { if (maze.getMaze()[boxLine][boxRow].isWalkable()) { - if (maze.getMaze()[boxLine][boxRow].isDeparture()) { - setABox(boxLine, boxRow); - } - else if (maze.getMaze()[boxLine][boxRow].isArrival()) { - setEBox(boxLine, boxRow); - } - else { - setWBox(boxLine, boxRow); - } + setWBox(boxLine, boxRow); } else { - setDBox(boxLine, boxRow); + setEBox(boxLine, boxRow); } } @@ -139,4 +131,62 @@ public class MazeAppModel extends Observable { initModelFromMaze(); saved = false; } + + public void newClearMaze(int numberOfLines, int numberOfRows) { + + Box[][] newClearMaze = new Box[numberOfLines][numberOfRows]; + + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + newClearMaze[line][row] = new EBox("E", line, row, this.maze); + } + } + this.maze = new Maze(newClearMaze); + initModelFromMaze(); + saved = false; + } + + public void addEmptyColumn() { + + Box[][] previousMaze = maze.getMaze(); + int numberOfLines = maze.getDimensionY(); + int numberOfRows = maze.getDimensionX()+1; + + newClearMaze(numberOfLines, numberOfRows); + + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + if (row == numberOfRows-1) { + maze.getMaze()[line][row] = new EBox("E", line, row, maze); + } else { + maze.getMaze()[line][row] = previousMaze[line][row]; + } + } + } + + initModelFromMaze(); + saved = false; + } + + public void addEmptyLine() { + + Box[][] previousMaze = maze.getMaze(); + int numberOfLines = maze.getDimensionY()+1; + int numberOfRows = maze.getDimensionX(); + + newClearMaze(numberOfLines, numberOfRows); + + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + if (line == numberOfLines-1) { + maze.getMaze()[line][row] = new EBox("E", line, row, maze); + } else { + maze.getMaze()[line][row] = previousMaze[line][row]; + } + } + } + + initModelFromMaze(); + saved = false; + } } diff --git a/src/ui/AddLineMenuItem.java b/src/ui/AddLineMenuItem.java new file mode 100644 index 0000000..2652d37 --- /dev/null +++ b/src/ui/AddLineMenuItem.java @@ -0,0 +1,25 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class AddLineMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public AddLineMenuItem(MazeApp mazeApp) { + + super("Add line"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent arg0) { + + this.mazeApp.getMazeAppModel().addEmptyLine(); + } +} diff --git a/src/ui/AddRowMenuItem.java b/src/ui/AddRowMenuItem.java new file mode 100644 index 0000000..4eb69e1 --- /dev/null +++ b/src/ui/AddRowMenuItem.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 AddRowMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public AddRowMenuItem(MazeApp mazeApp) { + + super("Add row"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent arg0) { + + this.mazeApp.getMazeAppModel().addEmptyColumn(); + } +} diff --git a/src/ui/ClearMazeMenuItem.java b/src/ui/ClearMazeMenuItem.java new file mode 100644 index 0000000..4911494 --- /dev/null +++ b/src/ui/ClearMazeMenuItem.java @@ -0,0 +1,35 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.Action; +import javax.swing.Icon; +import javax.swing.JMenuItem; +import javax.swing.KeyStroke; + +public class ClearMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public ClearMazeMenuItem(MazeApp mazeApp) { + + super("Clear maze"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control D")); + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + + int numberOfLines = mazeApp.getMazeAppModel() + .getMaze().getDimensionY(); + int numberOfRows = mazeApp.getMazeAppModel() + .getMaze().getDimensionX(); + this.mazeApp.getMazeAppModel() + .newClearMaze(numberOfLines, numberOfRows); + System.out.println("test"); + } +} diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index e257339..91339f1 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -7,6 +7,10 @@ public class MazeMenu extends JMenu { private final MazeApp mazeApp; private final DrawShortestPathMenuItem drawMenuItem; private final ClearShortestPathMenuItem clearMenuItem; + private final NewClearMazeMenuItem newClearMazeMenuItem; + private final ClearMazeMenuItem clearMazeMenuItem; + private final AddRowMenuItem addRowMenuItem; + private final AddLineMenuItem addLineMenuItem; public MazeMenu(MazeApp mazeApp) { @@ -15,5 +19,9 @@ public class MazeMenu extends JMenu { add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp)); add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp)); + add(newClearMazeMenuItem = new NewClearMazeMenuItem(mazeApp)); + add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp)); + add(addRowMenuItem = new AddRowMenuItem(mazeApp)); + add(addLineMenuItem = new AddLineMenuItem(mazeApp)); } } diff --git a/src/ui/NewClearMazeMenuItem.java b/src/ui/NewClearMazeMenuItem.java new file mode 100644 index 0000000..19cc68a --- /dev/null +++ b/src/ui/NewClearMazeMenuItem.java @@ -0,0 +1,30 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.KeyStroke; + +public class NewClearMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private NewMazeSpinnerBox spinnerBox; + + public NewClearMazeMenuItem(MazeApp mazeApp) { + + super("New clear maze"); + this.mazeApp = mazeApp; + this.spinnerBox = new NewMazeSpinnerBox(mazeApp); + this.setAccelerator(KeyStroke.getKeyStroke("control N")); + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent arg0) { + + spinnerBox.createAndShowGUI(); + } +} diff --git a/src/ui/NewMazeSpinnerBox.java b/src/ui/NewMazeSpinnerBox.java new file mode 100644 index 0000000..ae43d2a --- /dev/null +++ b/src/ui/NewMazeSpinnerBox.java @@ -0,0 +1,101 @@ +package ui; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Container; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.SpinnerModel; +import javax.swing.SpinnerNumberModel; +import javax.swing.SpringLayout; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +public class NewMazeSpinnerBox extends JPanel + implements ActionListener, + ChangeListener { + + private final MazeApp mazeApp; + private JDialog spinnerDialog; + private int numberOfLines; + private int numberOfRows; + private SpinnerModel lineSpinnerModel; + private SpinnerModel rowSpinnerModel; + + public NewMazeSpinnerBox(MazeApp mazeApp) { + + + this.mazeApp = mazeApp; + JPanel spinnerDialog = new JPanel(new SpringLayout()); + String[] labels = {"Number of lines :", "Number of rows :"}; + int len = labels.length; + int initialValue = 3; + int maximumValue = 200; + + this.lineSpinnerModel = + new SpinnerNumberModel(initialValue, //initial value + initialValue, //min + maximumValue, //max + 1); + this.lineSpinnerModel.addChangeListener(this); + JSpinner lineSpinner = addLabeledSpinner(this, labels[0], lineSpinnerModel); + + this.rowSpinnerModel = + new SpinnerNumberModel(initialValue, //initial value + initialValue, //min + maximumValue, //max + 1); + this.rowSpinnerModel.addChangeListener(this); + JSpinner rowSpinner = addLabeledSpinner(this, labels[0], rowSpinnerModel); + } + + static protected JSpinner addLabeledSpinner(Container c, + String label, + SpinnerModel model) { + + JLabel l = new JLabel(label); + c.add(l); + + JSpinner spinner = new JSpinner(model); + l.setLabelFor(spinner); + c.add(spinner); + + return spinner; + } + + public void createAndShowGUI() { + + spinnerDialog = new JDialog(spinnerDialog, "Create a new empty maze"); + spinnerDialog.setLayout(new BorderLayout()); + spinnerDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + + spinnerDialog.add(new NewMazeSpinnerBox(this.mazeApp), + BorderLayout.CENTER); + + JButton OKButton = new JButton("OK"); + OKButton.addActionListener(this); + spinnerDialog.add(OKButton, BorderLayout.SOUTH); + + spinnerDialog.pack(); + spinnerDialog.setVisible(true); + } + + @Override + public void actionPerformed(ActionEvent e) { + spinnerDialog.dispose(); + } + + @Override + public void stateChanged(ChangeEvent arg0) { + this.mazeApp.getMazeAppModel() + .newClearMaze((int) this.lineSpinnerModel.getValue(), + (int) this.rowSpinnerModel.getValue()); + } +} diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 8ff60ca..368c0b2 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -4,9 +4,9 @@ import javax.swing.*; import maze.Maze; import java.awt.*; -public class WindowPanel extends JPanel -{ - private final MazeDrawing drawingPanelGrid; +public class WindowPanel extends JPanel { + + private final MazeDrawing drawingPanelGrid; public WindowPanel(MazeApp mazeApp) { From e7c2ecb3532226fefa1e98ae3f1f998669bc8598 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 31 Dec 2017 13:05:17 +0100 Subject: [PATCH 2/2] Add path drawing interaction & animation feature --- src/MainTest.java | 1 + src/dijkstra/Dijkstra.java | 3 +- src/dijkstra/Previous.java | 7 +- src/dijkstra/VertexInterface.java | 12 +++ src/maze/Box.java | 13 ++- src/model/MazeAppModel.java | 28 +++++- src/ui/AnimatePathMenuItem.java | 27 ++++++ src/ui/MazeApp.java | 11 ++- src/ui/MazeDrawing.java | 141 ++++++++++++++++++++++++++---- src/ui/MazeMenu.java | 2 + src/ui/WindowPanel.java | 15 +++- 11 files changed, 227 insertions(+), 33 deletions(-) create mode 100644 src/ui/AnimatePathMenuItem.java 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(); + } }