From 37def85f5b23ef5793bc21f37c538b94abd9a0b5 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Thu, 28 Dec 2017 18:26:20 +0100 Subject: [PATCH 1/5] 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. --- src/maze/ABox.java | 12 ++++---- src/maze/DBox.java | 12 ++++---- src/maze/EBox.java | 12 ++++---- src/maze/WBox.java | 14 ++++----- src/model/MazeAppModel.java | 39 ++++++++++++++++++++++--- src/ui/MazeApp.java | 2 +- src/ui/MazeDrawing.java | 55 ++++++++++++++++++++++++++++++++++-- src/ui/SaveMazeMenuItem.java | 2 +- 8 files changed, 112 insertions(+), 36 deletions(-) diff --git a/src/maze/ABox.java b/src/maze/ABox.java index 94f88f7..ef934fa 100644 --- a/src/maze/ABox.java +++ b/src/maze/ABox.java @@ -4,13 +4,13 @@ package maze; * ABox is an arrival Box. It's the last Box to be walked through and it's an exit to the Maze. */ 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 ; diff --git a/src/maze/DBox.java b/src/maze/DBox.java index cc7b7fb..c6ae519 100644 --- a/src/maze/DBox.java +++ b/src/maze/DBox.java @@ -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 ; } - + } diff --git a/src/maze/EBox.java b/src/maze/EBox.java index b2d968b..375aebb 100644 --- a/src/maze/EBox.java +++ b/src/maze/EBox.java @@ -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) ; + } } diff --git a/src/maze/WBox.java b/src/maze/WBox.java index 29a1107..faee8ec 100644 --- a/src/maze/WBox.java +++ b/src/maze/WBox.java @@ -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 diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index c987abe..b3e9645 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -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); + } } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index c5a9dd7..48c54c5 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -79,8 +79,8 @@ public class MazeApp extends JFrame } public void drawShortestPath() { - mazeAppModel.setPathDrawn(true); windowPanel.paintShortestPath(); + mazeAppModel.setPathDrawn(true); } public void clearShortestPath() { diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 0a5bd55..793c00d 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -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 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; + } } diff --git a/src/ui/SaveMazeMenuItem.java b/src/ui/SaveMazeMenuItem.java index f5026d2..13b8c8e 100644 --- a/src/ui/SaveMazeMenuItem.java +++ b/src/ui/SaveMazeMenuItem.java @@ -41,7 +41,7 @@ public class SaveMazeMenuItem extends JMenuItem this.setEnabled(true); } else { - this.setEnabled(true); + this.setEnabled(false); } } } From 81df80e7ea9c206d56b7e65d1eabdec41777fee6 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Fri, 29 Dec 2017 12:04:15 +0100 Subject: [PATCH 2/5] Add a context menu to edit boxes type model: * New methods to directly set boxes type instead of only toggling (setXBox methods) * toggleBox now calls setXBox methods ui: * Add a context menu, enabled on right click, to directly choose the type of a box in the maze * Defer the calculation of the box coordinates in the matrix to two new methods, getBoxRow and getBoxLine --- src/model/MazeAppModel.java | 75 ++++++++++++++++++++++------------ src/ui/BoxTypeEditionMenu.java | 25 ++++++++++++ src/ui/MazeDrawing.java | 26 ++++++++---- src/ui/SetABoxMenuItem.java | 26 ++++++++++++ src/ui/SetDBoxMenuItem.java | 26 ++++++++++++ src/ui/SetEBoxMenuItem.java | 26 ++++++++++++ src/ui/SetWBoxMenuItem.java | 26 ++++++++++++ 7 files changed, 196 insertions(+), 34 deletions(-) create mode 100644 src/ui/BoxTypeEditionMenu.java create mode 100644 src/ui/SetABoxMenuItem.java create mode 100644 src/ui/SetDBoxMenuItem.java create mode 100644 src/ui/SetEBoxMenuItem.java create mode 100644 src/ui/SetWBoxMenuItem.java diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index b3e9645..b75cb4f 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -34,10 +34,13 @@ public class MazeAppModel extends Observable { if (departure == null || arrival == null) { previous = null; shortest = null; - return; } - previous = (Previous) Dijkstra.dijkstra(maze, departure); - shortest = previous.getShortestPathTo(arrival); + else { + previous = (Previous) Dijkstra.dijkstra(maze, departure); + shortest = previous.getShortestPathTo(arrival); + } + setChanged(); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public void loadMaze(String filename) { @@ -45,8 +48,6 @@ public class MazeAppModel extends Observable { 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); - } - maze.getMaze()[y][x] = new ABox("A", y, x, maze); + 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()[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 { - 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); + 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()[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); } } diff --git a/src/ui/BoxTypeEditionMenu.java b/src/ui/BoxTypeEditionMenu.java new file mode 100644 index 0000000..601fafc --- /dev/null +++ b/src/ui/BoxTypeEditionMenu.java @@ -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()); + } +} diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 793c00d..f32cae6 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -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; } diff --git a/src/ui/SetABoxMenuItem.java b/src/ui/SetABoxMenuItem.java new file mode 100644 index 0000000..2449a0c --- /dev/null +++ b/src/ui/SetABoxMenuItem.java @@ -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); + } +} diff --git a/src/ui/SetDBoxMenuItem.java b/src/ui/SetDBoxMenuItem.java new file mode 100644 index 0000000..fd9c5fd --- /dev/null +++ b/src/ui/SetDBoxMenuItem.java @@ -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); + } +} diff --git a/src/ui/SetEBoxMenuItem.java b/src/ui/SetEBoxMenuItem.java new file mode 100644 index 0000000..85a41fd --- /dev/null +++ b/src/ui/SetEBoxMenuItem.java @@ -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); + } +} diff --git a/src/ui/SetWBoxMenuItem.java b/src/ui/SetWBoxMenuItem.java new file mode 100644 index 0000000..3cc032b --- /dev/null +++ b/src/ui/SetWBoxMenuItem.java @@ -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); + } +} From 286023c5d8b1695a7e13d281cd32861dd6743982 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 30 Dec 2017 00:19:41 +0100 Subject: [PATCH 3/5] Add editing functionalities --- src/maze/Maze.java | 6 ++ src/model/MazeAppModel.java | 76 +++++++++++++++++++---- src/ui/AddLineMenuItem.java | 25 ++++++++ src/ui/AddRowMenuItem.java | 27 +++++++++ src/ui/ClearMazeMenuItem.java | 35 +++++++++++ src/ui/MazeMenu.java | 8 +++ src/ui/NewClearMazeMenuItem.java | 30 +++++++++ src/ui/NewMazeSpinnerBox.java | 101 +++++++++++++++++++++++++++++++ src/ui/WindowPanel.java | 6 +- 9 files changed, 298 insertions(+), 16 deletions(-) create mode 100644 src/ui/AddLineMenuItem.java create mode 100644 src/ui/AddRowMenuItem.java create mode 100644 src/ui/ClearMazeMenuItem.java create mode 100644 src/ui/NewClearMazeMenuItem.java create mode 100644 src/ui/NewMazeSpinnerBox.java diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 9f85bad..3290166 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -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; diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index b75cb4f..027064e 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -12,15 +12,15 @@ import maze.*; public class MazeAppModel extends Observable { - private String filename; + private String filename; private Maze maze; private VertexInterface departure; private VertexInterface arrival; private Previous previous; private ArrayList shortest; private ArrayList boxList; - private boolean saved = true; - private boolean pathDrawn = false; + private boolean saved = true; + private boolean pathDrawn = false; public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); @@ -89,18 +89,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 +131,62 @@ public class MazeAppModel extends Observable { initModelFromMaze(); saved = false; } + + 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; + } } diff --git a/src/ui/AddLineMenuItem.java b/src/ui/AddLineMenuItem.java new file mode 100644 index 0000000..2652d37 --- /dev/null +++ b/src/ui/AddLineMenuItem.java @@ -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(); + } +} diff --git a/src/ui/AddRowMenuItem.java b/src/ui/AddRowMenuItem.java new file mode 100644 index 0000000..4eb69e1 --- /dev/null +++ b/src/ui/AddRowMenuItem.java @@ -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(); + } +} diff --git a/src/ui/ClearMazeMenuItem.java b/src/ui/ClearMazeMenuItem.java new file mode 100644 index 0000000..4911494 --- /dev/null +++ b/src/ui/ClearMazeMenuItem.java @@ -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"); + } +} diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index e257339..91339f1 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -7,6 +7,10 @@ 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; public MazeMenu(MazeApp mazeApp) { @@ -15,5 +19,9 @@ public class MazeMenu extends JMenu { add(drawMenuItem = new DrawShortestPathMenuItem(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)); } } diff --git a/src/ui/NewClearMazeMenuItem.java b/src/ui/NewClearMazeMenuItem.java new file mode 100644 index 0000000..19cc68a --- /dev/null +++ b/src/ui/NewClearMazeMenuItem.java @@ -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(); + } +} diff --git a/src/ui/NewMazeSpinnerBox.java b/src/ui/NewMazeSpinnerBox.java new file mode 100644 index 0000000..ae43d2a --- /dev/null +++ b/src/ui/NewMazeSpinnerBox.java @@ -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()); + } +} diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 8ff60ca..368c0b2 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -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) { From 024a4f70c384ebdc08375a70afb1dd4c36bb920d Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 31 Dec 2017 20:25:16 +0100 Subject: [PATCH 4/5] Fix add column/line and spinners, cleanup Maintest * Remove legacy test code in MainTest maze: * Add methods to add/del lines and columns * Add various calls to matrixDeepCopy, allowing to overwrite only a part of a matrix model: * Better delegation to Maze : let Maze handle column/line adds/deletions ui: * Add buttons to context menu to add/remove lines and columns * Add buttons to Maze Menu to delete lines and columns * Fix the spinners, it used to call a new instance of itself, which made impossible to simple get the values from the spinners --- src/MainTest.java | 22 ---- src/maze/Maze.java | 220 +++++++++++++++++++++++++++++---- src/model/MazeAppModel.java | 73 +++++------ src/ui/AddLineMenuItem.java | 19 ++- src/ui/AddRowMenuItem.java | 22 +++- src/ui/BoxTypeEditionMenu.java | 9 ++ src/ui/ClearMazeMenuItem.java | 11 +- src/ui/DelLineMenuItem.java | 38 ++++++ src/ui/DelRowMenuItem.java | 37 ++++++ src/ui/MazeApp.java | 37 ++++++ src/ui/MazeMenu.java | 4 + src/ui/NewMazeSpinnerBox.java | 46 +++---- 12 files changed, 403 insertions(+), 135 deletions(-) create mode 100644 src/ui/DelLineMenuItem.java create mode 100644 src/ui/DelRowMenuItem.java diff --git a/src/MainTest.java b/src/MainTest.java index 66cdb93..5cb5c94 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,30 +1,8 @@ -import java.util.ArrayList; -import dijkstra.*; -import maze.*; import ui.*; public class MainTest { 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 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(); - } - } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 3290166..45c54b5 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -43,51 +43,89 @@ public class Maze } public Maze(Box[][] newMaze) { - this.dimensionX = newMaze[0].length; this.dimensionY = newMaze.length; + if (dimensionY > 0) { + this.dimensionX = newMaze[0].length; + } + else { + this.dimensionX = 0; + } this.maze = matrixDeepCopy(newMaze); } - private Box[][] matrixDeepCopy (Box[][] original) { - int numberOfLines = 0; - int numberOfRows = 0; - Box[][] copy; - /* - * We first get the dimensions of the matrix - */ - for (Box[] lineBoxes: original) { - numberOfLines++; + private Box[][] matrixDeepCopy(Box[][] original, + Box[][] destination, + int numberOfLines, + int numberOfRows, + int lineOffset, + int rowOffset) + { + if (destination == null) { + destination = new Box[numberOfLines][numberOfRows]; } - if (numberOfLines > 0) { - for (Box box: original[0]) { - numberOfRows++; - } + if (original.length < 1) { + return destination; } - copy = new Box[numberOfLines][numberOfRows]; - - /* - * Now we proceed to the deep copy of each box - */ - for (int line = 0; line < numberOfLines; line++) { - for (int row = 0; row < numberOfRows; row++) { + for (int line = lineOffset; line < lineOffset + numberOfLines; line++) { + for (int row = rowOffset; row < rowOffset + numberOfRows; row++) { Box box = original[line][row]; if (box.isWalkable()) { 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()) { - 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 { - 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 { - 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) { @@ -130,6 +168,136 @@ public class 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) { Box startBox = (Box) start ; diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 027064e..e2467a2 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -134,59 +134,52 @@ public class MazeAppModel extends Observable { 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); + maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows)); initModelFromMaze(); saved = false; } public void addEmptyColumn() { + addEmptyColumn(maze.getDimensionX()); + } - 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]; - } - } - } - + 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()); + } - 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]; - } - } - } - + 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()); + } } diff --git a/src/ui/AddLineMenuItem.java b/src/ui/AddLineMenuItem.java index 2652d37..095760d 100644 --- a/src/ui/AddLineMenuItem.java +++ b/src/ui/AddLineMenuItem.java @@ -9,17 +9,30 @@ 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 arg0) { - - this.mazeApp.getMazeAppModel().addEmptyLine(); + public void actionPerformed(ActionEvent e) { + if (lineNum == -1) { + mazeApp.addEmptyLine(); + } + else { + mazeApp.addEmptyLine(lineNum); + } } } diff --git a/src/ui/AddRowMenuItem.java b/src/ui/AddRowMenuItem.java index 4eb69e1..b49dfd7 100644 --- a/src/ui/AddRowMenuItem.java +++ b/src/ui/AddRowMenuItem.java @@ -3,25 +3,37 @@ 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; + 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 arg0) { - - this.mazeApp.getMazeAppModel().addEmptyColumn(); + public void actionPerformed(ActionEvent e) { + if (rowNum == -1) { + mazeApp.addEmptyColumn(); + } + else { + mazeApp.addEmptyColumn(rowNum); + } } } diff --git a/src/ui/BoxTypeEditionMenu.java b/src/ui/BoxTypeEditionMenu.java index 601fafc..3f07272 100644 --- a/src/ui/BoxTypeEditionMenu.java +++ b/src/ui/BoxTypeEditionMenu.java @@ -9,6 +9,10 @@ public class BoxTypeEditionMenu extends JPopupMenu { 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)); @@ -21,5 +25,10 @@ public class BoxTypeEditionMenu extends JPopupMenu { 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)); } } diff --git a/src/ui/ClearMazeMenuItem.java b/src/ui/ClearMazeMenuItem.java index 4911494..aeed823 100644 --- a/src/ui/ClearMazeMenuItem.java +++ b/src/ui/ClearMazeMenuItem.java @@ -3,8 +3,6 @@ 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; @@ -23,13 +21,6 @@ public class ClearMazeMenuItem extends JMenuItem @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"); + mazeApp.clearMaze(); } } diff --git a/src/ui/DelLineMenuItem.java b/src/ui/DelLineMenuItem.java new file mode 100644 index 0000000..165ac6e --- /dev/null +++ b/src/ui/DelLineMenuItem.java @@ -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); + } + } +} diff --git a/src/ui/DelRowMenuItem.java b/src/ui/DelRowMenuItem.java new file mode 100644 index 0000000..243b129 --- /dev/null +++ b/src/ui/DelRowMenuItem.java @@ -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); + } + } +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 48c54c5..0f43f77 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -87,4 +87,41 @@ public class MazeApp extends JFrame mazeAppModel.setPathDrawn(false); 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(); + } } diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index 91339f1..76d8639 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -11,6 +11,8 @@ public class MazeMenu extends JMenu { private final ClearMazeMenuItem clearMazeMenuItem; private final AddRowMenuItem addRowMenuItem; private final AddLineMenuItem addLineMenuItem; + private final DelRowMenuItem delRowMenuItem; + private final DelLineMenuItem delLineMenuItem; public MazeMenu(MazeApp mazeApp) { @@ -23,5 +25,7 @@ public class MazeMenu extends JMenu { 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)); } } diff --git a/src/ui/NewMazeSpinnerBox.java b/src/ui/NewMazeSpinnerBox.java index ae43d2a..2e5397c 100644 --- a/src/ui/NewMazeSpinnerBox.java +++ b/src/ui/NewMazeSpinnerBox.java @@ -15,23 +15,17 @@ 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 { + implements ActionListener { 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 :"}; @@ -40,25 +34,23 @@ public class NewMazeSpinnerBox extends JPanel int maximumValue = 200; this.lineSpinnerModel = - new SpinnerNumberModel(initialValue, //initial value - initialValue, //min - maximumValue, //max - 1); - this.lineSpinnerModel.addChangeListener(this); + 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); - this.rowSpinnerModel.addChangeListener(this); - JSpinner rowSpinner = addLabeledSpinner(this, labels[0], 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) { + String label, + SpinnerModel model) { JLabel l = new JLabel(label); c.add(l); @@ -76,8 +68,8 @@ public class NewMazeSpinnerBox extends JPanel spinnerDialog.setLayout(new BorderLayout()); spinnerDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); - spinnerDialog.add(new NewMazeSpinnerBox(this.mazeApp), - BorderLayout.CENTER); + spinnerDialog.add(this, + BorderLayout.CENTER); JButton OKButton = new JButton("OK"); OKButton.addActionListener(this); @@ -89,13 +81,9 @@ public class NewMazeSpinnerBox extends JPanel @Override public void actionPerformed(ActionEvent e) { + mazeApp.getMazeAppModel( + ).newClearMaze((int) lineSpinnerModel.getValue(), + (int) rowSpinnerModel.getValue()); spinnerDialog.dispose(); } - - @Override - public void stateChanged(ChangeEvent arg0) { - this.mazeApp.getMazeAppModel() - .newClearMaze((int) this.lineSpinnerModel.getValue(), - (int) this.rowSpinnerModel.getValue()); - } } From 43d58c815b770f579a25604688a404e6cc167a4e Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Mon, 1 Jan 2018 18:32:38 +0100 Subject: [PATCH 5/5] Fix null pointer exception when there is no path maze: * Add methods to Box and Maze to reset the neighbor lists of boxes model: * If the neighbor list of the arrival box is null, it means that no path exist between departure and arrival, and we therefore display the pop-up which is already displayed when the user tries to draw the path on a maze where there is no departure and/or arrival --- src/maze/Box.java | 6 +++++- src/maze/Maze.java | 8 ++++++++ src/model/MazeAppModel.java | 11 ++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/maze/Box.java b/src/maze/Box.java index 0c17d17..d96a886 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -35,7 +35,11 @@ public abstract class Box public ArrayList generateNeighbours() { this.neighbourList = maze.generateNeighbours(this); - return maze.generateNeighbours(this) ; + return this.neighbourList; + } + + public void resetNeighbourList() { + neighbourList = null; } public boolean isWalkable() { diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 45c54b5..d587ea8 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -510,4 +510,12 @@ public class Maze return maze; } + public void resetNeighbourLists() { + for (Box[] line: maze) { + for (Box box: line) { + box.resetNeighbourList(); + } + } + } + } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index e2467a2..791ce36 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -37,7 +37,12 @@ public class MazeAppModel extends Observable { } else { previous = (Previous) Dijkstra.dijkstra(maze, departure); - shortest = previous.getShortestPathTo(arrival); + if (((Box) arrival).getNeighbourList() == null) { + shortest = null; + } + else { + shortest = previous.getShortestPathTo(arrival); + } } setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); @@ -98,12 +103,14 @@ public class MazeAppModel extends Observable { 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; } @@ -116,6 +123,7 @@ public class MazeAppModel extends Observable { new EBox("E", departureLine, departureRow, maze); } maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze); + maze.resetNeighbourLists(); initModelFromMaze(); saved = false; } @@ -128,6 +136,7 @@ public class MazeAppModel extends Observable { new EBox("E", arrivalLine, arrivalRow, maze); } maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze); + maze.resetNeighbourLists(); initModelFromMaze(); saved = false; }