Add maze edition functionalities #27

Merged
antograb merged 5 commits from editMaze into mvc 2018-01-01 19:28:25 +00:00
25 changed files with 880 additions and 71 deletions

No files matched your search

-22
View File
@@ -1,30 +1,8 @@
import java.util.ArrayList;
import dijkstra.*;
import maze.*;
import ui.*; import ui.*;
public class MainTest { public class MainTest {
public static void main(String[] args) { public static void main(String[] args) {
Maze mazeTest = new Maze("data/labyrinthe.txt") ;
System.out.println(mazeTest);
mazeTest.saveToTextFile("data/labyOutput.txt");
VertexInterface departure = mazeTest.getDeparture();
VertexInterface arrival = mazeTest.getArrival();
Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, departure);
ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
//Print shortest path to arrival
System.out.println(arrival);
for (VertexInterface vertex : shortest) {
System.out.println(vertex);
}
MazeApp app = new MazeApp(); MazeApp app = new MazeApp();
} }
} }
+2 -2
View File
@@ -5,9 +5,9 @@ package maze;
*/ */
public class ABox extends EBox{ public class ABox extends EBox{
public ABox (String label, int x, int y, Maze maze) { public ABox (String label, int y, int x, Maze maze) {
super(label,x,y,maze) ; super(label, y, x, maze) ;
} }
+5 -1
View File
@@ -35,7 +35,11 @@ public abstract class Box
public ArrayList<VertexInterface> generateNeighbours() { public ArrayList<VertexInterface> generateNeighbours() {
this.neighbourList = maze.generateNeighbours(this); this.neighbourList = maze.generateNeighbours(this);
return maze.generateNeighbours(this) ; return this.neighbourList;
}
public void resetNeighbourList() {
neighbourList = null;
} }
public boolean isWalkable() { public boolean isWalkable() {
+2 -2
View File
@@ -6,9 +6,9 @@ package maze;
public class DBox extends EBox{ public class DBox extends EBox{
public DBox (String label, int x, int y, Maze maze) { public DBox (String label, int y, int x, Maze maze) {
super(label,x,y,maze) ; super(label, y, x, maze) ;
} }
+2 -4
View File
@@ -7,10 +7,8 @@ package maze;
public class EBox extends Box{ public class EBox extends Box{
public EBox (String label, int x, int y, Maze maze) { public EBox (String label, int y, int x, Maze maze) {
super(label,x,y,maze) ;
super(label, y, x, maze) ;
} }
} }
+205 -23
View File
@@ -42,46 +42,90 @@ public class Maze
this.maze = matrixDeepCopy(boxes); this.maze = matrixDeepCopy(boxes);
} }
private Box[][] matrixDeepCopy (Box[][] original) { public Maze(Box[][] newMaze) {
int numberOfLines = 0; this.dimensionY = newMaze.length;
int numberOfRows = 0; if (dimensionY > 0) {
Box[][] copy; this.dimensionX = newMaze[0].length;
/*
* We first get the dimensions of the matrix
*/
for (Box[] lineBoxes: original) {
numberOfLines++;
} }
if (numberOfLines > 0) { else {
for (Box box: original[0]) { this.dimensionX = 0;
numberOfRows++;
} }
this.maze = matrixDeepCopy(newMaze);
} }
copy = new Box[numberOfLines][numberOfRows];
/* private Box[][] matrixDeepCopy(Box[][] original,
* Now we proceed to the deep copy of each box Box[][] destination,
*/ int numberOfLines,
for (int line = 0; line < numberOfLines; line++) { int numberOfRows,
for (int row = 0; row < numberOfRows; row++) { int lineOffset,
int rowOffset)
{
if (destination == null) {
destination = new Box[numberOfLines][numberOfRows];
}
if (original.length < 1) {
return destination;
}
for (int line = lineOffset; line < lineOffset + numberOfLines; line++) {
for (int row = rowOffset; row < rowOffset + numberOfRows; row++) {
Box box = original[line][row]; Box box = original[line][row];
if (box.isWalkable()) { if (box.isWalkable()) {
if (box.isDeparture()) { if (box.isDeparture()) {
copy[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); destination[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
} }
else if (box.isArrival()) { else if (box.isArrival()) {
copy[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); destination[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
} }
else { else {
copy[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); destination[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
} }
} }
else { else {
copy[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); destination[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
} }
} }
} }
return copy; return destination;
}
private Box[][] matrixDeepCopy(Box[][] original,
int numberOfLines,
int numberOfRows,
int lineOffset,
int rowOffset,
int numberOfDestLines,
int numberOfDestRows) {
return matrixDeepCopy(original,
new Box[numberOfDestLines][numberOfDestRows],
numberOfLines,
numberOfRows,
lineOffset,
rowOffset);
}
private Box[][] matrixDeepCopy(Box [][] original,
int numberOfLines,
int numberOfRows,
int lineOffset,
int rowOffset) {
return matrixDeepCopy(original,
numberOfLines,
numberOfRows,
lineOffset,
rowOffset,
numberOfLines,
numberOfRows);
}
private Box[][] matrixDeepCopy(Box[][] original, int numberOfLines, int numberOfRows) {
return matrixDeepCopy(original, numberOfLines, numberOfRows, 0, 0);
}
private Box[][] matrixDeepCopy(Box[][] original) {
if (original.length < 1) {
return null;
}
return matrixDeepCopy(original, original.length, original[0].length);
} }
public static Maze emptyMaze(int dimension) { public static Maze emptyMaze(int dimension) {
@@ -124,6 +168,136 @@ public class Maze
return new Maze(numberOfLines, numberOfRows, maze); return new Maze(numberOfLines, numberOfRows, maze);
} }
public void addEmptyColumn(int posColumn) {
Box[][] newMaze = matrixDeepCopy(maze,
dimensionY,
posColumn,
0,
0,
dimensionY,
dimensionX + 1);
for (int line = 0; line < dimensionY; line++) {
newMaze[line][posColumn] = new EBox("E", line, posColumn, this);
}
for (int line = 0; line < dimensionY; line++) {
for (int row = posColumn; row < dimensionX; row++) {
Box box = maze[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
newMaze[line][row + 1] = new DBox(box.getLabel(), box.getY(), box.getX() + 1, this);
}
else if (box.isArrival()) {
newMaze[line][row + 1] = new ABox(box.getLabel(), box.getY(), box.getX() + 1, this);
}
else {
newMaze[line][row + 1] = new EBox(box.getLabel(), box.getY(), box.getX() + 1, this);
}
}
else {
newMaze[line][row + 1] = new WBox(box.getLabel(), box.getY(), box.getX() + 1, this);
}
}
}
maze = newMaze;
dimensionX = dimensionX + 1;
}
public void delColumn(int posColumn) {
Box[][] newMaze = matrixDeepCopy(maze,
dimensionY,
posColumn,
0,
0,
dimensionY,
dimensionX - 1);
for (int line = 0; line < dimensionY; line++) {
for (int row = posColumn + 1; row < dimensionX; row++) {
Box box = maze[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
newMaze[line][row - 1] = new DBox(box.getLabel(), box.getY(), box.getX() - 1, this);
}
else if (box.isArrival()) {
newMaze[line][row - 1] = new ABox(box.getLabel(), box.getY(), box.getX() - 1, this);
}
else {
newMaze[line][row - 1] = new EBox(box.getLabel(), box.getY(), box.getX() - 1, this);
}
}
else {
newMaze[line][row - 1] = new WBox(box.getLabel(), box.getY(), box.getX() - 1, this);
}
}
}
maze = newMaze;
dimensionX = dimensionX - 1;
}
public void addEmptyLine(int posLine) {
Box[][] newMaze = matrixDeepCopy(maze,
posLine,
dimensionX,
0,
0,
dimensionY + 1,
dimensionX);
for (int row = 0; row < dimensionX; row++) {
newMaze[posLine][row] = new EBox("E", posLine, row, this);
}
for (int line = posLine; line < dimensionY; line++) {
for (int row = 0; row < dimensionX; row++) {
Box box = maze[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
newMaze[line + 1][row] = new DBox(box.getLabel(), box.getY() + 1, box.getX(), this);
}
else if (box.isArrival()) {
newMaze[line + 1][row] = new ABox(box.getLabel(), box.getY() + 1, box.getX(), this);
}
else {
newMaze[line + 1][row] = new EBox(box.getLabel(), box.getY() + 1, box.getX(), this);
}
}
else {
newMaze[line + 1][row] = new WBox(box.getLabel(), box.getY() + 1, box.getX(), this);
}
}
}
dimensionY = dimensionY + 1;
maze = newMaze;
}
public void delLine(int posLine) {
Box[][] newMaze = matrixDeepCopy(maze,
posLine,
dimensionX,
0,
0,
dimensionY - 1,
dimensionX);
for (int line = posLine + 1; line < dimensionY; line++) {
for (int row = 0; row < dimensionX; row++) {
Box box = maze[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
newMaze[line - 1][row] = new DBox(box.getLabel(), box.getY() - 1, box.getX(), this);
}
else if (box.isArrival()) {
newMaze[line - 1][row] = new ABox(box.getLabel(), box.getY() - 1, box.getX(), this);
}
else {
newMaze[line - 1][row] = new EBox(box.getLabel(), box.getY() - 1, box.getX(), this);
}
}
else {
newMaze[line - 1][row] = new WBox(box.getLabel(), box.getY() - 1, box.getX(), this);
}
}
}
maze = newMaze;
dimensionY = dimensionY - 1;
}
public int getCost(VertexInterface start, VertexInterface end) { public int getCost(VertexInterface start, VertexInterface end) {
Box startBox = (Box) start ; Box startBox = (Box) start ;
@@ -336,4 +510,12 @@ public class Maze
return maze; return maze;
} }
public void resetNeighbourLists() {
for (Box[] line: maze) {
for (Box box: line) {
box.resetNeighbourList();
}
}
}
} }
+2 -4
View File
@@ -6,11 +6,9 @@ package maze;
public class WBox extends Box { public class WBox extends Box {
public WBox (String label, int y, int x, Maze maze) {
super(label, y, x, maze) ;
public WBox (String label, int x, int y, Maze maze) {
super(label,x,y,maze) ;
} }
+111 -7
View File
@@ -7,10 +7,8 @@ import java.util.ArrayList;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import dijkstra.VertexInterface; import dijkstra.*;
import dijkstra.Dijkstra; import maze.*;
import dijkstra.Previous;
import maze.Maze;
public class MazeAppModel extends Observable { public class MazeAppModel extends Observable {
@@ -36,19 +34,25 @@ public class MazeAppModel extends Observable {
if (departure == null || arrival == null) { if (departure == null || arrival == null) {
previous = null; previous = null;
shortest = null; shortest = null;
return;
} }
else {
previous = (Previous) Dijkstra.dijkstra(maze, departure); previous = (Previous) Dijkstra.dijkstra(maze, departure);
if (((Box) arrival).getNeighbourList() == null) {
shortest = null;
}
else {
shortest = previous.getShortestPathTo(arrival); shortest = previous.getShortestPathTo(arrival);
} }
}
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
public void loadMaze(String filename) { public void loadMaze(String filename) {
this.filename = new String(filename); this.filename = new String(filename);
maze = new Maze(this.filename); maze = new Maze(this.filename);
initModelFromMaze(); initModelFromMaze();
saved = true; saved = true;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public Maze getMaze() { public Maze getMaze() {
@@ -87,4 +91,104 @@ public class MazeAppModel extends Observable {
setChanged(); setChanged();
notifyObservers(MazeAppModelMessage.FileChange); notifyObservers(MazeAppModelMessage.FileChange);
} }
public void toggleBox(int boxLine, int boxRow) {
if (maze.getMaze()[boxLine][boxRow].isWalkable()) {
setWBox(boxLine, boxRow);
}
else {
setEBox(boxLine, boxRow);
}
}
public void setWBox(int boxLine, int boxRow) {
maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
}
public void setEBox(int boxLine, int boxRow) {
maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
}
public void setDBox(int boxLine, int boxRow) {
if (departure != null) {
int departureRow = ((EBox) departure).getX();
int departureLine = ((EBox) departure).getY();
maze.getMaze()[departureLine][departureRow] =
new EBox("E", departureLine, departureRow, maze);
}
maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
}
public void setABox(int boxLine, int boxRow) {
if (arrival != null) {
int arrivalRow = ((EBox) arrival).getX();
int arrivalLine = ((EBox) arrival).getY();
maze.getMaze()[arrivalLine][arrivalRow] =
new EBox("E", arrivalLine, arrivalRow, maze);
}
maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
}
public void newClearMaze(int numberOfLines, int numberOfRows) {
maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows));
initModelFromMaze();
saved = false;
}
public void addEmptyColumn() {
addEmptyColumn(maze.getDimensionX());
}
public void addEmptyColumn(int posColumn) {
maze.addEmptyColumn(posColumn);
initModelFromMaze();
saved = false;
}
public void delColumn(int posColumn) {
maze.delColumn(posColumn);
initModelFromMaze();
saved = false;
}
public void delColumn() {
delColumn(maze.getDimensionX() - 1);
}
public void addEmptyLine() {
addEmptyLine(maze.getDimensionY());
}
public void addEmptyLine(int posLine) {
maze.addEmptyLine(posLine);
initModelFromMaze();
saved = false;
}
public void delLine(int posLine) {
maze.delLine(posLine);
initModelFromMaze();
saved = false;
}
public void delLine() {
delLine(maze.getDimensionY() - 1);
}
public void clearMaze() {
newClearMaze(maze.getDimensionY(), maze.getDimensionX());
}
} }
+38
View File
@@ -0,0 +1,38 @@
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;
private final int lineNum;
public AddLineMenuItem(MazeApp mazeApp) {
super("Add line");
this.mazeApp = mazeApp;
lineNum = -1;
addActionListener(this);
}
public AddLineMenuItem(MazeApp mazeApp, int lineNum) {
super("Add line");
this.mazeApp = mazeApp;
this.lineNum = lineNum;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (lineNum == -1) {
mazeApp.addEmptyLine();
}
else {
mazeApp.addEmptyLine(lineNum);
}
}
}
+39
View File
@@ -0,0 +1,39 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class AddRowMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int rowNum;
public AddRowMenuItem(MazeApp mazeApp) {
super("Add row");
this.mazeApp = mazeApp;
rowNum = -1;
addActionListener(this);
}
public AddRowMenuItem(MazeApp mazeApp, int rowNum) {
super("Add row");
this.mazeApp = mazeApp;
this.rowNum = rowNum;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (rowNum == -1) {
mazeApp.addEmptyColumn();
}
else {
mazeApp.addEmptyColumn(rowNum);
}
}
}
+34
View File
@@ -0,0 +1,34 @@
package ui;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import maze.Box;
public class BoxTypeEditionMenu extends JPopupMenu {
JMenuItem setWBox;
JMenuItem setEBox;
JMenuItem setDBox;
JMenuItem setABox;
JMenuItem addRow;
JMenuItem addLine;
JMenuItem delRow;
JMenuItem delLine;
public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) {
add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow));
add(setWBox = new SetWBoxMenuItem(mazeApp, boxLine, boxRow));
add(setDBox = new SetDBoxMenuItem(mazeApp, boxLine, boxRow));
add(setABox = new SetABoxMenuItem(mazeApp, boxLine, boxRow));
Box box = (Box) mazeApp.getMazeAppModel().getMaze().getMaze()[boxLine][boxRow];
setEBox.setEnabled((box.isWalkable() && (box.isDeparture() ||
box.isArrival())) || ! box.isWalkable());
setWBox.setEnabled(box.isWalkable());
setDBox.setEnabled(! box.isDeparture());
setABox.setEnabled(! box.isArrival());
add(addRow = new AddRowMenuItem(mazeApp, boxRow));
add(addLine = new AddLineMenuItem(mazeApp, boxLine));
add(delRow = new DelRowMenuItem(mazeApp, boxRow));
add(delLine = new DelLineMenuItem(mazeApp, boxLine));
}
}
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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) {
mazeApp.clearMaze();
}
}
+38
View File
@@ -0,0 +1,38 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class DelLineMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int lineNum;
public DelLineMenuItem(MazeApp mazeApp) {
super("Delete line");
this.mazeApp = mazeApp;
lineNum = -1;
addActionListener(this);
}
public DelLineMenuItem(MazeApp mazeApp, int lineNum) {
super("Delete line");
this.mazeApp = mazeApp;
this.lineNum = lineNum;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (lineNum == -1) {
mazeApp.delLine();
}
else {
mazeApp.delLine(lineNum);
}
}
}
+37
View File
@@ -0,0 +1,37 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class DelRowMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int rowNum;
public DelRowMenuItem(MazeApp mazeApp) {
super("Delete row");
this.mazeApp = mazeApp;
rowNum = -1;
addActionListener(this);
}
public DelRowMenuItem(MazeApp mazeApp, int rowNum) {
super("Delete row");
this.mazeApp = mazeApp;
this.rowNum = rowNum;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (rowNum == -1) {
mazeApp.delColumn();
}
else {
mazeApp.delColumn(rowNum);
}
}
}
+38 -1
View File
@@ -79,12 +79,49 @@ public class MazeApp extends JFrame
} }
public void drawShortestPath() { public void drawShortestPath() {
mazeAppModel.setPathDrawn(true);
windowPanel.paintShortestPath(); windowPanel.paintShortestPath();
mazeAppModel.setPathDrawn(true);
} }
public void clearShortestPath() { public void clearShortestPath() {
mazeAppModel.setPathDrawn(false); mazeAppModel.setPathDrawn(false);
this.repaint(); this.repaint();
} }
public void addEmptyColumn(int posColumn) {
mazeAppModel.addEmptyColumn(posColumn);
}
public void addEmptyColumn() {
mazeAppModel.addEmptyColumn();
}
public void delColumn(int posColumn) {
mazeAppModel.delColumn(posColumn);
}
public void delColumn() {
mazeAppModel.delColumn();
}
public void addEmptyLine(int posLine) {
mazeAppModel.addEmptyLine(posLine);
}
public void addEmptyLine() {
mazeAppModel.addEmptyLine();
}
public void delLine(int posLine) {
mazeAppModel.delLine(posLine);
}
public void delLine() {
mazeAppModel.delLine();
}
public void clearMaze() {
mazeAppModel.clearMaze();
this.repaint();
}
} }
+63 -2
View File
@@ -1,17 +1,21 @@
package ui; package ui;
import java.util.ArrayList;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.GridLayout; import java.awt.GridLayout;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import maze.Maze; import maze.Maze;
import model.MazeAppModelMessage; import model.MazeAppModelMessage;
import maze.EBox; import maze.EBox;
import dijkstra.VertexInterface; import dijkstra.VertexInterface;
public class MazeDrawing extends JPanel { public class MazeDrawing extends JPanel implements MouseListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -20,6 +24,7 @@ public class MazeDrawing extends JPanel {
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
setBackground(Color.BLACK); setBackground(Color.BLACK);
setPreferredSize(new Dimension(500,500)); setPreferredSize(new Dimension(500,500));
addMouseListener(this);
} }
@Override @Override
@@ -83,7 +88,22 @@ public class MazeDrawing extends JPanel {
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int boxWidth = width / dimensionX; int boxWidth = width / dimensionX;
int boxHeight = height / dimensionY; int boxHeight = height / dimensionY;
for (VertexInterface vertex : mazeApp.getMazeAppModel().getShortest()) { ArrayList<VertexInterface> shortest =
mazeApp.getMazeAppModel().getShortest();
if (shortest == null && ! mazeApp.getMazeAppModel().isPathDrawn()) {
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 (shortest == null) {
mazeApp.clearShortestPath();
return;
}
for (VertexInterface vertex : shortest) {
EBox box = (EBox) vertex; EBox box = (EBox) vertex;
g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight); g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight);
} }
@@ -92,4 +112,45 @@ public class MazeDrawing extends JPanel {
public void paintShortestPath() { public void paintShortestPath() {
paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight());
} }
public void mousePressed(MouseEvent e) {
return;
}
private int getBoxRow(int x) {
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
int boxWidth = getWidth() / dimensionX;
return x / boxWidth;
}
private int getBoxLine(int y) {
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int boxHeight = getHeight() / dimensionY;
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;
}
public void mouseExited(MouseEvent e) {
return;
}
public void mouseEntered(MouseEvent e) {
return;
}
} }
+12
View File
@@ -7,6 +7,12 @@ public class MazeMenu extends JMenu {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final DrawShortestPathMenuItem drawMenuItem; private final DrawShortestPathMenuItem drawMenuItem;
private final ClearShortestPathMenuItem clearMenuItem; private final ClearShortestPathMenuItem clearMenuItem;
private final NewClearMazeMenuItem newClearMazeMenuItem;
private final ClearMazeMenuItem clearMazeMenuItem;
private final AddRowMenuItem addRowMenuItem;
private final AddLineMenuItem addLineMenuItem;
private final DelRowMenuItem delRowMenuItem;
private final DelLineMenuItem delLineMenuItem;
public MazeMenu(MazeApp mazeApp) { public MazeMenu(MazeApp mazeApp) {
@@ -15,5 +21,11 @@ public class MazeMenu extends JMenu {
add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp)); add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp));
add(clearMenuItem = new ClearShortestPathMenuItem(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));
add(delRowMenuItem = new DelRowMenuItem(mazeApp));
add(delLineMenuItem = new DelLineMenuItem(mazeApp));
} }
} }
+30
View File
@@ -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();
}
}
+89
View File
@@ -0,0 +1,89 @@
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;
public class NewMazeSpinnerBox extends JPanel
implements ActionListener {
private final MazeApp mazeApp;
private JDialog spinnerDialog;
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);
JSpinner lineSpinner = addLabeledSpinner(this, labels[0], lineSpinnerModel);
this.rowSpinnerModel =
new SpinnerNumberModel(initialValue, //initial value
initialValue, //min
maximumValue, //max
1);
JSpinner rowSpinner = addLabeledSpinner(this, labels[1], 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(this,
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) {
mazeApp.getMazeAppModel(
).newClearMaze((int) lineSpinnerModel.getValue(),
(int) rowSpinnerModel.getValue());
spinnerDialog.dispose();
}
}
+1 -1
View File
@@ -41,7 +41,7 @@ public class SaveMazeMenuItem extends JMenuItem
this.setEnabled(true); this.setEnabled(true);
} }
else { else {
this.setEnabled(true); this.setEnabled(false);
} }
} }
} }
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class SetABoxMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int boxLine;
private final int boxRow;
public SetABoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) {
super("Set Arrival Box");
this.mazeApp = mazeApp;
this.boxLine = boxLine;
this.boxRow = boxRow;
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setABox(boxLine, boxRow);
}
}
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class SetDBoxMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int boxLine;
private final int boxRow;
public SetDBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) {
super("Set Departure Box");
this.mazeApp = mazeApp;
this.boxLine = boxLine;
this.boxRow = boxRow;
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setDBox(boxLine, boxRow);
}
}
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class SetEBoxMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int boxLine;
private final int boxRow;
public SetEBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) {
super("Set Empty Box");
this.mazeApp = mazeApp;
this.boxLine = boxLine;
this.boxRow = boxRow;
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setEBox(boxLine, boxRow);
}
}
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class SetWBoxMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int boxLine;
private final int boxRow;
public SetWBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) {
super("Set Wall Box");
this.mazeApp = mazeApp;
this.boxLine = boxLine;
this.boxRow = boxRow;
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setWBox(boxLine, boxRow);
}
}
+2 -2
View File
@@ -4,8 +4,8 @@ import javax.swing.*;
import maze.Maze; import maze.Maze;
import java.awt.*; import java.awt.*;
public class WindowPanel extends JPanel public class WindowPanel extends JPanel {
{
private final MazeDrawing drawingPanelGrid; private final MazeDrawing drawingPanelGrid;
public WindowPanel(MazeApp mazeApp) { public WindowPanel(MazeApp mazeApp) {