Add animation to path drawing #28
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
import dijkstra.*;
|
||||
import maze.*;
|
||||
import ui.*;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) ;
|
||||
}
|
||||
|
||||
@@ -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
@@ -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() {
|
||||
@@ -38,6 +39,14 @@ public abstract class Box
|
||||
return maze.generateNeighbours(this) ;
|
||||
}
|
||||
|
||||
public void addCandidate(VertexInterface vertex) {
|
||||
candidates.add(vertex);
|
||||
}
|
||||
|
||||
public ArrayList<VertexInterface> getCandidates() {
|
||||
return candidates;
|
||||
}
|
||||
|
||||
public boolean isWalkable() {
|
||||
return true ;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
+84
-14
@@ -12,7 +12,7 @@ import maze.*;
|
||||
|
||||
public class MazeAppModel extends Observable {
|
||||
|
||||
private String filename;
|
||||
private String filename;
|
||||
private Maze maze;
|
||||
private VertexInterface departure;
|
||||
private VertexInterface arrival;
|
||||
@@ -20,7 +20,15 @@ public class MazeAppModel extends Observable {
|
||||
private ArrayList<VertexInterface> shortest;
|
||||
private ArrayList<VertexInterface> boxList;
|
||||
private boolean saved = true;
|
||||
private boolean pathDrawn = false;
|
||||
/*
|
||||
* 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<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;
|
||||
}
|
||||
|
||||
@@ -89,18 +105,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 +147,66 @@ public class MazeAppModel extends Observable {
|
||||
initModelFromMaze();
|
||||
saved = false;
|
||||
}
|
||||
|
||||
public VertexInterface getDeparture() {
|
||||
return departure;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
+9
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
+123
-18
@@ -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>();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,11 @@ 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;
|
||||
private final AnimatePathMenuItem animatePathMenuItem;
|
||||
|
||||
public MazeMenu(MazeApp mazeApp) {
|
||||
|
||||
@@ -14,6 +19,11 @@ 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));
|
||||
add(addRowMenuItem = new AddRowMenuItem(mazeApp));
|
||||
add(addLineMenuItem = new AddLineMenuItem(mazeApp));
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
+15
-6
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user