Add maze edition functionalities #27
No files matched your search
+40
-19
@@ -34,19 +34,20 @@ public class MazeAppModel extends Observable {
|
||||
if (departure == null || arrival == null) {
|
||||
previous = null;
|
||||
shortest = null;
|
||||
return;
|
||||
}
|
||||
else {
|
||||
previous = (Previous) Dijkstra.dijkstra(maze, departure);
|
||||
shortest = previous.getShortestPathTo(arrival);
|
||||
}
|
||||
setChanged();
|
||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||
}
|
||||
|
||||
public void loadMaze(String filename) {
|
||||
this.filename = new String(filename);
|
||||
maze = new Maze(this.filename);
|
||||
initModelFromMaze();
|
||||
saved = true;
|
||||
setChanged();
|
||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||
}
|
||||
|
||||
public Maze getMaze() {
|
||||
@@ -86,36 +87,56 @@ public class MazeAppModel extends Observable {
|
||||
notifyObservers(MazeAppModelMessage.FileChange);
|
||||
}
|
||||
|
||||
public void toggleBox(int x, int y) {
|
||||
if (maze.getMaze()[y][x].isWalkable()) {
|
||||
if (maze.getMaze()[y][x].isDeparture()) {
|
||||
if (arrival != null) {
|
||||
int arrivalRow = ((EBox) arrival).getX();
|
||||
int arrivalLine = ((EBox) arrival).getY();
|
||||
maze.getMaze()[arrivalLine][arrivalRow] =
|
||||
new EBox("E", arrivalLine, arrivalRow, maze);
|
||||
public void toggleBox(int boxLine, int boxRow) {
|
||||
if (maze.getMaze()[boxLine][boxRow].isWalkable()) {
|
||||
if (maze.getMaze()[boxLine][boxRow].isDeparture()) {
|
||||
setABox(boxLine, boxRow);
|
||||
}
|
||||
maze.getMaze()[y][x] = new ABox("A", y, x, maze);
|
||||
}
|
||||
else if (maze.getMaze()[y][x].isArrival()) {
|
||||
maze.getMaze()[y][x] = new EBox("E", y, x, maze);
|
||||
else if (maze.getMaze()[boxLine][boxRow].isArrival()) {
|
||||
setEBox(boxLine, boxRow);
|
||||
}
|
||||
else {
|
||||
maze.getMaze()[y][x] = new WBox("W", y, x, maze);
|
||||
setWBox(boxLine, boxRow);
|
||||
}
|
||||
}
|
||||
else {
|
||||
setDBox(boxLine, boxRow);
|
||||
}
|
||||
}
|
||||
|
||||
public void setWBox(int boxLine, int boxRow) {
|
||||
maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze);
|
||||
initModelFromMaze();
|
||||
saved = false;
|
||||
}
|
||||
|
||||
public void setEBox(int boxLine, int boxRow) {
|
||||
maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze);
|
||||
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()[y][x] = new DBox("D", y, x, maze);
|
||||
maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze);
|
||||
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);
|
||||
initModelFromMaze();
|
||||
saved = false;
|
||||
setChanged();
|
||||
notifyObservers(MazeAppModelMessage.MazeRenewal);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
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;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
+19
-7
@@ -117,16 +117,28 @@ public class MazeDrawing extends JPanel implements MouseListener {
|
||||
return;
|
||||
}
|
||||
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
int x = e.getX();
|
||||
int y = e.getY();
|
||||
private int getBoxRow(int x) {
|
||||
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
|
||||
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
|
||||
int boxWidth = getWidth() / dimensionX;
|
||||
return x / boxWidth;
|
||||
}
|
||||
|
||||
private int getBoxLine(int y) {
|
||||
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
|
||||
int boxHeight = getHeight() / dimensionY;
|
||||
int boxX = x / boxWidth;
|
||||
int boxY = y / boxHeight;
|
||||
mazeApp.getMazeAppModel().toggleBox(boxX, boxY);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user