Add a toggle to dynamically change boxes type

maze:
* Fix EBox, ABox, DBox and WBox, which still used legacy coordinates
  (x for y and y for x). See 213071d3 and #20

model:
* Add a toggle method to MazeAppModel. For a box given by its
  coordinates, it toggles its type (Wall, Empty, Departure or
  Arrival), sends a notification to observers and sets the maze's
  status to 'not saved'.

ui:
* Fix SaveMazeMenuItem, which enabled itself whenever a Maze renewal
  notification was received. It now enables itself only if a filename is
  set (expected behavior).
* Add a MouseListener to MazeDrawing to catch clicks on boxes. The
  selected box is found using the x and y coordinates of where the mouse
  clicked.
* Now print a message when the user tries to draw the shortest path
  whereas the maze doesn't contain an arrival and/or a departure.
This commit is contained in:
regar42 committed 2017-12-28 18:26:20 +01:00
1 parent 7c1d136856
commit 37def85f5b
8 files changed
+112 -36

No files matched your search

+6 -6
View File
@@ -4,13 +4,13 @@ package maze;
* <code>ABox</code> is an arrival <code>Box</code>. It's the last <code>Box</code> to be walked through and it's an exit to the <code>Maze</code>.
*/
public class ABox extends EBox{
public ABox (String label, int x, int y, Maze maze) {
super(label,x,y,maze) ;
public ABox (String label, int y, int x, Maze maze) {
super(label, y, x, maze) ;
}
@Override
public boolean isArrival() {
return true ;
+6 -6
View File
@@ -6,15 +6,15 @@ package maze;
public class DBox extends EBox{
public DBox (String label, int x, int y, Maze maze) {
super(label,x,y,maze) ;
public DBox (String label, int y, int x, Maze maze) {
super(label, y, x, maze) ;
}
@Override
public boolean isDeparture() {
return true ;
}
}
+5 -7
View File
@@ -5,12 +5,10 @@ package maze;
*/
public class EBox extends Box{
public EBox (String label, int x, int y, Maze maze) {
super(label,x,y,maze) ;
}
public EBox (String label, int y, int x, Maze maze) {
super(label, y, x, maze) ;
}
}
+6 -8
View File
@@ -5,14 +5,12 @@ package maze;
*/
public class WBox extends Box {
public WBox (String label, int x, int y, Maze maze) {
super(label,x,y,maze) ;
public WBox (String label, int y, int x, Maze maze) {
super(label, y, x, maze) ;
}
@Override
+35 -4
View File
@@ -7,10 +7,8 @@ import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import dijkstra.VertexInterface;
import dijkstra.Dijkstra;
import dijkstra.Previous;
import maze.Maze;
import dijkstra.*;
import maze.*;
public class MazeAppModel extends Observable {
@@ -87,4 +85,37 @@ public class MazeAppModel extends Observable {
setChanged();
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);
}
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 {
maze.getMaze()[y][x] = new WBox("W", y, x, maze);
}
}
else {
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);
}
initModelFromMaze();
saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
}
+1 -1
View File
@@ -79,8 +79,8 @@ public class MazeApp extends JFrame
}
public void drawShortestPath() {
mazeAppModel.setPathDrawn(true);
windowPanel.paintShortestPath();
mazeAppModel.setPathDrawn(true);
}
public void clearShortestPath() {
+52 -3
View File
@@ -1,17 +1,21 @@
package ui;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import maze.Maze;
import model.MazeAppModelMessage;
import maze.EBox;
import dijkstra.VertexInterface;
public class MazeDrawing extends JPanel {
public class MazeDrawing extends JPanel implements MouseListener {
private final MazeApp mazeApp;
@@ -19,7 +23,8 @@ public class MazeDrawing extends JPanel {
this.mazeApp = mazeApp;
setBackground(Color.BLACK);
setPreferredSize(new Dimension(500,500)) ;
setPreferredSize(new Dimension(500,500));
addMouseListener(this);
}
@Override
@@ -83,7 +88,22 @@ public class MazeDrawing extends JPanel {
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int boxWidth = width / dimensionX;
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;
g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight);
}
@@ -92,4 +112,33 @@ public class MazeDrawing extends JPanel {
public void paintShortestPath() {
paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight());
}
public void mousePressed(MouseEvent e) {
return;
}
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int boxWidth = getWidth() / dimensionX;
int boxHeight = getHeight() / dimensionY;
int boxX = x / boxWidth;
int boxY = y / boxHeight;
mazeApp.getMazeAppModel().toggleBox(boxX, boxY);
return;
}
public void mouseReleased(MouseEvent e) {
return;
}
public void mouseExited(MouseEvent e) {
return;
}
public void mouseEntered(MouseEvent e) {
return;
}
}
+1 -1
View File
@@ -41,7 +41,7 @@ public class SaveMazeMenuItem extends JMenuItem
this.setEnabled(true);
}
else {
this.setEnabled(true);
this.setEnabled(false);
}
}
}