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

+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);
}
}
}