Add UI to the project #29

Manually merged
regar42 merged 45 commits from mvc into master 2018-01-03 11:03:09 +00:00
10 changed files with 224 additions and 31 deletions
Showing only changes of commit f91f373986 - Show all commits

No files matched your search

+1
View File
@@ -27,6 +27,7 @@ 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);
}
}
}
+4 -3
View File
@@ -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<VertexInterface> 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) ;
}
+12
View File
@@ -18,4 +18,16 @@ public interface VertexInterface {
* @return The neighbourList of the element.
*/
public ArrayList<VertexInterface> 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<VertexInterface> getCandidates();
}
+10 -1
View File
@@ -23,6 +23,7 @@ public abstract class Box
private String label = null ;
private Maze maze ;
private ArrayList<VertexInterface> neighbourList;
private ArrayList<VertexInterface> candidates;
public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation
@@ -30,7 +31,7 @@ public abstract class Box
this.x = x ;
this.y = y ;
this.maze = maze ;
this.candidates = new ArrayList<VertexInterface>();
}
public ArrayList<VertexInterface> generateNeighbours() {
@@ -42,6 +43,14 @@ public abstract class Box
neighbourList = null;
}
public void addCandidate(VertexInterface vertex) {
candidates.add(vertex);
}
public ArrayList<VertexInterface> getCandidates() {
return candidates;
}
public boolean isWalkable() {
return true ;
}
+24 -4
View File
@@ -19,8 +19,16 @@ public class MazeAppModel extends Observable {
private Previous previous;
private ArrayList<VertexInterface> shortest;
private ArrayList<VertexInterface> 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));
@@ -71,11 +79,19 @@ public class MazeAppModel extends Observable {
return shortest;
}
public boolean isPathDrawn() {
public ArrayList<VertexInterface> getShortestDeepCopy() {
ArrayList<VertexInterface> deepCopy = new ArrayList<VertexInterface>();
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;
}
@@ -188,6 +204,10 @@ public class MazeAppModel extends Observable {
delLine(maze.getDimensionY() - 1);
}
public VertexInterface getDeparture() {
return departure;
}
public void clearMaze() {
newClearMaze(maze.getDimensionY(), maze.getDimensionX());
}
+27
View File
@@ -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();
}
}
+9 -2
View File
@@ -80,11 +80,12 @@ 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();
}
@@ -124,4 +125,10 @@ public class MazeApp extends JFrame
mazeAppModel.clearMaze();
this.repaint();
}
public void animateShortestPath() {
windowPanel.animateShortestPath();
mazeAppModel.setPathDrawn(1);
}
}
+123 -18
View File
@@ -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<VertexInterface> shortestPath;
private ArrayList<VertexInterface> finalPath;
private int pathCounter;
public MazeDrawing(MazeApp mazeApp) {
this.mazeApp = mazeApp;
this.pathDrawingMode = false;
this.shortestPath = new ArrayList<VertexInterface>();
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<VertexInterface> 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<VertexInterface>();
}
}
+2
View File
@@ -13,6 +13,7 @@ public class MazeMenu extends JMenu {
private final AddLineMenuItem addLineMenuItem;
private final DelRowMenuItem delRowMenuItem;
private final DelLineMenuItem delLineMenuItem;
private final AnimatePathMenuItem animatePathMenuItem;
public MazeMenu(MazeApp mazeApp) {
@@ -20,6 +21,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));
+12 -3
View File
@@ -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();
}
}