From 0e398a7b572e7227f46ff25a1ce44d37e6423565 Mon Sep 17 00:00:00 2001 From: Antonin GODARD Date: Mon, 11 Dec 2017 11:42:01 +0100 Subject: [PATCH 01/37] Add UI package and JMenuBar --- src/MainTest.java | 3 +++ src/ui/FileMenu.java | 19 +++++++++++++++++++ src/ui/MazeApp.java | 22 ++++++++++++++++++++++ src/ui/MazeMenuBar.java | 18 ++++++++++++++++++ src/ui/QuitMenuItem.java | 16 ++++++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 src/ui/FileMenu.java create mode 100644 src/ui/MazeApp.java create mode 100644 src/ui/MazeMenuBar.java create mode 100644 src/ui/QuitMenuItem.java diff --git a/src/MainTest.java b/src/MainTest.java index 599e8fb..b7cd87e 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import dijkstra.*; import maze.*; +import ui.*; public class MainTest { @@ -18,6 +19,8 @@ public class MainTest { } mazeTest.saveToTextFile("data/labyOutput.txt"); + + new MazeApp(); } diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java new file mode 100644 index 0000000..56f0b97 --- /dev/null +++ b/src/ui/FileMenu.java @@ -0,0 +1,19 @@ +package ui; + +import javax.swing.JMenu; + +public class FileMenu extends JMenu { + + private final MazeApp mazeApp; + private final QuitMenuItem quitMenuItem; + + public FileMenu(MazeApp mazeApp) { + + super("File"); + this.mazeApp = mazeApp; + + add(quitMenuItem = new QuitMenuItem(mazeApp)); + + } + +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java new file mode 100644 index 0000000..3a27c97 --- /dev/null +++ b/src/ui/MazeApp.java @@ -0,0 +1,22 @@ +package ui; + +import javax.swing.*; + +public class MazeApp extends JFrame { + + private final MazeMenuBar mazeMenuBar; + + public MazeApp() { + + super("Maze Solving Application"); + + setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + pack(); + setVisible(true); + + } + +} diff --git a/src/ui/MazeMenuBar.java b/src/ui/MazeMenuBar.java new file mode 100644 index 0000000..f8d47da --- /dev/null +++ b/src/ui/MazeMenuBar.java @@ -0,0 +1,18 @@ +package ui; + +import javax.swing.JMenuBar; + +public class MazeMenuBar extends JMenuBar { + + private final MazeApp mazeApp; + private final FileMenu fileMenu; + + public MazeMenuBar(MazeApp mazeApp) { + + super(); + this.mazeApp = mazeApp; + + add(fileMenu = new FileMenu(mazeApp)); + + } +} diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java new file mode 100644 index 0000000..66caac6 --- /dev/null +++ b/src/ui/QuitMenuItem.java @@ -0,0 +1,16 @@ +package ui; + +import javax.swing.JMenuItem; + +public class QuitMenuItem extends JMenuItem { + + private final MazeApp mazeApp; + + public QuitMenuItem(MazeApp mazeApp) { + + super("Quit"); + this.mazeApp = mazeApp; + + } + +} -- 2.56.1 From 94f4ab4e5a64425117d517db4bf8ab4ee45c24dd Mon Sep 17 00:00:00 2001 From: Antonin GODARD Date: Wed, 13 Dec 2017 10:11:20 +0100 Subject: [PATCH 02/37] Add grid layout for the maze Still have to add the maze to MazeAppModel and delete reference in ui classes --- .gitignore | 3 ++ data/labyOutput.txt | 16 +++++----- src/MainTest.java | 2 +- src/model/MazeAppModel.java | 6 ++++ src/ui/ArrivalDrawingBox.java | 11 +++++++ src/ui/DepartureDrawingBox.java | 11 +++++++ src/ui/DrawingBox.java | 20 ++++++++++++ src/ui/DrawingPanelGrid.java | 54 +++++++++++++++++++++++++++++++++ src/ui/EmptyDrawingBox.java | 11 +++++++ src/ui/MazeApp.java | 6 +++- src/ui/WallDrawingBox.java | 11 +++++++ src/ui/WindowPanel.java | 19 ++++++++++++ 12 files changed, 160 insertions(+), 10 deletions(-) create mode 100644 src/model/MazeAppModel.java create mode 100644 src/ui/ArrivalDrawingBox.java create mode 100644 src/ui/DepartureDrawingBox.java create mode 100644 src/ui/DrawingBox.java create mode 100644 src/ui/DrawingPanelGrid.java create mode 100644 src/ui/EmptyDrawingBox.java create mode 100644 src/ui/WallDrawingBox.java create mode 100644 src/ui/WindowPanel.java diff --git a/.gitignore b/.gitignore index 5e1d7a3..e527c41 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ *.css src/package-list src/script.js +/bin/ +*.classpath +*.project diff --git a/data/labyOutput.txt b/data/labyOutput.txt index c27fe74..ca83a98 100644 --- a/data/labyOutput.txt +++ b/data/labyOutput.txt @@ -1,8 +1,8 @@ -WEEEEEWW -EDWWWEEW -WEEWWWEE -WWEEEEEW -EEEWWWWW -EWWWWAWW -EWWEEEWW -EEEEWWWW +WEWWEEEE +EDEWEWWE +EWEEEWWE +EWWEWWEE +EWWEWWEW +EEWEWAEW +WEEEWWWW +WWEWWWWW diff --git a/src/MainTest.java b/src/MainTest.java index b7cd87e..9802546 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -20,7 +20,7 @@ public class MainTest { mazeTest.saveToTextFile("data/labyOutput.txt"); - new MazeApp(); + new MazeApp(mazeTest); } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java new file mode 100644 index 0000000..9ac6941 --- /dev/null +++ b/src/model/MazeAppModel.java @@ -0,0 +1,6 @@ +package model; + +public class MazeAppModel { + + +} diff --git a/src/ui/ArrivalDrawingBox.java b/src/ui/ArrivalDrawingBox.java new file mode 100644 index 0000000..6677e9a --- /dev/null +++ b/src/ui/ArrivalDrawingBox.java @@ -0,0 +1,11 @@ +package ui; + +import java.awt.Color; + +public class ArrivalDrawingBox extends DrawingBox { + + public ArrivalDrawingBox(MazeApp mazeApp, Color color) { + super(mazeApp, color); + } + +} diff --git a/src/ui/DepartureDrawingBox.java b/src/ui/DepartureDrawingBox.java new file mode 100644 index 0000000..3c21ec1 --- /dev/null +++ b/src/ui/DepartureDrawingBox.java @@ -0,0 +1,11 @@ +package ui; + +import java.awt.Color; + +public class DepartureDrawingBox extends DrawingBox { + + public DepartureDrawingBox(MazeApp mazeApp, Color color) { + super(mazeApp, color); + } + +} diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java new file mode 100644 index 0000000..e2b3a36 --- /dev/null +++ b/src/ui/DrawingBox.java @@ -0,0 +1,20 @@ +package ui; + +import java.awt.Color; +import java.awt.Dimension; +import javax.swing.JPanel; + +public abstract class DrawingBox extends JPanel { + + private final MazeApp mazeApp; + private Color color; + + public DrawingBox(MazeApp mazeApp, Color color) { + + this.mazeApp = mazeApp; + this.color = color; + + setBackground(color); + setPreferredSize(new Dimension(20,20)); + } +} diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java new file mode 100644 index 0000000..624a29b --- /dev/null +++ b/src/ui/DrawingPanelGrid.java @@ -0,0 +1,54 @@ +package ui; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.GridLayout; +import javax.swing.JPanel; +import maze.Maze; + +public class DrawingPanelGrid extends JPanel { + + private final MazeApp mazeApp; + private Maze maze; + private int dimensionX; + private int dimensionY; + + public DrawingPanelGrid(MazeApp mazeApp, Maze maze) { + + this.mazeApp = mazeApp; + this.maze = maze; + this.dimensionX = maze.getDimensionX(); + this.dimensionY = maze.getDimensionY(); + + setLayout(new GridLayout(this.dimensionX, this.dimensionY)); + initFromMaze(maze); + + setPreferredSize(new Dimension(256, 256)); + } + + private void initFromMaze(Maze maze) { + + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + + for (int i = 0; i < dimensionX; i++) { + for (int j = 0; j < dimensionY; j++) { + if (maze.getMaze()[j][i].isWalkable()) { + if (maze.getMaze()[j][i].isDeparture()) { + add(new DepartureDrawingBox(mazeApp, Color.GREEN)); + } + else if (maze.getMaze()[j][i].isArrival()) { + add(new ArrivalDrawingBox(mazeApp, Color.RED)); + } + else { + add(new EmptyDrawingBox(mazeApp, Color.BLACK)); + } + } + else { + add(new WallDrawingBox(mazeApp, Color.WHITE)); + } + } + } + + } +} diff --git a/src/ui/EmptyDrawingBox.java b/src/ui/EmptyDrawingBox.java new file mode 100644 index 0000000..1b649d3 --- /dev/null +++ b/src/ui/EmptyDrawingBox.java @@ -0,0 +1,11 @@ +package ui; + +import java.awt.Color; + +public class EmptyDrawingBox extends DrawingBox { + + public EmptyDrawingBox(MazeApp mazeApp, Color color) { + super(mazeApp, color); + } + +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 3a27c97..f60cb6e 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -1,16 +1,20 @@ package ui; import javax.swing.*; +import maze.Maze; public class MazeApp extends JFrame { private final MazeMenuBar mazeMenuBar; + private final WindowPanel windowPanel; + private Maze maze; - public MazeApp() { + public MazeApp(Maze maze) { super("Maze Solving Application"); setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); + setContentPane(windowPanel = new WindowPanel(this, maze)) ; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/src/ui/WallDrawingBox.java b/src/ui/WallDrawingBox.java new file mode 100644 index 0000000..207bc7a --- /dev/null +++ b/src/ui/WallDrawingBox.java @@ -0,0 +1,11 @@ +package ui; + +import java.awt.Color; + +public class WallDrawingBox extends DrawingBox { + + public WallDrawingBox(MazeApp mazeApp, Color color) { + super(mazeApp, color); + } + +} diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java new file mode 100644 index 0000000..422753d --- /dev/null +++ b/src/ui/WindowPanel.java @@ -0,0 +1,19 @@ +package ui; + +import javax.swing.*; +import maze.Maze; +import java.awt.*; + +public class WindowPanel extends JPanel +{ + private final DrawingPanelGrid drawingPanelGrid; + private Maze maze; + + public WindowPanel(MazeApp mazeApp, Maze maze) + { + setLayout(new BorderLayout()); + + add(drawingPanelGrid = new DrawingPanelGrid(mazeApp, maze), BorderLayout.CENTER); + } + +} -- 2.56.1 From 892d7e6ff3b45c591e845d21ec40e32ed2219a64 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Thu, 14 Dec 2017 09:54:46 +0100 Subject: [PATCH 03/37] Add maze in Model, then modify UI methods using maze. --- src/MainTest.java | 2 +- src/model/MazeAppModel.java | 14 ++++++++++++-- src/ui/DrawingBox.java | 4 ++-- src/ui/DrawingPanelGrid.java | 4 ++-- src/ui/MazeApp.java | 19 +++++++++++-------- src/ui/QuitMenuItem.java | 1 - src/ui/WindowPanel.java | 7 +++---- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/MainTest.java b/src/MainTest.java index 9802546..b7cd87e 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -20,7 +20,7 @@ public class MainTest { mazeTest.saveToTextFile("data/labyOutput.txt"); - new MazeApp(mazeTest); + new MazeApp(); } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 9ac6941..1694450 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -1,6 +1,16 @@ package model; +import maze.Maze; + public class MazeAppModel { - -} + private Maze maze = new Maze("data/labyrinthe.txt"); + + public void setMaze(Maze maze) { + this.maze = maze; + } + + public Maze getMaze() { + return maze; + } +} \ No newline at end of file diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java index e2b3a36..2397c7e 100644 --- a/src/ui/DrawingBox.java +++ b/src/ui/DrawingBox.java @@ -12,9 +12,9 @@ public abstract class DrawingBox extends JPanel { public DrawingBox(MazeApp mazeApp, Color color) { this.mazeApp = mazeApp; - this.color = color; + this.color = color; setBackground(color); - setPreferredSize(new Dimension(20,20)); + setPreferredSize(new Dimension(40,40)); } } diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index 624a29b..4671537 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -13,10 +13,10 @@ public class DrawingPanelGrid extends JPanel { private int dimensionX; private int dimensionY; - public DrawingPanelGrid(MazeApp mazeApp, Maze maze) { + public DrawingPanelGrid(MazeApp mazeApp) { this.mazeApp = mazeApp; - this.maze = maze; + this.maze = mazeApp.getMazeAppModel().getMaze(); this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index f60cb6e..3dbb3e6 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -2,25 +2,28 @@ package ui; import javax.swing.*; import maze.Maze; +import model.MazeAppModel; public class MazeApp extends JFrame { - private final MazeMenuBar mazeMenuBar; - private final WindowPanel windowPanel; - private Maze maze; - - public MazeApp(Maze maze) { + private final MazeMenuBar mazeMenuBar; + private final WindowPanel windowPanel; + private MazeAppModel mazeAppModel = new MazeAppModel(); + + public MazeApp() { super("Maze Solving Application"); setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); - setContentPane(windowPanel = new WindowPanel(this, maze)) ; + setContentPane(windowPanel = new WindowPanel(this)) ; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); - } - + + public MazeAppModel getMazeAppModel() { + return mazeAppModel; + } } diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 66caac6..30b3196 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -10,7 +10,6 @@ public class QuitMenuItem extends JMenuItem { super("Quit"); this.mazeApp = mazeApp; - } } diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 422753d..55ec5c0 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -7,13 +7,12 @@ import java.awt.*; public class WindowPanel extends JPanel { private final DrawingPanelGrid drawingPanelGrid; - private Maze maze; - public WindowPanel(MazeApp mazeApp, Maze maze) - { + public WindowPanel(MazeApp mazeApp) { + setLayout(new BorderLayout()); - add(drawingPanelGrid = new DrawingPanelGrid(mazeApp, maze), BorderLayout.CENTER); + add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); } } -- 2.56.1 From 40c05909e6a1acb85e949cd6620b62ef13778775 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 16 Dec 2017 13:15:25 +0100 Subject: [PATCH 04/37] Add orange borders to the printed boxes --- src/ui/DrawingBox.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java index 2397c7e..bca18ac 100644 --- a/src/ui/DrawingBox.java +++ b/src/ui/DrawingBox.java @@ -3,6 +3,7 @@ package ui; import java.awt.Color; import java.awt.Dimension; import javax.swing.JPanel; +import javax.swing.BorderFactory; public abstract class DrawingBox extends JPanel { @@ -16,5 +17,8 @@ public abstract class DrawingBox extends JPanel { setBackground(color); setPreferredSize(new Dimension(40,40)); + setBorder(BorderFactory.createLineBorder(Color.orange)); + + } } -- 2.56.1 From f8623fdd7a5d0e74b059a89edd03786b21a8f810 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 16 Dec 2017 14:51:39 +0100 Subject: [PATCH 05/37] Add Observer and Observable implementation Add update method, notifyForUpdates methods Currently not working --- src/maze/Maze.java | 26 +++++++++++++++++++ src/model/Box.java | 11 +++++++++ src/model/Maze.java | 9 +++++++ src/model/MazeAppModel.java | 16 +++++++++--- src/model/Previous.java | 14 +++++++++++ src/ui/ArrivalDrawingBox.java | 7 ++++++ src/ui/DepartureDrawingBox.java | 7 ++++++ src/ui/DrawingBox.java | 14 +++++++++++ src/ui/DrawingPanelGrid.java | 44 +++++++++++++++++++++++++-------- src/ui/EmptyDrawingBox.java | 7 ++++++ src/ui/MazeApp.java | 12 ++++++++- src/ui/WallDrawingBox.java | 7 ++++++ src/ui/WindowPanel.java | 5 ++++ 13 files changed, 165 insertions(+), 14 deletions(-) create mode 100644 src/model/Box.java create mode 100644 src/model/Maze.java create mode 100644 src/model/Previous.java diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 6d7ce44..07f8dcb 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -59,6 +59,32 @@ public class Maze return vertexList ; } + /**Method to determine the departure in the maze. Returns null if the departure is non-existent. + * + * @return Departure vertex + */ + public VertexInterface getDeparture() { + + ArrayList vertexes = getVertexes() ; + for (VertexInterface vertex : vertexes) { + if (((Box) vertex).isDeparture()) return vertex; + } + return null; + } + + /**Method to determine the arrival in the maze. Returns null if the arrival is non-existent. + * + * @return Arrival vertex + */ + public VertexInterface getArrival() { + + ArrayList vertexes = getVertexes() ; + for (VertexInterface vertex : vertexes) { + if (((Box) vertex).isArrival()) return vertex; + } + return null; + } + public ArrayList generateNeighbours(VertexInterface vertex) { Box box = (Box)vertex ; diff --git a/src/model/Box.java b/src/model/Box.java new file mode 100644 index 0000000..c5d1975 --- /dev/null +++ b/src/model/Box.java @@ -0,0 +1,11 @@ +package model; + +import maze.Maze; + +public class Box extends maze.Box { + + public Box(String label, int x, int y, Maze maze) { + super(label, x, y, maze); + } + +} diff --git a/src/model/Maze.java b/src/model/Maze.java new file mode 100644 index 0000000..3bd6162 --- /dev/null +++ b/src/model/Maze.java @@ -0,0 +1,9 @@ +package model; + +public class Maze extends maze.Maze { + + public Maze(String filename) { + super(filename); + } + +} diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 1694450..3b6e9fc 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -1,10 +1,20 @@ package model; -import maze.Maze; +import java.util.ArrayList; +import java.util.Observable; -public class MazeAppModel { +import dijkstra.VertexInterface; +import dijkstra.Dijkstra; - private Maze maze = new Maze("data/labyrinthe.txt"); +public class MazeAppModel extends Observable { + + private Maze maze = new Maze("data/labyrinthe.txt"); + private VertexInterface departure = maze.getDeparture(); + private VertexInterface arrival = maze.getArrival(); + private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure); + private ArrayList shortest = previous.getShortestPathTo(arrival); + private ArrayList boxList = maze.getVertexes(); + private Boolean modified = false; public void setMaze(Maze maze) { this.maze = maze; diff --git a/src/model/Previous.java b/src/model/Previous.java new file mode 100644 index 0000000..24d42cf --- /dev/null +++ b/src/model/Previous.java @@ -0,0 +1,14 @@ +package model; + +import java.util.ArrayList; +import java.util.Hashtable; + +import dijkstra.VertexInterface; + +public class Previous extends dijkstra.Previous { + + public Previous(Hashtable hashPrevious, ArrayList shortestPath) { + super(hashPrevious, shortestPath); + } + +} diff --git a/src/ui/ArrivalDrawingBox.java b/src/ui/ArrivalDrawingBox.java index 6677e9a..c8ad9a7 100644 --- a/src/ui/ArrivalDrawingBox.java +++ b/src/ui/ArrivalDrawingBox.java @@ -1,11 +1,18 @@ package ui; import java.awt.Color; +import java.awt.Graphics; public class ArrivalDrawingBox extends DrawingBox { public ArrivalDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } + + @Override + protected final void paintComponent(Graphics g) { + super.paintComponent(g); + setBackground(Color.RED); + } } diff --git a/src/ui/DepartureDrawingBox.java b/src/ui/DepartureDrawingBox.java index 3c21ec1..87278ac 100644 --- a/src/ui/DepartureDrawingBox.java +++ b/src/ui/DepartureDrawingBox.java @@ -1,6 +1,7 @@ package ui; import java.awt.Color; +import java.awt.Graphics; public class DepartureDrawingBox extends DrawingBox { @@ -8,4 +9,10 @@ public class DepartureDrawingBox extends DrawingBox { super(mazeApp, color); } + @Override + protected final void paintComponent(Graphics g) { + + super.paintComponent(g); + setBackground(Color.GREEN); + } } diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java index 2397c7e..33c9e8d 100644 --- a/src/ui/DrawingBox.java +++ b/src/ui/DrawingBox.java @@ -2,6 +2,8 @@ package ui; import java.awt.Color; import java.awt.Dimension; +import java.awt.Graphics; + import javax.swing.JPanel; public abstract class DrawingBox extends JPanel { @@ -17,4 +19,16 @@ public abstract class DrawingBox extends JPanel { setBackground(color); setPreferredSize(new Dimension(40,40)); } + + @Override + protected void paintComponent(Graphics g) { + + super.paintComponent(g) ; + setBackground(color); + } + + public void notifyForUpdates(Object param) { + + repaint(); + } } diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index 4671537..fdf8c1e 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -8,17 +8,19 @@ import maze.Maze; public class DrawingPanelGrid extends JPanel { - private final MazeApp mazeApp; - private Maze maze; - private int dimensionX; - private int dimensionY; + private final MazeApp mazeApp; + private Maze maze; + private DrawingBox[][] drawingBoxMatrix; + private int dimensionX; + private int dimensionY; public DrawingPanelGrid(MazeApp mazeApp) { - this.mazeApp = mazeApp; - this.maze = mazeApp.getMazeAppModel().getMaze(); + this.mazeApp = mazeApp; + this.maze = mazeApp.getMazeAppModel().getMaze(); this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); + this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY]; setLayout(new GridLayout(this.dimensionX, this.dimensionY)); initFromMaze(maze); @@ -28,27 +30,49 @@ public class DrawingPanelGrid extends JPanel { private void initFromMaze(Maze maze) { - int dimensionX = maze.getDimensionX(); - int dimensionY = maze.getDimensionY(); - for (int i = 0; i < dimensionX; i++) { for (int j = 0; j < dimensionY; j++) { if (maze.getMaze()[j][i].isWalkable()) { if (maze.getMaze()[j][i].isDeparture()) { add(new DepartureDrawingBox(mazeApp, Color.GREEN)); + drawingBoxMatrix[j][i] = new DepartureDrawingBox(mazeApp, Color.GREEN); } else if (maze.getMaze()[j][i].isArrival()) { add(new ArrivalDrawingBox(mazeApp, Color.RED)); + drawingBoxMatrix[j][i] = new ArrivalDrawingBox(mazeApp, Color.RED); } else { add(new EmptyDrawingBox(mazeApp, Color.BLACK)); + drawingBoxMatrix[j][i] = new EmptyDrawingBox(mazeApp, Color.BLACK); } } else { add(new WallDrawingBox(mazeApp, Color.WHITE)); + drawingBoxMatrix[j][i] = new WallDrawingBox(mazeApp, Color.WHITE); + } + } + } + } + + public void notifyForUpdates(Object param) { + + for (int i = 0; i < dimensionX; i++) { + for (int j = 0; j < dimensionY; j++) { + if (maze.getMaze()[j][i].isWalkable()) { + if (maze.getMaze()[j][i].isDeparture()) { + drawingBoxMatrix[j][i].notifyForUpdates(); + } + else if (maze.getMaze()[j][i].isArrival()) { + drawingBoxMatrix[j][i].notifyForUpdates(); + } + else { + drawingBoxMatrix[j][i].notifyForUpdates(); + } + } + else { + drawingBoxMatrix[j][i].notifyForUpdates(); } } } - } } diff --git a/src/ui/EmptyDrawingBox.java b/src/ui/EmptyDrawingBox.java index 1b649d3..bbb7d62 100644 --- a/src/ui/EmptyDrawingBox.java +++ b/src/ui/EmptyDrawingBox.java @@ -1,6 +1,7 @@ package ui; import java.awt.Color; +import java.awt.Graphics; public class EmptyDrawingBox extends DrawingBox { @@ -8,4 +9,10 @@ public class EmptyDrawingBox extends DrawingBox { super(mazeApp, color); } + @Override + protected final void paintComponent(Graphics g) { + + super.paintComponent(g); + setBackground(Color.WHITE); + } } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 3dbb3e6..84c95e2 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -4,7 +4,11 @@ import javax.swing.*; import maze.Maze; import model.MazeAppModel; -public class MazeApp extends JFrame { +import java.util.Observable; +import java.util.Observer; + +public class MazeApp extends JFrame + implements Observer { private final MazeMenuBar mazeMenuBar; private final WindowPanel windowPanel; @@ -26,4 +30,10 @@ public class MazeApp extends JFrame { public MazeAppModel getMazeAppModel() { return mazeAppModel; } + + @Override + public void update(Observable obs, Object param) { + + windowPanel.notifyForUpdates(param); + } } diff --git a/src/ui/WallDrawingBox.java b/src/ui/WallDrawingBox.java index 207bc7a..59ef424 100644 --- a/src/ui/WallDrawingBox.java +++ b/src/ui/WallDrawingBox.java @@ -1,6 +1,7 @@ package ui; import java.awt.Color; +import java.awt.Graphics; public class WallDrawingBox extends DrawingBox { @@ -8,4 +9,10 @@ public class WallDrawingBox extends DrawingBox { super(mazeApp, color); } + @Override + protected final void paintComponent(Graphics g) { + + super.paintComponent(g); + setBackground(Color.BLACK); + } } diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 55ec5c0..455933a 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -14,5 +14,10 @@ public class WindowPanel extends JPanel add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); } + + public void notifyForUpdates(Object param) { + + drawingPanelGrid.notifyForUpdates(param); + } } -- 2.56.1 From d8acf6ebe86721c5f6aee9278b7ad88b33bc830e Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 16 Dec 2017 15:15:39 +0100 Subject: [PATCH 06/37] Add ActionListener for QuitMenuItem --- src/model/MazeAppModel.java | 9 +++++++++ src/ui/QuitMenuItem.java | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 1694450..dcb09b9 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -5,6 +5,7 @@ import maze.Maze; public class MazeAppModel { private Maze maze = new Maze("data/labyrinthe.txt"); + private boolean modified = false; public void setMaze(Maze maze) { this.maze = maze; @@ -13,4 +14,12 @@ public class MazeAppModel { public Maze getMaze() { return maze; } + + public boolean isModified() { + return modified; + } + + public void saveToFile() { + maze.saveToTextFile("data/labyOutput.txt"); + } } \ No newline at end of file diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 30b3196..caead12 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -1,8 +1,15 @@ package ui; -import javax.swing.JMenuItem; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; -public class QuitMenuItem extends JMenuItem { +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; + +import model.MazeAppModel; + +public class QuitMenuItem extends JMenuItem + implements ActionListener { private final MazeApp mazeApp; @@ -10,6 +17,31 @@ public class QuitMenuItem extends JMenuItem { super("Quit"); this.mazeApp = mazeApp; + addActionListener(this); } + public void actionPerformed(ActionEvent evt) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + + if (mazeAppModel.isModified()) { + int response = JOptionPane.showInternalOptionDialog(this, + "Maze not saved. Save it ?", + "Quit application", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.WARNING_MESSAGE, + null,null,null) ; + switch (response) { + case JOptionPane.CANCEL_OPTION: + return; + case JOptionPane.OK_OPTION: + mazeAppModel.saveToFile(); + break; + case JOptionPane.NO_OPTION: + break; + } + } + System.exit(0) ; + } + } -- 2.56.1 From a6783e4a0d9d23a90567292931b377c941929507 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 16 Dec 2017 15:47:16 +0100 Subject: [PATCH 07/37] Add LoadMazeMenuItem (not finished) --- src/ui/FileMenu.java | 2 ++ src/ui/LoadMazeMenuItem.java | 40 ++++++++++++++++++++++++++++++++++++ src/ui/QuitMenuItem.java | 29 +++++++++++++------------- 3 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 src/ui/LoadMazeMenuItem.java diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index 56f0b97..c2438d0 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -6,6 +6,7 @@ public class FileMenu extends JMenu { private final MazeApp mazeApp; private final QuitMenuItem quitMenuItem; + private final LoadMazeMenuItem loadMazeMenuItem; public FileMenu(MazeApp mazeApp) { @@ -13,6 +14,7 @@ public class FileMenu extends JMenu { this.mazeApp = mazeApp; add(quitMenuItem = new QuitMenuItem(mazeApp)); + add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp)); } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java new file mode 100644 index 0000000..746181e --- /dev/null +++ b/src/ui/LoadMazeMenuItem.java @@ -0,0 +1,40 @@ +package ui; + +import javax.swing.JFileChooser; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileNameExtensionFilter; + +import model.MazeAppModel; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class LoadMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public LoadMazeMenuItem(MazeApp mazeApp) { + + super("Load maze"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + JFileChooser fileChooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter( + "Text files", "txt"); + fileChooser.setFileFilter(filter); + int returnVal = fileChooser.showOpenDialog(mazeApp); + if(returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to open this file: " + + fileChooser.getSelectedFile().getName()); + String filename = fileChooser.getSelectedFile().getName(); + } + mazeApp.getMazeAppModel().setMaze(model.Maze.); + } +} diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index caead12..806379b 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -25,23 +25,22 @@ public class QuitMenuItem extends JMenuItem MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); if (mazeAppModel.isModified()) { - int response = JOptionPane.showInternalOptionDialog(this, + int response = JOptionPane.showInternalOptionDialog(this, "Maze not saved. Save it ?", "Quit application", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, - null,null,null) ; - switch (response) { - case JOptionPane.CANCEL_OPTION: - return; - case JOptionPane.OK_OPTION: - mazeAppModel.saveToFile(); - break; - case JOptionPane.NO_OPTION: - break; - } - } - System.exit(0) ; - } - + null,null,null); + switch (response) { + case JOptionPane.CANCEL_OPTION: + return; + case JOptionPane.OK_OPTION: + mazeAppModel.saveToFile(); + break; + case JOptionPane.NO_OPTION: + break; + } + } + System.exit(0) ; + } } -- 2.56.1 From a5d0df4b96c6467026c5b57ceb48c487bebde07e Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 16 Dec 2017 16:21:57 +0100 Subject: [PATCH 08/37] Modified LoadMenuItem --- data/labyOutput.txt | 4 ++-- src/model/Maze.java | 9 +++++++++ src/model/MazeAppModel.java | 7 +++++-- src/ui/LoadMazeMenuItem.java | 5 +++-- 4 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src/model/Maze.java diff --git a/data/labyOutput.txt b/data/labyOutput.txt index ca83a98..1436118 100644 --- a/data/labyOutput.txt +++ b/data/labyOutput.txt @@ -4,5 +4,5 @@ EWEEEWWE EWWEWWEE EWWEWWEW EEWEWAEW -WEEEWWWW -WWEWWWWW +WEEEWWWE +WWEWWWWE diff --git a/src/model/Maze.java b/src/model/Maze.java new file mode 100644 index 0000000..3bd6162 --- /dev/null +++ b/src/model/Maze.java @@ -0,0 +1,9 @@ +package model; + +public class Maze extends maze.Maze { + + public Maze(String filename) { + super(filename); + } + +} diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index dcb09b9..f331efe 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -1,14 +1,17 @@ package model; -import maze.Maze; +import java.util.Observable; -public class MazeAppModel { +public class MazeAppModel extends Observable { private Maze maze = new Maze("data/labyrinthe.txt"); private boolean modified = false; public void setMaze(Maze maze) { this.maze = maze; + modified = true; + setChanged(); + notifyObservers(); } public Maze getMaze() { diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 746181e..06d0505 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -6,6 +6,7 @@ import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; import model.MazeAppModel; +import model.Maze; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -33,8 +34,8 @@ public class LoadMazeMenuItem extends JMenuItem if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + fileChooser.getSelectedFile().getName()); - String filename = fileChooser.getSelectedFile().getName(); } - mazeApp.getMazeAppModel().setMaze(model.Maze.); + String filename = fileChooser.getSelectedFile().getPath(); + mazeApp.getMazeAppModel().setMaze(new Maze(filename)); } } -- 2.56.1 From bac9b54d58233eb3da7c224026ac355f10444cc4 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 16 Dec 2017 17:40:35 +0100 Subject: [PATCH 09/37] Fixing lines and spaces and restoring labyOutput --- data/labyOutput.txt | 16 ++++++++-------- src/MainTest.java | 12 ++++++------ src/model/Maze.java | 1 - src/model/MazeAppModel.java | 2 +- src/ui/ArrivalDrawingBox.java | 1 - src/ui/DepartureDrawingBox.java | 1 - src/ui/DrawingBox.java | 6 +++--- src/ui/DrawingPanelGrid.java | 13 ++++++------- src/ui/EmptyDrawingBox.java | 1 - src/ui/FileMenu.java | 8 +++----- src/ui/LoadMazeMenuItem.java | 4 ++-- src/ui/MazeApp.java | 12 ++++++------ src/ui/MazeMenuBar.java | 7 +++---- src/ui/QuitMenuItem.java | 8 ++++---- src/ui/WallDrawingBox.java | 1 - src/ui/WindowPanel.java | 7 +++---- 16 files changed, 45 insertions(+), 55 deletions(-) diff --git a/data/labyOutput.txt b/data/labyOutput.txt index 1436118..6e7752a 100644 --- a/data/labyOutput.txt +++ b/data/labyOutput.txt @@ -1,8 +1,8 @@ -WEWWEEEE -EDEWEWWE -EWEEEWWE -EWWEWWEE -EWWEWWEW -EEWEWAEW -WEEEWWWE -WWEWWWWE +WEEEEEWW +EDWWWEEW +WEEWWWEE +WWEEEEEW +EEEWWWWW +EWWWWAWW +EWWEEEWW +EEEEWWWW \ No newline at end of file diff --git a/src/MainTest.java b/src/MainTest.java index b7cd87e..a05dadc 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -7,19 +7,19 @@ import ui.*; public class MainTest { public static void main(String[] args) { - + Maze mazeTest = new Maze("data/labyrinthe.txt") ; - + Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]); - + ArrayList shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]); - + for (VertexInterface vertex : shortest) { System.out.println(vertex); } - + mazeTest.saveToTextFile("data/labyOutput.txt"); - + new MazeApp(); } diff --git a/src/model/Maze.java b/src/model/Maze.java index 3bd6162..adbc938 100644 --- a/src/model/Maze.java +++ b/src/model/Maze.java @@ -5,5 +5,4 @@ public class Maze extends maze.Maze { public Maze(String filename) { super(filename); } - } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index f331efe..2639ee1 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -25,4 +25,4 @@ public class MazeAppModel extends Observable { public void saveToFile() { maze.saveToTextFile("data/labyOutput.txt"); } -} \ No newline at end of file +} diff --git a/src/ui/ArrivalDrawingBox.java b/src/ui/ArrivalDrawingBox.java index 6677e9a..f221d34 100644 --- a/src/ui/ArrivalDrawingBox.java +++ b/src/ui/ArrivalDrawingBox.java @@ -7,5 +7,4 @@ public class ArrivalDrawingBox extends DrawingBox { public ArrivalDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - } diff --git a/src/ui/DepartureDrawingBox.java b/src/ui/DepartureDrawingBox.java index 3c21ec1..bd4ca1b 100644 --- a/src/ui/DepartureDrawingBox.java +++ b/src/ui/DepartureDrawingBox.java @@ -7,5 +7,4 @@ public class DepartureDrawingBox extends DrawingBox { public DepartureDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - } diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java index 2397c7e..d2f24ed 100644 --- a/src/ui/DrawingBox.java +++ b/src/ui/DrawingBox.java @@ -8,12 +8,12 @@ public abstract class DrawingBox extends JPanel { private final MazeApp mazeApp; private Color color; - + public DrawingBox(MazeApp mazeApp, Color color) { - + this.mazeApp = mazeApp; this.color = color; - + setBackground(color); setPreferredSize(new Dimension(40,40)); } diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index 4671537..6d220d4 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -12,25 +12,25 @@ public class DrawingPanelGrid extends JPanel { private Maze maze; private int dimensionX; private int dimensionY; - + public DrawingPanelGrid(MazeApp mazeApp) { - + this.mazeApp = mazeApp; this.maze = mazeApp.getMazeAppModel().getMaze(); this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); - + setLayout(new GridLayout(this.dimensionX, this.dimensionY)); initFromMaze(maze); - + setPreferredSize(new Dimension(256, 256)); } - + private void initFromMaze(Maze maze) { int dimensionX = maze.getDimensionX(); int dimensionY = maze.getDimensionY(); - + for (int i = 0; i < dimensionX; i++) { for (int j = 0; j < dimensionY; j++) { if (maze.getMaze()[j][i].isWalkable()) { @@ -49,6 +49,5 @@ public class DrawingPanelGrid extends JPanel { } } } - } } diff --git a/src/ui/EmptyDrawingBox.java b/src/ui/EmptyDrawingBox.java index 1b649d3..7fe2c69 100644 --- a/src/ui/EmptyDrawingBox.java +++ b/src/ui/EmptyDrawingBox.java @@ -7,5 +7,4 @@ public class EmptyDrawingBox extends DrawingBox { public EmptyDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - } diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index c2438d0..2cfac46 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -7,15 +7,13 @@ public class FileMenu extends JMenu { private final MazeApp mazeApp; private final QuitMenuItem quitMenuItem; private final LoadMazeMenuItem loadMazeMenuItem; - + public FileMenu(MazeApp mazeApp) { - + super("File"); this.mazeApp = mazeApp; - + add(quitMenuItem = new QuitMenuItem(mazeApp)); add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp)); - } - } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 06d0505..9724fde 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -15,14 +15,14 @@ public class LoadMazeMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; - + public LoadMazeMenuItem(MazeApp mazeApp) { super("Load maze"); this.mazeApp = mazeApp; addActionListener(this); } - + public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 3dbb3e6..0b7f1c1 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -5,24 +5,24 @@ import maze.Maze; import model.MazeAppModel; public class MazeApp extends JFrame { - + private final MazeMenuBar mazeMenuBar; private final WindowPanel windowPanel; private MazeAppModel mazeAppModel = new MazeAppModel(); public MazeApp() { - + super("Maze Solving Application"); - + setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); setContentPane(windowPanel = new WindowPanel(this)) ; - + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - + pack(); setVisible(true); } - + public MazeAppModel getMazeAppModel() { return mazeAppModel; } diff --git a/src/ui/MazeMenuBar.java b/src/ui/MazeMenuBar.java index f8d47da..60fba7a 100644 --- a/src/ui/MazeMenuBar.java +++ b/src/ui/MazeMenuBar.java @@ -6,13 +6,12 @@ public class MazeMenuBar extends JMenuBar { private final MazeApp mazeApp; private final FileMenu fileMenu; - + public MazeMenuBar(MazeApp mazeApp) { - + super(); this.mazeApp = mazeApp; - + add(fileMenu = new FileMenu(mazeApp)); - } } diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 806379b..46908e5 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -12,18 +12,18 @@ public class QuitMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; - + public QuitMenuItem(MazeApp mazeApp) { super("Quit"); this.mazeApp = mazeApp; addActionListener(this); } - + public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - + if (mazeAppModel.isModified()) { int response = JOptionPane.showInternalOptionDialog(this, "Maze not saved. Save it ?", @@ -41,6 +41,6 @@ public class QuitMenuItem extends JMenuItem break; } } - System.exit(0) ; + System.exit(0); } } diff --git a/src/ui/WallDrawingBox.java b/src/ui/WallDrawingBox.java index 207bc7a..9a6483c 100644 --- a/src/ui/WallDrawingBox.java +++ b/src/ui/WallDrawingBox.java @@ -7,5 +7,4 @@ public class WallDrawingBox extends DrawingBox { public WallDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - } diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 55ec5c0..774a57a 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -7,12 +7,11 @@ import java.awt.*; public class WindowPanel extends JPanel { private final DrawingPanelGrid drawingPanelGrid; - + public WindowPanel(MazeApp mazeApp) { - + setLayout(new BorderLayout()); - + add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); } - } -- 2.56.1 From 7d5b2938fc97980f22dee8f112571265333fd3c4 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 17 Dec 2017 20:27:55 +0100 Subject: [PATCH 10/37] Add loading options in JMenuBar Need to fix bug when loading new maze with different dimensions --- data/labyOutput.txt | 2 +- data/labyrinthe.txt | 1 + src/MainTest.java | 6 ++--- src/model/MazeAppModel.java | 19 ++++++++------- src/model/Previous.java | 13 ---------- src/ui/ArrivalDrawingBox.java | 7 ------ src/ui/DepartureDrawingBox.java | 7 ------ src/ui/DrawingBox.java | 17 +++++++------ src/ui/DrawingPanelGrid.java | 42 ++++++++++++++++----------------- src/ui/EmptyDrawingBox.java | 7 ------ src/ui/MazeApp.java | 6 ++++- src/ui/WallDrawingBox.java | 7 ------ src/ui/WindowPanel.java | 1 - 13 files changed, 51 insertions(+), 84 deletions(-) delete mode 100644 src/model/Previous.java diff --git a/data/labyOutput.txt b/data/labyOutput.txt index 6e7752a..c27fe74 100644 --- a/data/labyOutput.txt +++ b/data/labyOutput.txt @@ -5,4 +5,4 @@ WWEEEEEW EEEWWWWW EWWWWAWW EWWEEEWW -EEEEWWWW \ No newline at end of file +EEEEWWWW diff --git a/data/labyrinthe.txt b/data/labyrinthe.txt index 6e7752a..e4bd637 100644 --- a/data/labyrinthe.txt +++ b/data/labyrinthe.txt @@ -5,4 +5,5 @@ WWEEEEEW EEEWWWWW EWWWWAWW EWWEEEWW +EEEEWWWW EEEEWWWW \ No newline at end of file diff --git a/src/MainTest.java b/src/MainTest.java index a05dadc..61a3012 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,5 +1,5 @@ import java.util.ArrayList; - +import model.MazeAppModel; import dijkstra.*; import maze.*; import ui.*; @@ -18,9 +18,7 @@ public class MainTest { System.out.println(vertex); } - mazeTest.saveToTextFile("data/labyOutput.txt"); - - new MazeApp(); + MazeApp app = new MazeApp(); } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 695b02f..3ac826e 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -2,25 +2,28 @@ package model; import java.util.ArrayList; import java.util.Observable; +import java.util.Observer; import dijkstra.VertexInterface; import dijkstra.Dijkstra; +import dijkstra.Previous; public class MazeAppModel extends Observable { - private Maze maze = new Maze("data/labyrinthe.txt"); - private VertexInterface departure = maze.getDeparture(); - private VertexInterface arrival = maze.getArrival(); - private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure); - private ArrayList shortest = previous.getShortestPathTo(arrival); - private ArrayList boxList = maze.getVertexes(); - private Boolean modified = false; + private Maze maze = new Maze("data/labyrinthe.txt"); + private VertexInterface departure = maze.getDeparture(); + private VertexInterface arrival = maze.getArrival(); + private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure); + private ArrayList shortest = previous.getShortestPathTo(arrival); + private ArrayList boxList = maze.getVertexes(); + private Boolean modified = false; + private ArrayList listObserver = new ArrayList(); public void setMaze(Maze maze) { this.maze = maze; modified = true; setChanged(); - notifyObservers(); + notifyObservers(maze); } public Maze getMaze() { diff --git a/src/model/Previous.java b/src/model/Previous.java deleted file mode 100644 index cd78bf4..0000000 --- a/src/model/Previous.java +++ /dev/null @@ -1,13 +0,0 @@ -package model; - -import java.util.ArrayList; -import java.util.Hashtable; - -import dijkstra.VertexInterface; - -public class Previous extends dijkstra.Previous { - - public Previous(Hashtable hashPrevious, ArrayList shortestPath) { - super(hashPrevious, shortestPath); - } -} diff --git a/src/ui/ArrivalDrawingBox.java b/src/ui/ArrivalDrawingBox.java index e4df895..829725d 100644 --- a/src/ui/ArrivalDrawingBox.java +++ b/src/ui/ArrivalDrawingBox.java @@ -8,11 +8,4 @@ public class ArrivalDrawingBox extends DrawingBox { public ArrivalDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - - @Override - protected final void paintComponent(Graphics g) { - - super.paintComponent(g); - setBackground(Color.RED); - } } diff --git a/src/ui/DepartureDrawingBox.java b/src/ui/DepartureDrawingBox.java index 87278ac..b841e6c 100644 --- a/src/ui/DepartureDrawingBox.java +++ b/src/ui/DepartureDrawingBox.java @@ -8,11 +8,4 @@ public class DepartureDrawingBox extends DrawingBox { public DepartureDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - - @Override - protected final void paintComponent(Graphics g) { - - super.paintComponent(g); - setBackground(Color.GREEN); - } } diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java index cbcd771..65c4eb2 100644 --- a/src/ui/DrawingBox.java +++ b/src/ui/DrawingBox.java @@ -16,21 +16,24 @@ public abstract class DrawingBox extends JPanel { this.mazeApp = mazeApp; this.color = color; - - setBackground(color); - setPreferredSize(new Dimension(40,40)); - setBorder(BorderFactory.createLineBorder(Color.orange)); + + setOpaque(true); + setPreferredSize(new Dimension(50,50)); + setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); } @Override - protected void paintComponent(Graphics g) { - + public void paintComponent(Graphics g) { super.paintComponent(g); + setBackground(color); } public void notifyForUpdates(Object param) { + this.repaint(); + } - repaint(); + public void setColor(Color color) { + this.color = color; } } diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index a2a2e6e..f5e70f6 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -22,57 +22,57 @@ public class DrawingPanelGrid extends JPanel { this.dimensionY = maze.getDimensionY(); this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY]; - setLayout(new GridLayout(this.dimensionX, this.dimensionY)); + setLayout(new GridLayout(this.dimensionY, this.dimensionX)); //inverted coordinates initFromMaze(maze); - - setPreferredSize(new Dimension(256, 256)); } private void initFromMaze(Maze maze) { - for (int i = 0; i < dimensionX; i++) { - for (int j = 0; j < dimensionY; j++) { + for (int i = 0; i < dimensionY; i++) { + for (int j = 0; j < dimensionX; j++) { if (maze.getMaze()[j][i].isWalkable()) { if (maze.getMaze()[j][i].isDeparture()) { - add(new DepartureDrawingBox(mazeApp, Color.GREEN)); drawingBoxMatrix[j][i] = new DepartureDrawingBox(mazeApp, Color.GREEN); } else if (maze.getMaze()[j][i].isArrival()) { - add(new ArrivalDrawingBox(mazeApp, Color.RED)); drawingBoxMatrix[j][i] = new ArrivalDrawingBox(mazeApp, Color.RED); } else { - add(new EmptyDrawingBox(mazeApp, Color.BLACK)); drawingBoxMatrix[j][i] = new EmptyDrawingBox(mazeApp, Color.BLACK); } } else { - add(new WallDrawingBox(mazeApp, Color.WHITE)); drawingBoxMatrix[j][i] = new WallDrawingBox(mazeApp, Color.WHITE); } + + add(drawingBoxMatrix[j][i]); } } } public void notifyForUpdates(Object param) { + if (param instanceof Maze) { + maze = (Maze) param; - for (int i = 0; i < dimensionX; i++) { - for (int j = 0; j < dimensionY; j++) { - if (maze.getMaze()[j][i].isWalkable()) { - if (maze.getMaze()[j][i].isDeparture()) { - drawingBoxMatrix[j][i].notifyForUpdates(); - } - else if (maze.getMaze()[j][i].isArrival()) { - drawingBoxMatrix[j][i].notifyForUpdates(); + for (int j = 0; j < dimensionX; j++) { + for (int i = 0; i < dimensionY; i++) { + if (maze.getMaze()[j][i].isWalkable()) { + if (maze.getMaze()[j][i].isDeparture()) { + drawingBoxMatrix[j][i].setColor(Color.GREEN); + } + else if (maze.getMaze()[j][i].isArrival()) { + drawingBoxMatrix[j][i].setColor(Color.RED); + } + else { + drawingBoxMatrix[j][i].setColor(Color.BLACK); + } } else { - drawingBoxMatrix[j][i].notifyForUpdates(); + drawingBoxMatrix[j][i].setColor(Color.WHITE); } } - else { - drawingBoxMatrix[j][i].notifyForUpdates(); - } } + repaint(); } } } diff --git a/src/ui/EmptyDrawingBox.java b/src/ui/EmptyDrawingBox.java index bbb7d62..b4cbe67 100644 --- a/src/ui/EmptyDrawingBox.java +++ b/src/ui/EmptyDrawingBox.java @@ -8,11 +8,4 @@ public class EmptyDrawingBox extends DrawingBox { public EmptyDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - - @Override - protected final void paintComponent(Graphics g) { - - super.paintComponent(g); - setBackground(Color.WHITE); - } } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index b9df188..2e7b37d 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -12,12 +12,15 @@ public class MazeApp extends JFrame private final MazeMenuBar mazeMenuBar; private final WindowPanel windowPanel; - private MazeAppModel mazeAppModel = new MazeAppModel(); + private MazeAppModel mazeAppModel; public MazeApp() { super("Maze Solving Application"); + this.mazeAppModel = new MazeAppModel(); + mazeAppModel.addObserver(this); + setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); setContentPane(windowPanel = new WindowPanel(this)); @@ -34,6 +37,7 @@ public class MazeApp extends JFrame @Override public void update(Observable obs, Object param) { + System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } } diff --git a/src/ui/WallDrawingBox.java b/src/ui/WallDrawingBox.java index 59ef424..8d986c7 100644 --- a/src/ui/WallDrawingBox.java +++ b/src/ui/WallDrawingBox.java @@ -8,11 +8,4 @@ public class WallDrawingBox extends DrawingBox { public WallDrawingBox(MazeApp mazeApp, Color color) { super(mazeApp, color); } - - @Override - protected final void paintComponent(Graphics g) { - - super.paintComponent(g); - setBackground(Color.BLACK); - } } diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index ab6875d..55d17fc 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -16,7 +16,6 @@ public class WindowPanel extends JPanel } public void notifyForUpdates(Object param) { - drawingPanelGrid.notifyForUpdates(param); } -- 2.56.1 From 9aa134eb7728d468650c475bea5d5d0e0b358cf2 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 17 Dec 2017 20:30:35 +0100 Subject: [PATCH 11/37] Accidentally forgot to save file --- src/ui/DrawingPanelGrid.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index f5e70f6..f37d0ea 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -51,6 +51,7 @@ public class DrawingPanelGrid extends JPanel { } public void notifyForUpdates(Object param) { + if (param instanceof Maze) { maze = (Maze) param; -- 2.56.1 From d9b52c35e2c015996bc5ecc315085d0984f23acd Mon Sep 17 00:00:00 2001 From: Antonin GODARD Date: Mon, 18 Dec 2017 11:47:55 +0100 Subject: [PATCH 12/37] [WIP] Load maze --- src/MainTest.java | 1 - src/maze/Maze.java | 30 +++++++++++++++++++++++++----- src/model/Maze.java | 4 ++++ src/model/MazeAppModel.java | 3 +-- src/ui/DrawingPanelGrid.java | 31 +++++++++++++++++++------------ src/ui/LoadMazeMenuItem.java | 19 +++++++++++-------- 6 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/MainTest.java b/src/MainTest.java index 61a3012..26010af 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,5 +1,4 @@ import java.util.ArrayList; -import model.MazeAppModel; import dijkstra.*; import maze.*; import ui.*; diff --git a/src/maze/Maze.java b/src/maze/Maze.java index e08996e..b81d89c 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -29,11 +29,31 @@ public class Maze } - // public Maze(Box[][] maze){ - // //Replacement for setMaze() - // //Todo : check if the given array is a proper maze - // //and then set dimensionX and dimensionX - // } + public Maze(Maze maze){ + + this.dimensionX = maze.getDimensionX(); + this.dimensionY = maze.getDimensionY(); + this.maze = new Box[dimensionX][dimensionY]; + for (int line = 0; line < dimensionX; line++) { + for (int row = 0; row < dimensionY; row++) { + Box box = maze.getMaze()[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + this.maze[line][row] = new DBox(box.getLabel(), box.getX(), box.getY(), this); + } + else if (box.isArrival()) { + this.maze[line][row] = new ABox(box.getLabel(), box.getX(), box.getY(), this); + } + else { + this.maze[line][row] = new EBox(box.getLabel(), box.getX(), box.getY(), this); + } + } + else { + this.maze[line][row] = new WBox(box.getLabel(), box.getX(), box.getY(), this); + } + } + } + } public int getCost(VertexInterface start, VertexInterface end) { diff --git a/src/model/Maze.java b/src/model/Maze.java index adbc938..9afdc55 100644 --- a/src/model/Maze.java +++ b/src/model/Maze.java @@ -5,4 +5,8 @@ public class Maze extends maze.Maze { public Maze(String filename) { super(filename); } + + public Maze(Maze maze) { + super(maze); + } } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 3ac826e..a347261 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -17,10 +17,9 @@ public class MazeAppModel extends Observable { private ArrayList shortest = previous.getShortestPathTo(arrival); private ArrayList boxList = maze.getVertexes(); private Boolean modified = false; - private ArrayList listObserver = new ArrayList(); public void setMaze(Maze maze) { - this.maze = maze; + this.maze = new Maze(maze); modified = true; setChanged(); notifyObservers(maze); diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index f37d0ea..0bb3bdb 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -13,6 +13,7 @@ public class DrawingPanelGrid extends JPanel { private DrawingBox[][] drawingBoxMatrix; private int dimensionX; private int dimensionY; + private GridLayout gridLayout; public DrawingPanelGrid(MazeApp mazeApp) { @@ -20,9 +21,10 @@ public class DrawingPanelGrid extends JPanel { this.maze = mazeApp.getMazeAppModel().getMaze(); this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); - this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY]; + this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX]; + this.gridLayout = new GridLayout(this.dimensionY, this.dimensionX); //inverted coordinates - setLayout(new GridLayout(this.dimensionY, this.dimensionX)); //inverted coordinates + setLayout(gridLayout); initFromMaze(maze); } @@ -53,23 +55,28 @@ public class DrawingPanelGrid extends JPanel { public void notifyForUpdates(Object param) { if (param instanceof Maze) { - maze = (Maze) param; + this.maze = (Maze) param; + this.dimensionX = maze.getDimensionX(); + this.dimensionY = maze.getDimensionY(); + this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX]; + System.out.println("x : "+dimensionX+" y : "+dimensionY); + this.gridLayout = new GridLayout(dimensionY, dimensionX); - for (int j = 0; j < dimensionX; j++) { - for (int i = 0; i < dimensionY; i++) { - if (maze.getMaze()[j][i].isWalkable()) { - if (maze.getMaze()[j][i].isDeparture()) { - drawingBoxMatrix[j][i].setColor(Color.GREEN); + for (int line = 0; line < dimensionX; line++) { + for (int row = 0; row < dimensionY; row++) { + if (maze.getMaze()[line][row].isWalkable()) { + if (maze.getMaze()[line][row].isDeparture()) { + drawingBoxMatrix[row][line].setColor(Color.GREEN); } - else if (maze.getMaze()[j][i].isArrival()) { - drawingBoxMatrix[j][i].setColor(Color.RED); + else if (maze.getMaze()[line][row].isArrival()) { + drawingBoxMatrix[row][line].setColor(Color.RED); } else { - drawingBoxMatrix[j][i].setColor(Color.BLACK); + drawingBoxMatrix[row][line].setColor(Color.BLACK); } } else { - drawingBoxMatrix[j][i].setColor(Color.WHITE); + drawingBoxMatrix[row][line].setColor(Color.WHITE); } } } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 9724fde..71bfc89 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -27,15 +27,18 @@ public class LoadMazeMenuItem extends JMenuItem MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); JFileChooser fileChooser = new JFileChooser(); - FileNameExtensionFilter filter = new FileNameExtensionFilter( - "Text files", "txt"); + + FileNameExtensionFilter filter = + new FileNameExtensionFilter("Text files", "txt"); fileChooser.setFileFilter(filter); + int returnVal = fileChooser.showOpenDialog(mazeApp); - if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to open this file: " + - fileChooser.getSelectedFile().getName()); - } - String filename = fileChooser.getSelectedFile().getPath(); - mazeApp.getMazeAppModel().setMaze(new Maze(filename)); + if(returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to open this file: " + + fileChooser.getSelectedFile().getName()); + } + + String filename = fileChooser.getSelectedFile().getPath(); + mazeApp.getMazeAppModel().setMaze(new Maze(filename)); } } -- 2.56.1 From 213071d3c218056bbc8e5c1572208f1a47f64064 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Mon, 18 Dec 2017 16:32:47 +0100 Subject: [PATCH 13/37] Fix indexes confusion --- src/MainTest.java | 10 ++++-- src/maze/Box.java | 2 +- src/maze/Maze.java | 70 +++++++++++++++++++++--------------- src/ui/DrawingPanelGrid.java | 35 +++++++++--------- 4 files changed, 69 insertions(+), 48 deletions(-) diff --git a/src/MainTest.java b/src/MainTest.java index 26010af..66cdb93 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -8,11 +8,17 @@ 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, mazeTest.getMaze()[1][1]); + Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, departure); - ArrayList shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]); + ArrayList shortest = previous.getShortestPathTo(arrival); + //Print shortest path to arrival + System.out.println(arrival); for (VertexInterface vertex : shortest) { System.out.println(vertex); } diff --git a/src/maze/Box.java b/src/maze/Box.java index 00f7694..3a665e7 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -24,7 +24,7 @@ public abstract class Box private Maze maze ; private ArrayList neighbourList = new ArrayList() ; - public Box (String label, int x, int y, Maze maze) { + public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation this.label = label ; this.x = x ; diff --git a/src/maze/Maze.java b/src/maze/Maze.java index b81d89c..1d9d3c3 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -33,9 +33,10 @@ public class Maze this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); - this.maze = new Box[dimensionX][dimensionY]; - for (int line = 0; line < dimensionX; line++) { - for (int row = 0; row < dimensionY; row++) { + this.maze = new Box[dimensionY][dimensionX]; + + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { Box box = maze.getMaze()[line][row]; if (box.isWalkable()) { if (box.isDeparture()) { @@ -69,10 +70,10 @@ public class Maze public ArrayList getVertexes() { ArrayList vertexList = new ArrayList() ; - for (int i = 0 ; i < dimensionX ; i++) { - for (int j = 0 ; j < dimensionY ; j++) { - if (maze[i][j].isWalkable()) { - vertexList.add(maze[i][j]) ; + for (int line = 0 ; line < dimensionY; line++) { + for (int row = 0 ; row < dimensionX; row++) { + if (maze[line][row].isWalkable()) { + vertexList.add(maze[line][row]) ; } } } @@ -110,6 +111,7 @@ public class Maze public ArrayList generateNeighbours(VertexInterface vertex) { Box box = (Box)vertex ; + //System.out.println(vertex); int x = box.getX() ; int y = box.getY() ; ArrayList neighbourList = new ArrayList() ; @@ -119,22 +121,23 @@ public class Maze return neighbourList ; } - if (x > 0 && maze[x-1][y].isWalkable()) { - neighbourList.add(maze[x-1][y]) ; + if (x > 0 && maze[y][x-1].isWalkable()) { + neighbourList.add(maze[y][x-1]) ; } - if (x < dimensionX-1 && maze[x+1][y].isWalkable()) { - neighbourList.add(maze[x+1][y]) ; + if (x < dimensionX-1 && maze[y][x+1].isWalkable()) { + neighbourList.add(maze[y][x+1]) ; } - if (y > 0 && maze[x][y-1].isWalkable()) { - neighbourList.add(maze[x][y-1]) ; + if (y > 0 && maze[y-1][x].isWalkable()) { + neighbourList.add(maze[y-1][x]) ; } - if (y < dimensionY-1 && maze[x][y+1].isWalkable()) { - neighbourList.add(maze[x][y+1]) ; + if (y < dimensionY-1 && maze[y+1][x].isWalkable()) { + neighbourList.add(maze[y+1][x]) ; } + //System.out.println(" "+ neighbourList); return neighbourList ; } @@ -148,7 +151,7 @@ public class Maze FileReader fr = null ; BufferedReader br = null ; - + try { fr = new FileReader(filename) ; br = new BufferedReader(fr) ; @@ -168,10 +171,10 @@ public class Maze currentLine = br.readLine() ; dimensionX = currentLine.length() ; //maze's width int currentLineLength ; + + this.maze = new Box[dimensionY][dimensionX] ; + int compteurLigne = 0 ; - - this.maze = new Box[dimensionX][dimensionY] ; - while (currentLine != null) { currentLineLength = currentLine.length() ; @@ -182,16 +185,16 @@ public class Maze switch(currentChar) { - case 'W' : maze[compteurColonne][compteurLigne] = new WBox("W", compteurColonne, compteurLigne, this) ; + case 'W' : maze[compteurLigne][compteurColonne] = new WBox("W", compteurLigne, compteurColonne, this) ; break; - case 'E' : maze[compteurColonne][compteurLigne] = new EBox("E", compteurColonne, compteurLigne, this) ; + case 'E' : maze[compteurLigne][compteurColonne] = new EBox("E", compteurLigne, compteurColonne, this) ; break; - case 'D' : maze[compteurColonne][compteurLigne] = new DBox("D", compteurColonne, compteurLigne, this) ; + case 'D' : maze[compteurLigne][compteurColonne] = new DBox("D", compteurLigne, compteurColonne, this) ; break; - case 'A' : maze[compteurColonne][compteurLigne] = new ABox("A", compteurColonne, compteurLigne, this) ; + case 'A' : maze[compteurLigne][compteurColonne] = new ABox("A", compteurLigne, compteurColonne, this) ; break; default : throw new MazeReadingException(filename) ; @@ -213,7 +216,6 @@ public class Maze } finally { try { br.close() ;} catch (Exception e) {} } - } public final void saveToTextFile(String fileName) { @@ -226,10 +228,10 @@ public class Maze fos = new FileOutputStream(fileName) ; pw = new PrintWriter(fos) ; - for (int i = 0; i < dimensionX ; i++) { - for (int j = 0 ; j < dimensionY ; j++) { + for (int line = 0; line < dimensionY ; line++) { + for (int row = 0 ; row < dimensionX ; row++) { - pw.print(maze[i][j].getLabel()); + pw.print(maze[line][row].getLabel()); } pw.print("\n"); @@ -241,7 +243,19 @@ public class Maze try { pw.close();} catch(Exception e) {} } } - + + public String toString() { + + String mazeStr = ""; + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + mazeStr += maze[line][row].getLabel(); + } + mazeStr += "\n"; + } + return mazeStr; + } + public int getDimensionX() { return dimensionX; } diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index 0bb3bdb..6fd3a5a 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -30,24 +30,24 @@ public class DrawingPanelGrid extends JPanel { private void initFromMaze(Maze maze) { - for (int i = 0; i < dimensionY; i++) { - for (int j = 0; j < dimensionX; j++) { - if (maze.getMaze()[j][i].isWalkable()) { - if (maze.getMaze()[j][i].isDeparture()) { - drawingBoxMatrix[j][i] = new DepartureDrawingBox(mazeApp, Color.GREEN); + for (int line = 0 ; line < dimensionY; line++) { + for (int row = 0 ; row < dimensionX; row++) { + if (maze.getMaze()[line][row].isWalkable()) { + if (maze.getMaze()[line][row].isDeparture()) { + drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN); } - else if (maze.getMaze()[j][i].isArrival()) { - drawingBoxMatrix[j][i] = new ArrivalDrawingBox(mazeApp, Color.RED); + else if (maze.getMaze()[line][row].isArrival()) { + drawingBoxMatrix[line][row] = new ArrivalDrawingBox(mazeApp, Color.RED); } else { - drawingBoxMatrix[j][i] = new EmptyDrawingBox(mazeApp, Color.BLACK); + drawingBoxMatrix[line][row] = new EmptyDrawingBox(mazeApp, Color.BLACK); } } else { - drawingBoxMatrix[j][i] = new WallDrawingBox(mazeApp, Color.WHITE); + drawingBoxMatrix[line][row] = new WallDrawingBox(mazeApp, Color.WHITE); } - add(drawingBoxMatrix[j][i]); + add(drawingBoxMatrix[line][row]); } } } @@ -55,28 +55,29 @@ public class DrawingPanelGrid extends JPanel { public void notifyForUpdates(Object param) { if (param instanceof Maze) { + // set all new parameters this.maze = (Maze) param; this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX]; - System.out.println("x : "+dimensionX+" y : "+dimensionY); this.gridLayout = new GridLayout(dimensionY, dimensionX); + setLayout(this.gridLayout); - for (int line = 0; line < dimensionX; line++) { - for (int row = 0; row < dimensionY; row++) { + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { if (maze.getMaze()[line][row].isWalkable()) { if (maze.getMaze()[line][row].isDeparture()) { - drawingBoxMatrix[row][line].setColor(Color.GREEN); + drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN); } else if (maze.getMaze()[line][row].isArrival()) { - drawingBoxMatrix[row][line].setColor(Color.RED); + drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.RED); } else { - drawingBoxMatrix[row][line].setColor(Color.BLACK); + drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.BLACK); } } else { - drawingBoxMatrix[row][line].setColor(Color.WHITE); + drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN); } } } -- 2.56.1 From 53ee6f1565756c7d94e19dfead04f86519423416 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Tue, 19 Dec 2017 23:30:12 +0100 Subject: [PATCH 14/37] Add a button to save the maze from the ui --- src/model/MazeAppModel.java | 4 ++++ src/ui/FileMenu.java | 2 ++ src/ui/SaveMazeMenuItem.java | 42 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/ui/SaveMazeMenuItem.java diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index a347261..8f7180f 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -36,4 +36,8 @@ public class MazeAppModel extends Observable { public void saveToFile() { maze.saveToTextFile("data/labyOutput.txt"); } + + public void saveToFile(String filename) { + maze.saveToTextFile(filename); + } } diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index 2cfac46..67447c3 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -7,6 +7,7 @@ public class FileMenu extends JMenu { private final MazeApp mazeApp; private final QuitMenuItem quitMenuItem; private final LoadMazeMenuItem loadMazeMenuItem; + private final SaveMazeMenuItem saveMazeMenuItem; public FileMenu(MazeApp mazeApp) { @@ -15,5 +16,6 @@ public class FileMenu extends JMenu { add(quitMenuItem = new QuitMenuItem(mazeApp)); add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp)); + add(saveMazeMenuItem = new SaveMazeMenuItem(mazeApp)); } } diff --git a/src/ui/SaveMazeMenuItem.java b/src/ui/SaveMazeMenuItem.java new file mode 100644 index 0000000..61cd3e0 --- /dev/null +++ b/src/ui/SaveMazeMenuItem.java @@ -0,0 +1,42 @@ +package ui; + +import javax.swing.JFileChooser; +import javax.swing.JMenuItem; +import javax.swing.filechooser.FileNameExtensionFilter; + +import model.MazeAppModel; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class SaveMazeMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public SaveMazeMenuItem(MazeApp mazeApp) { + + super("Save maze"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + JFileChooser fileChooser = new JFileChooser(); + + FileNameExtensionFilter filter = + new FileNameExtensionFilter("Text files", "txt"); + fileChooser.setFileFilter(filter); + + int returnVal = fileChooser.showOpenDialog(mazeApp); + if(returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to save to this file: " + + fileChooser.getSelectedFile().getName()); + } + + String filename = fileChooser.getSelectedFile().getPath(); + mazeApp.getMazeAppModel().saveToFile(filename); + } +} -- 2.56.1 From 987fb858e4d3222ee064a1cc59e48049ec2d221a Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Wed, 20 Dec 2017 01:18:37 +0100 Subject: [PATCH 15/37] Use proper dialog type for the "Save Maze" button Instead of using showOpenDialog(), use showSaveDialog() --- src/ui/SaveMazeMenuItem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/SaveMazeMenuItem.java b/src/ui/SaveMazeMenuItem.java index 61cd3e0..d3b6e4c 100644 --- a/src/ui/SaveMazeMenuItem.java +++ b/src/ui/SaveMazeMenuItem.java @@ -30,7 +30,7 @@ public class SaveMazeMenuItem extends JMenuItem new FileNameExtensionFilter("Text files", "txt"); fileChooser.setFileFilter(filter); - int returnVal = fileChooser.showOpenDialog(mazeApp); + int returnVal = fileChooser.showSaveDialog(mazeApp); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to save to this file: " + fileChooser.getSelectedFile().getName()); -- 2.56.1 From 3cdf4254355d1a1c7aaa6af81bc956062deec352 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Wed, 20 Dec 2017 21:05:56 +0100 Subject: [PATCH 16/37] Add load maze --- src/maze/Maze.java | 8 +-- src/model/MazeAppModel.java | 33 +++++++++++++ src/ui/ArrivalDrawingBox.java | 11 ----- src/ui/DepartureDrawingBox.java | 11 ----- src/ui/DrawingBox.java | 39 --------------- src/ui/DrawingPanelGrid.java | 87 --------------------------------- src/ui/EmptyDrawingBox.java | 11 ----- src/ui/MazeDrawing.java | 42 ++++++++++++++++ src/ui/WallDrawingBox.java | 11 ----- src/ui/WindowPanel.java | 4 +- 10 files changed, 81 insertions(+), 176 deletions(-) delete mode 100644 src/ui/ArrivalDrawingBox.java delete mode 100644 src/ui/DepartureDrawingBox.java delete mode 100644 src/ui/DrawingBox.java delete mode 100644 src/ui/DrawingPanelGrid.java delete mode 100644 src/ui/EmptyDrawingBox.java create mode 100644 src/ui/MazeDrawing.java delete mode 100644 src/ui/WallDrawingBox.java diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 1d9d3c3..8e6edad 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -40,17 +40,17 @@ public class Maze Box box = maze.getMaze()[line][row]; if (box.isWalkable()) { if (box.isDeparture()) { - this.maze[line][row] = new DBox(box.getLabel(), box.getX(), box.getY(), this); + this.maze[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); } else if (box.isArrival()) { - this.maze[line][row] = new ABox(box.getLabel(), box.getX(), box.getY(), this); + this.maze[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); } else { - this.maze[line][row] = new EBox(box.getLabel(), box.getX(), box.getY(), this); + this.maze[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); } } else { - this.maze[line][row] = new WBox(box.getLabel(), box.getX(), box.getY(), this); + this.maze[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); } } } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 8f7180f..f46b461 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -1,5 +1,8 @@ package model; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; import java.util.ArrayList; import java.util.Observable; import java.util.Observer; @@ -40,4 +43,34 @@ public class MazeAppModel extends Observable { public void saveToFile(String filename) { maze.saveToTextFile(filename); } + + public void paintMaze(Graphics2D g, int width, int height) { + + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + int boxWidth = width/dimensionX; + int boxHeight = height/dimensionY; + + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + if (maze.getMaze()[line][row].isWalkable()) { + if (maze.getMaze()[line][row].isDeparture()) { + g.setColor(Color.GREEN); + } + else if (maze.getMaze()[line][row].isArrival()) { + g.setColor(Color.RED); + } + else { + g.setColor(Color.BLACK); + } + } + else { + g.setColor(Color.WHITE); + } + g.fill3DRect(row*boxWidth, line*boxHeight, + boxWidth, boxHeight, + true); + } + } + } } diff --git a/src/ui/ArrivalDrawingBox.java b/src/ui/ArrivalDrawingBox.java deleted file mode 100644 index 829725d..0000000 --- a/src/ui/ArrivalDrawingBox.java +++ /dev/null @@ -1,11 +0,0 @@ -package ui; - -import java.awt.Color; -import java.awt.Graphics; - -public class ArrivalDrawingBox extends DrawingBox { - - public ArrivalDrawingBox(MazeApp mazeApp, Color color) { - super(mazeApp, color); - } -} diff --git a/src/ui/DepartureDrawingBox.java b/src/ui/DepartureDrawingBox.java deleted file mode 100644 index b841e6c..0000000 --- a/src/ui/DepartureDrawingBox.java +++ /dev/null @@ -1,11 +0,0 @@ -package ui; - -import java.awt.Color; -import java.awt.Graphics; - -public class DepartureDrawingBox extends DrawingBox { - - public DepartureDrawingBox(MazeApp mazeApp, Color color) { - super(mazeApp, color); - } -} diff --git a/src/ui/DrawingBox.java b/src/ui/DrawingBox.java deleted file mode 100644 index 65c4eb2..0000000 --- a/src/ui/DrawingBox.java +++ /dev/null @@ -1,39 +0,0 @@ -package ui; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; - -import javax.swing.JPanel; -import javax.swing.BorderFactory; - -public abstract class DrawingBox extends JPanel { - - private final MazeApp mazeApp; - private Color color; - - public DrawingBox(MazeApp mazeApp, Color color) { - - this.mazeApp = mazeApp; - this.color = color; - - setOpaque(true); - setPreferredSize(new Dimension(50,50)); - setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); - } - - @Override - public void paintComponent(Graphics g) { - super.paintComponent(g); - - setBackground(color); - } - - public void notifyForUpdates(Object param) { - this.repaint(); - } - - public void setColor(Color color) { - this.color = color; - } -} diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java deleted file mode 100644 index 6fd3a5a..0000000 --- a/src/ui/DrawingPanelGrid.java +++ /dev/null @@ -1,87 +0,0 @@ -package ui; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.GridLayout; -import javax.swing.JPanel; -import maze.Maze; - -public class DrawingPanelGrid extends JPanel { - - private final MazeApp mazeApp; - private Maze maze; - private DrawingBox[][] drawingBoxMatrix; - private int dimensionX; - private int dimensionY; - private GridLayout gridLayout; - - public DrawingPanelGrid(MazeApp mazeApp) { - - this.mazeApp = mazeApp; - this.maze = mazeApp.getMazeAppModel().getMaze(); - this.dimensionX = maze.getDimensionX(); - this.dimensionY = maze.getDimensionY(); - this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX]; - this.gridLayout = new GridLayout(this.dimensionY, this.dimensionX); //inverted coordinates - - setLayout(gridLayout); - initFromMaze(maze); - } - - private void initFromMaze(Maze maze) { - - for (int line = 0 ; line < dimensionY; line++) { - for (int row = 0 ; row < dimensionX; row++) { - if (maze.getMaze()[line][row].isWalkable()) { - if (maze.getMaze()[line][row].isDeparture()) { - drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN); - } - else if (maze.getMaze()[line][row].isArrival()) { - drawingBoxMatrix[line][row] = new ArrivalDrawingBox(mazeApp, Color.RED); - } - else { - drawingBoxMatrix[line][row] = new EmptyDrawingBox(mazeApp, Color.BLACK); - } - } - else { - drawingBoxMatrix[line][row] = new WallDrawingBox(mazeApp, Color.WHITE); - } - - add(drawingBoxMatrix[line][row]); - } - } - } - - public void notifyForUpdates(Object param) { - - if (param instanceof Maze) { - // set all new parameters - this.maze = (Maze) param; - this.dimensionX = maze.getDimensionX(); - this.dimensionY = maze.getDimensionY(); - this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX]; - this.gridLayout = new GridLayout(dimensionY, dimensionX); - setLayout(this.gridLayout); - - for (int line = 0; line < dimensionY; line++) { - for (int row = 0; row < dimensionX; row++) { - if (maze.getMaze()[line][row].isWalkable()) { - if (maze.getMaze()[line][row].isDeparture()) { - drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN); - } - else if (maze.getMaze()[line][row].isArrival()) { - drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.RED); - } - else { - drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.BLACK); - } - } - else { - drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN); - } - } - } - repaint(); - } - } -} diff --git a/src/ui/EmptyDrawingBox.java b/src/ui/EmptyDrawingBox.java deleted file mode 100644 index b4cbe67..0000000 --- a/src/ui/EmptyDrawingBox.java +++ /dev/null @@ -1,11 +0,0 @@ -package ui; - -import java.awt.Color; -import java.awt.Graphics; - -public class EmptyDrawingBox extends DrawingBox { - - public EmptyDrawingBox(MazeApp mazeApp, Color color) { - super(mazeApp, color); - } -} diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java new file mode 100644 index 0000000..f43e4ea --- /dev/null +++ b/src/ui/MazeDrawing.java @@ -0,0 +1,42 @@ +package ui; + +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 maze.Maze; + +public class MazeDrawing extends JPanel { + + private final MazeApp mazeApp; + + public MazeDrawing(MazeApp mazeApp) { + + this.mazeApp = mazeApp; + setBackground(Color.BLACK); + setPreferredSize(new Dimension(500,500)) ; + + } + + @Override + protected final void paintComponent(Graphics g) { + + Graphics2D g2d = (Graphics2D) g; + super.paintComponent(g2d); + + int width = getWidth(); + int height = getHeight(); + + mazeApp.getMazeAppModel().paintMaze(g2d, width, height); + + } + + public void notifyForUpdates(Object param) { + + if (param instanceof Maze) { + this.repaint(); + } + } +} diff --git a/src/ui/WallDrawingBox.java b/src/ui/WallDrawingBox.java deleted file mode 100644 index 8d986c7..0000000 --- a/src/ui/WallDrawingBox.java +++ /dev/null @@ -1,11 +0,0 @@ -package ui; - -import java.awt.Color; -import java.awt.Graphics; - -public class WallDrawingBox extends DrawingBox { - - public WallDrawingBox(MazeApp mazeApp, Color color) { - super(mazeApp, color); - } -} diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 55d17fc..0599a33 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -6,13 +6,13 @@ import java.awt.*; public class WindowPanel extends JPanel { - private final DrawingPanelGrid drawingPanelGrid; + private final MazeDrawing drawingPanelGrid; public WindowPanel(MazeApp mazeApp) { setLayout(new BorderLayout()); - add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); + add(drawingPanelGrid = new MazeDrawing(mazeApp), BorderLayout.CENTER); } public void notifyForUpdates(Object param) { -- 2.56.1 From 16df6a26c665f41927af18d52197119ef91fc6ea Mon Sep 17 00:00:00 2001 From: Antonin GODARD Date: Thu, 21 Dec 2017 09:03:55 +0100 Subject: [PATCH 17/37] Fix exception when exiting showDialog --- src/ui/LoadMazeMenuItem.java | 6 +++--- src/ui/SaveMazeMenuItem.java | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 71bfc89..b0147f6 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -32,13 +32,13 @@ public class LoadMazeMenuItem extends JMenuItem new FileNameExtensionFilter("Text files", "txt"); fileChooser.setFileFilter(filter); + String filename; int returnVal = fileChooser.showOpenDialog(mazeApp); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + fileChooser.getSelectedFile().getName()); + filename = fileChooser.getSelectedFile().getPath(); + mazeApp.getMazeAppModel().setMaze(new Maze(filename)); } - - String filename = fileChooser.getSelectedFile().getPath(); - mazeApp.getMazeAppModel().setMaze(new Maze(filename)); } } diff --git a/src/ui/SaveMazeMenuItem.java b/src/ui/SaveMazeMenuItem.java index d3b6e4c..4beefbe 100644 --- a/src/ui/SaveMazeMenuItem.java +++ b/src/ui/SaveMazeMenuItem.java @@ -34,9 +34,8 @@ public class SaveMazeMenuItem extends JMenuItem if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to save to this file: " + fileChooser.getSelectedFile().getName()); + String filename = fileChooser.getSelectedFile().getPath(); + mazeApp.getMazeAppModel().saveToFile(filename); } - - String filename = fileChooser.getSelectedFile().getPath(); - mazeApp.getMazeAppModel().saveToFile(filename); } } -- 2.56.1 From 9f56d3082a23438b424f01f383031d7abb52fcf5 Mon Sep 17 00:00:00 2001 From: Antonin GODARD Date: Thu, 21 Dec 2017 09:38:31 +0100 Subject: [PATCH 18/37] Add paintShortestPath to MazeAppModel --- src/model/MazeAppModel.java | 20 +++++++++++++++++++- src/ui/MazeDrawing.java | 1 - 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index f46b461..815f3f6 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -8,6 +8,7 @@ import java.util.Observable; import java.util.Observer; import dijkstra.VertexInterface; +import maze.EBox; import dijkstra.Dijkstra; import dijkstra.Previous; @@ -18,11 +19,15 @@ public class MazeAppModel extends Observable { private VertexInterface arrival = maze.getArrival(); private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure); private ArrayList shortest = previous.getShortestPathTo(arrival); - private ArrayList boxList = maze.getVertexes(); + //private ArrayList boxList = maze.getVertexes(); private Boolean modified = false; public void setMaze(Maze maze) { this.maze = new Maze(maze); + this.departure = maze.getDeparture(); + this.arrival = maze.getArrival(); + this.previous = (Previous) Dijkstra.dijkstra(maze, departure); + this.shortest = previous.getShortestPathTo(arrival); modified = true; setChanged(); notifyObservers(maze); @@ -73,4 +78,17 @@ public class MazeAppModel extends Observable { } } } + + public void paintShortestPath(Graphics2D g, int width, int height) { + + g.setColor(Color.ORANGE); + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + int boxWidth = width/dimensionX; + int boxHeight = height/dimensionY; + for (VertexInterface vertex : shortest) { + EBox box = (EBox) vertex; + g.fillOval(box.getX()*boxHeight, box.getY()*boxWidth, boxWidth, boxHeight); + } + } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index f43e4ea..2a83fe1 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -30,7 +30,6 @@ public class MazeDrawing extends JPanel { int height = getHeight(); mazeApp.getMazeAppModel().paintMaze(g2d, width, height); - } public void notifyForUpdates(Object param) { -- 2.56.1 From 78dfe72e01273f140989f379f2a2988bbbcc685d Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Thu, 21 Dec 2017 10:13:26 +0100 Subject: [PATCH 19/37] Open app on empty maze and improve load/save model: * Add a filename attribute to MazeAppModel to keep track of the file currently being used by the app * Add saveToFile(filename) to save the maze into a specific file * Rewrite saveToFile() to use the stored filename attribute to know into which file is to be used to save the maze, instead of the hardcoded path * Remove unused models * MazeAppModel is now initialized with an empty maze, instead of a maze read from a hardcoded path (see #13) * Add a MazeAppModelMessage class to pass proper messages to observers * Replace setMaze(maze) by loadMaze(filename) for now * Move paintMaze into ui/MazeApp ui: * Add a Save As button (just a new name for the previous Save button) and implement an actual Save button (which saves into the file currently being used), disabled when no file is currently in use. (see #16) * Create the FileBox class to handle file choices for the load, save, save as buttons * Add keystrokes to trigger Load, Save or Save As actions from the menu maze: * Add a method to Maze to perform a deep copy of a matrix of boxes * Add a static method to generate an empty maze of a fixed size (10*10) This behavior might change in the future * Add a constructor to make a Maze from a matrix of boxes --- src/maze/Maze.java | 58 +++++++++++++++++++---- src/model/Box.java | 10 ---- src/model/Maze.java | 12 ----- src/model/MazeAppModel.java | 75 ++++++++++++++---------------- src/model/MazeAppModelMessage.java | 6 +++ src/ui/FileBox.java | 36 ++++++++++++++ src/ui/FileMenu.java | 6 ++- src/ui/LoadMazeMenuItem.java | 27 +++-------- src/ui/MazeDrawing.java | 39 ++++++++++++++-- src/ui/QuitMenuItem.java | 12 +++-- src/ui/SaveMazeAsMenuItem.java | 32 +++++++++++++ src/ui/SaveMazeMenuItem.java | 38 ++++++++------- 12 files changed, 232 insertions(+), 119 deletions(-) delete mode 100644 src/model/Box.java delete mode 100644 src/model/Maze.java create mode 100644 src/model/MazeAppModelMessage.java create mode 100644 src/ui/FileBox.java create mode 100644 src/ui/SaveMazeAsMenuItem.java diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 8e6edad..e5a5ece 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -29,31 +29,71 @@ public class Maze } - public Maze(Maze maze){ + public Maze(Maze maze) { this.dimensionX = maze.getDimensionX(); this.dimensionY = maze.getDimensionY(); - this.maze = new Box[dimensionY][dimensionX]; + this.maze = matrixDeepCopy(maze.getMaze()); + } - for (int line = 0; line < dimensionY; line++) { - for (int row = 0; row < dimensionX; row++) { - Box box = maze.getMaze()[line][row]; + public Maze(int numberOfLines, int numberOfRows, Box[][] boxes) { + this.dimensionY = numberOfLines; + this.dimensionX = numberOfRows; + this.maze = matrixDeepCopy(boxes); + } + + 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++; + } + if (numberOfLines > 0) { + for (Box box: original[0]) { + numberOfRows++; + } + } + 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++) { + Box box = original[line][row]; if (box.isWalkable()) { if (box.isDeparture()) { - this.maze[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); + copy[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); } else if (box.isArrival()) { - this.maze[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); + copy[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); } else { - this.maze[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); + copy[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); } } else { - this.maze[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); + copy[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); } } } + return copy; + } + + public static Maze emptySquareMaze() { + int dimensionX = 10; + int dimensionY = 10; + Box[][] maze = new Box[dimensionY][dimensionX]; + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + maze[line][row] = new EBox("E", line, row, null); + } + } + return new Maze(dimensionY, dimensionX, maze); } public int getCost(VertexInterface start, VertexInterface end) { diff --git a/src/model/Box.java b/src/model/Box.java deleted file mode 100644 index ff96031..0000000 --- a/src/model/Box.java +++ /dev/null @@ -1,10 +0,0 @@ -package model; - -import maze.Maze; - -public class Box extends maze.Box { - - public Box(String label, int x, int y, Maze maze) { - super(label, x, y, maze); - } -} diff --git a/src/model/Maze.java b/src/model/Maze.java deleted file mode 100644 index 9afdc55..0000000 --- a/src/model/Maze.java +++ /dev/null @@ -1,12 +0,0 @@ -package model; - -public class Maze extends maze.Maze { - - public Maze(String filename) { - super(filename); - } - - public Maze(Maze maze) { - super(maze); - } -} diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index f46b461..f43f38d 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -10,67 +10,60 @@ import java.util.Observer; import dijkstra.VertexInterface; import dijkstra.Dijkstra; import dijkstra.Previous; +import maze.Maze; public class MazeAppModel extends Observable { - private Maze maze = new Maze("data/labyrinthe.txt"); - private VertexInterface departure = maze.getDeparture(); - private VertexInterface arrival = maze.getArrival(); - private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure); - private ArrayList shortest = previous.getShortestPathTo(arrival); - private ArrayList boxList = maze.getVertexes(); + private String filename; + private Maze maze; + private VertexInterface departure; + private VertexInterface arrival; + private Previous previous; + private ArrayList shortest; + private ArrayList boxList; private Boolean modified = false; - public void setMaze(Maze maze) { - this.maze = new Maze(maze); + public MazeAppModel() { + maze = new Maze(Maze.emptySquareMaze()); + departure = maze.getDeparture(); + arrival = maze.getArrival(); + boxList = maze.getVertexes(); + if (departure == null || arrival == null) { + return; + } + previous = (Previous) Dijkstra.dijkstra(maze, departure); + shortest = previous.getShortestPathTo(arrival); + } + + public void loadMaze(String filename) { + this.filename = new String(filename); + maze = new Maze(this.filename); modified = true; setChanged(); - notifyObservers(maze); + notifyObservers(MazeAppModelMessage.MazeRenewal); } public Maze getMaze() { return maze; } + public String getFilename() { + return filename; + } + public boolean isModified() { return modified; } public void saveToFile() { - maze.saveToTextFile("data/labyOutput.txt"); - } - - public void saveToFile(String filename) { maze.saveToTextFile(filename); } - public void paintMaze(Graphics2D g, int width, int height) { - - int dimensionX = maze.getDimensionX(); - int dimensionY = maze.getDimensionY(); - int boxWidth = width/dimensionX; - int boxHeight = height/dimensionY; - - for (int line = 0; line < dimensionY; line++) { - for (int row = 0; row < dimensionX; row++) { - if (maze.getMaze()[line][row].isWalkable()) { - if (maze.getMaze()[line][row].isDeparture()) { - g.setColor(Color.GREEN); - } - else if (maze.getMaze()[line][row].isArrival()) { - g.setColor(Color.RED); - } - else { - g.setColor(Color.BLACK); - } - } - else { - g.setColor(Color.WHITE); - } - g.fill3DRect(row*boxWidth, line*boxHeight, - boxWidth, boxHeight, - true); - } - } + public void saveToFile(String filename) { + this.filename = new String(filename); + maze.saveToTextFile(this.filename); + modified = true; + setChanged(); + notifyObservers(MazeAppModelMessage.FileChange); } } diff --git a/src/model/MazeAppModelMessage.java b/src/model/MazeAppModelMessage.java new file mode 100644 index 0000000..af9cf63 --- /dev/null +++ b/src/model/MazeAppModelMessage.java @@ -0,0 +1,6 @@ +package model; + +public class MazeAppModelMessage { + public final static String MazeRenewal = "MazeRenewal"; + public final static String FileChange = "FileChange"; +} diff --git a/src/ui/FileBox.java b/src/ui/FileBox.java new file mode 100644 index 0000000..fc651c2 --- /dev/null +++ b/src/ui/FileBox.java @@ -0,0 +1,36 @@ +package ui; + +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileNameExtensionFilter; + +public class FileBox { + private static JFileChooser fileChooser = new JFileChooser(); + private static FileNameExtensionFilter filter = + new FileNameExtensionFilter("Text files", "txt"); + + public static String load(MazeApp mazeApp) { + fileChooser.setFileFilter(filter); + + int returnVal = fileChooser.showOpenDialog(mazeApp); + if(returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to open this file: " + + fileChooser.getSelectedFile().getName()); + String filename = fileChooser.getSelectedFile().getPath(); + return filename; + } + return null; + } + + public static String save(MazeApp mazeApp) { + fileChooser.setFileFilter(filter); + + int returnVal = fileChooser.showSaveDialog(mazeApp); + if(returnVal == JFileChooser.APPROVE_OPTION) { + System.out.println("You chose to save to this file: " + + fileChooser.getSelectedFile().getName()); + String filename = fileChooser.getSelectedFile().getPath(); + return filename; + } + return null; + } +} diff --git a/src/ui/FileMenu.java b/src/ui/FileMenu.java index 67447c3..baadb8e 100644 --- a/src/ui/FileMenu.java +++ b/src/ui/FileMenu.java @@ -5,17 +5,19 @@ import javax.swing.JMenu; public class FileMenu extends JMenu { private final MazeApp mazeApp; - private final QuitMenuItem quitMenuItem; private final LoadMazeMenuItem loadMazeMenuItem; private final SaveMazeMenuItem saveMazeMenuItem; + private final SaveMazeAsMenuItem saveMazeAsMenuItem; + private final QuitMenuItem quitMenuItem; public FileMenu(MazeApp mazeApp) { super("File"); this.mazeApp = mazeApp; - add(quitMenuItem = new QuitMenuItem(mazeApp)); add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp)); add(saveMazeMenuItem = new SaveMazeMenuItem(mazeApp)); + add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp)); + add(quitMenuItem = new QuitMenuItem(mazeApp)); } } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index b0147f6..40d4a49 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -1,16 +1,12 @@ package ui; -import javax.swing.JFileChooser; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.filechooser.FileNameExtensionFilter; - -import model.MazeAppModel; -import model.Maze; - +import javax.swing.KeyStroke; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import model.MazeAppModel; + public class LoadMazeMenuItem extends JMenuItem implements ActionListener { @@ -20,25 +16,16 @@ public class LoadMazeMenuItem extends JMenuItem super("Load maze"); this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control O")); addActionListener(this); } public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - JFileChooser fileChooser = new JFileChooser(); - - FileNameExtensionFilter filter = - new FileNameExtensionFilter("Text files", "txt"); - fileChooser.setFileFilter(filter); - - String filename; - int returnVal = fileChooser.showOpenDialog(mazeApp); - if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to open this file: " + - fileChooser.getSelectedFile().getName()); - filename = fileChooser.getSelectedFile().getPath(); - mazeApp.getMazeAppModel().setMaze(new Maze(filename)); + String filename = FileBox.load(mazeApp); + if (filename != null) { + mazeApp.getMazeAppModel().loadMaze(filename); } } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index f43e4ea..3f33f4c 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -7,6 +7,7 @@ import java.awt.Graphics2D; import java.awt.GridLayout; import javax.swing.JPanel; import maze.Maze; +import model.MazeAppModelMessage; public class MazeDrawing extends JPanel { @@ -17,7 +18,6 @@ public class MazeDrawing extends JPanel { this.mazeApp = mazeApp; setBackground(Color.BLACK); setPreferredSize(new Dimension(500,500)) ; - } @Override @@ -29,13 +29,44 @@ public class MazeDrawing extends JPanel { int width = getWidth(); int height = getHeight(); - mazeApp.getMazeAppModel().paintMaze(g2d, width, height); - + paintMaze(g2d, width, height); + } + + private void paintMaze(Graphics2D g, int width, int height) { + Maze maze = mazeApp.getMazeAppModel().getMaze(); + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + int boxWidth = width / dimensionX; + int boxHeight = height / dimensionY; + + for (int line = 0; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + if (maze.getMaze()[line][row].isWalkable()) { + if (maze.getMaze()[line][row].isDeparture()) { + g.setColor(Color.GREEN); + } + else if (maze.getMaze()[line][row].isArrival()) { + g.setColor(Color.RED); + } + else { + g.setColor(Color.BLACK); + } + } + else { + g.setColor(Color.WHITE); + } + g.fill3DRect(row * boxWidth, + line * boxHeight, + boxWidth, + boxHeight, + true); + } + } } public void notifyForUpdates(Object param) { - if (param instanceof Maze) { + if (param == MazeAppModelMessage.MazeRenewal) { this.repaint(); } } diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 46908e5..13250a0 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -5,6 +5,7 @@ import java.awt.event.ActionListener; import javax.swing.JMenuItem; import javax.swing.JOptionPane; +import javax.swing.KeyStroke; import model.MazeAppModel; @@ -17,6 +18,7 @@ public class QuitMenuItem extends JMenuItem super("Quit"); this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control Q")); addActionListener(this); } @@ -26,11 +28,11 @@ public class QuitMenuItem extends JMenuItem if (mazeAppModel.isModified()) { int response = JOptionPane.showInternalOptionDialog(this, - "Maze not saved. Save it ?", - "Quit application", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.WARNING_MESSAGE, - null,null,null); + "Maze not saved. Save it ?", + "Quit application", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.WARNING_MESSAGE, + null, null, null); switch (response) { case JOptionPane.CANCEL_OPTION: return; diff --git a/src/ui/SaveMazeAsMenuItem.java b/src/ui/SaveMazeAsMenuItem.java new file mode 100644 index 0000000..22195f1 --- /dev/null +++ b/src/ui/SaveMazeAsMenuItem.java @@ -0,0 +1,32 @@ +package ui; + +import javax.swing.JMenuItem; +import javax.swing.KeyStroke; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import model.MazeAppModel; + +public class SaveMazeAsMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public SaveMazeAsMenuItem(MazeApp mazeApp) { + + super("Save maze as"); + this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control shift S")); + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + + String filename = FileBox.save(mazeApp); + if (filename != null) { + mazeApp.getMazeAppModel().saveToFile(filename); + } + } +} diff --git a/src/ui/SaveMazeMenuItem.java b/src/ui/SaveMazeMenuItem.java index 4beefbe..f5026d2 100644 --- a/src/ui/SaveMazeMenuItem.java +++ b/src/ui/SaveMazeMenuItem.java @@ -1,16 +1,16 @@ package ui; -import javax.swing.JFileChooser; import javax.swing.JMenuItem; -import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.KeyStroke; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Observer; +import java.util.Observable; import model.MazeAppModel; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - public class SaveMazeMenuItem extends JMenuItem - implements ActionListener { + implements ActionListener, Observer { private final MazeApp mazeApp; @@ -18,24 +18,30 @@ public class SaveMazeMenuItem extends JMenuItem super("Save maze"); this.mazeApp = mazeApp; + this.setAccelerator(KeyStroke.getKeyStroke("control S")); + mazeApp.getMazeAppModel().addObserver(this); + if (mazeApp.getMazeAppModel().getFilename() == null) { + this.setEnabled(false); + } addActionListener(this); } public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - JFileChooser fileChooser = new JFileChooser(); - FileNameExtensionFilter filter = - new FileNameExtensionFilter("Text files", "txt"); - fileChooser.setFileFilter(filter); + if (mazeAppModel.getFilename() != null) { + mazeAppModel.saveToFile(); + } + } - int returnVal = fileChooser.showSaveDialog(mazeApp); - if(returnVal == JFileChooser.APPROVE_OPTION) { - System.out.println("You chose to save to this file: " + - fileChooser.getSelectedFile().getName()); - String filename = fileChooser.getSelectedFile().getPath(); - mazeApp.getMazeAppModel().saveToFile(filename); + @Override + public void update(Observable obs, Object param) { + if (mazeApp.getMazeAppModel().getFilename() != null) { + this.setEnabled(true); + } + else { + this.setEnabled(true); } } } -- 2.56.1 From 97f9aa4ba06b4d231750283a10545abbab558ca0 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 23 Dec 2017 21:00:03 +0100 Subject: [PATCH 20/37] Add generic maze generation methods maze: * Change emptySquareMaze() to become emptyMaze(int dimension) * emptyMaze now takes 1 or 2 parameteres, and accordingly generate a square or a rectangle maze of the given dimension(s) * Add wallMaze, which generate a maze full of walls. As emptyMaze, it takes one or two parameters model: * Update mazeAppModel tu use the new emptyMaze method --- src/maze/Maze.java | 42 ++++++++++++++++++++++++++++++------- src/model/MazeAppModel.java | 2 +- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/maze/Maze.java b/src/maze/Maze.java index e5a5ece..f626bef 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -84,16 +84,44 @@ public class Maze return copy; } - public static Maze emptySquareMaze() { - int dimensionX = 10; - int dimensionY = 10; - Box[][] maze = new Box[dimensionY][dimensionX]; - for (int line = 0; line < dimensionY; line++) { - for (int row = 0; row < dimensionX; row++) { + public static Maze emptyMaze(int dimension) { + Box[][] maze = new Box[dimension][dimension]; + for (int line = 0; line < dimension; line++) { + for (int row = 0; row < dimension; row++) { maze[line][row] = new EBox("E", line, row, null); } } - return new Maze(dimensionY, dimensionX, maze); + return new Maze(dimension, dimension, maze); + } + + public static Maze emptyMaze(int numberOfLines, int numberOfRows) { + Box[][] maze = new Box[numberOfLines][numberOfRows]; + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + maze[line][row] = new EBox("E", line, row, null); + } + } + return new Maze(numberOfLines, numberOfRows, maze); + } + + public static Maze wallMaze(int dimension) { + Box[][] maze = new Box[dimension][dimension]; + for (int line = 0; line < dimension; line++) { + for (int row = 0; row < dimension; row++) { + maze[line][row] = new WBox("W", line, row, null); + } + } + return new Maze(dimension, dimension, maze); + } + + public static Maze wallMaze(int numberOfLines, int numberOfRows) { + Box[][] maze = new Box[numberOfLines][numberOfRows]; + for (int line = 0; line < numberOfLines; line++) { + for (int row = 0; row < numberOfRows; row++) { + maze[line][row] = new WBox("W", line, row, null); + } + } + return new Maze(numberOfLines, numberOfRows, maze); } public int getCost(VertexInterface start, VertexInterface end) { diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index f43f38d..ae03bda 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -24,7 +24,7 @@ public class MazeAppModel extends Observable { private Boolean modified = false; public MazeAppModel() { - maze = new Maze(Maze.emptySquareMaze()); + maze = new Maze(Maze.emptyMaze(10)); departure = maze.getDeparture(); arrival = maze.getArrival(); boxList = maze.getVertexes(); -- 2.56.1 From 80ffabdd69100f19fd8a7f0ea451da124ba915f6 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 23 Dec 2017 21:21:47 +0100 Subject: [PATCH 21/37] Refresh attributes in MazeAppModel at maze loading model: * Add initModelFromMaze() to refresh the model attributes, such as arrival, departure, previous, etc, at maze change --- src/model/MazeAppModel.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index ae03bda..7ab7954 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -14,7 +14,7 @@ import maze.Maze; public class MazeAppModel extends Observable { - private String filename; + private String filename; private Maze maze; private VertexInterface departure; private VertexInterface arrival; @@ -25,10 +25,16 @@ public class MazeAppModel extends Observable { public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); + initModelFromMaze(); + } + + private void initModelFromMaze() { departure = maze.getDeparture(); arrival = maze.getArrival(); boxList = maze.getVertexes(); if (departure == null || arrival == null) { + previous = null; + shortest = null; return; } previous = (Previous) Dijkstra.dijkstra(maze, departure); @@ -38,6 +44,7 @@ public class MazeAppModel extends Observable { public void loadMaze(String filename) { this.filename = new String(filename); maze = new Maze(this.filename); + initModelFromMaze(); modified = true; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); -- 2.56.1 From b5eb6b0b8cb8765b422d7e150b19bb2e41b9ba46 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 23 Dec 2017 22:31:56 +0100 Subject: [PATCH 22/37] Move paintShortestPath from model to ui --- src/model/MazeAppModel.java | 14 -------------- src/ui/MazeApp.java | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 815f3f6..e0be3d1 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -8,7 +8,6 @@ import java.util.Observable; import java.util.Observer; import dijkstra.VertexInterface; -import maze.EBox; import dijkstra.Dijkstra; import dijkstra.Previous; @@ -78,17 +77,4 @@ public class MazeAppModel extends Observable { } } } - - public void paintShortestPath(Graphics2D g, int width, int height) { - - g.setColor(Color.ORANGE); - int dimensionX = maze.getDimensionX(); - int dimensionY = maze.getDimensionY(); - int boxWidth = width/dimensionX; - int boxHeight = height/dimensionY; - for (VertexInterface vertex : shortest) { - EBox box = (EBox) vertex; - g.fillOval(box.getX()*boxHeight, box.getY()*boxWidth, boxWidth, boxHeight); - } - } } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 2e7b37d..03f00ef 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -3,6 +3,7 @@ package ui; import javax.swing.*; import maze.Maze; import model.MazeAppModel; +import maze.EBox; import java.util.Observable; import java.util.Observer; @@ -40,4 +41,17 @@ public class MazeApp extends JFrame System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } + + public void paintShortestPath(Graphics2D g, int width, int height) { + + g.setColor(Color.ORANGE); + int dimensionX = maze.getDimensionX(); + int dimensionY = maze.getDimensionY(); + int boxWidth = width/dimensionX; + int boxHeight = height/dimensionY; + for (VertexInterface vertex : shortest) { + EBox box = (EBox) vertex; + g.fillOval(box.getX()*boxHeight, box.getY()*boxWidth, boxWidth, boxHeight); + } + } } -- 2.56.1 From 1e934b05e64d477bf8adc4f2df188a0eff9b6d16 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 24 Dec 2017 19:09:14 +0100 Subject: [PATCH 23/37] Fix paintShortestPath and move it into MazeDrawing model: * Add getShortest() to MazeAppModel, which returns the list of vertexes on the shortest path from departure to arrival ui: * Move paintShortestPath from MazeApp to MazeDrawing * Fix a bug in paintShortestPath where a switch between width and height caused ellipses to be mislocated (probably still the same confusion between the x and y axes) --- src/model/MazeAppModel.java | 4 ++++ src/ui/MazeApp.java | 13 ------------- src/ui/MazeDrawing.java | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index e0be3d1..b782287 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -36,6 +36,10 @@ public class MazeAppModel extends Observable { return maze; } + public ArrayList getShortest() { + return shortest; + } + public boolean isModified() { return modified; } diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 03f00ef..48c6256 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -41,17 +41,4 @@ public class MazeApp extends JFrame System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } - - public void paintShortestPath(Graphics2D g, int width, int height) { - - g.setColor(Color.ORANGE); - int dimensionX = maze.getDimensionX(); - int dimensionY = maze.getDimensionY(); - int boxWidth = width/dimensionX; - int boxHeight = height/dimensionY; - for (VertexInterface vertex : shortest) { - EBox box = (EBox) vertex; - g.fillOval(box.getX()*boxHeight, box.getY()*boxWidth, boxWidth, boxHeight); - } - } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index 2a83fe1..ce96b79 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -7,6 +7,8 @@ import java.awt.Graphics2D; import java.awt.GridLayout; import javax.swing.JPanel; import maze.Maze; +import maze.EBox; +import dijkstra.VertexInterface; public class MazeDrawing extends JPanel { @@ -38,4 +40,17 @@ public class MazeDrawing extends JPanel { this.repaint(); } } + + public void paintShortestPath(Graphics2D g, int width, int height) { + + g.setColor(Color.ORANGE); + int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); + int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); + int boxWidth = width / dimensionX; + int boxHeight = height / dimensionY; + for (VertexInterface vertex : mazeApp.getMazeAppModel().getShortest()) { + EBox box = (EBox) vertex; + g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight); + } + } } -- 2.56.1 From c0afbe104c3523451dd431e6a8355d783165c5f0 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 24 Dec 2017 20:20:40 +0100 Subject: [PATCH 24/37] Add menu button to draw the shortest path ui: * Add methods to draw the shortest path from MazeApp, WindowPanel and MazeDrawing * Add a Maze menu with a button to draw the shortest path from departure to arrival --- src/ui/DrawShortestPathMenuItem.java | 25 +++++++++++++++++++++++++ src/ui/MazeApp.java | 4 ++++ src/ui/MazeDrawing.java | 8 ++++++-- src/ui/MazeMenu.java | 17 +++++++++++++++++ src/ui/MazeMenuBar.java | 2 ++ src/ui/WindowPanel.java | 3 +++ 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/ui/DrawShortestPathMenuItem.java create mode 100644 src/ui/MazeMenu.java diff --git a/src/ui/DrawShortestPathMenuItem.java b/src/ui/DrawShortestPathMenuItem.java new file mode 100644 index 0000000..4338172 --- /dev/null +++ b/src/ui/DrawShortestPathMenuItem.java @@ -0,0 +1,25 @@ +package ui; + +import javax.swing.JMenuItem; + +import model.MazeAppModel; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class DrawShortestPathMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public DrawShortestPathMenuItem(MazeApp mazeApp) { + + super("Draw shortest path"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.drawShortestPath(); + } +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 48c6256..13e350d 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -41,4 +41,8 @@ public class MazeApp extends JFrame System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } + + public void drawShortestPath() { + windowPanel.paintShortestPath(); + } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index ce96b79..b5ca367 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -19,7 +19,7 @@ public class MazeDrawing extends JPanel { this.mazeApp = mazeApp; setBackground(Color.BLACK); setPreferredSize(new Dimension(500,500)) ; - + } @Override @@ -41,7 +41,7 @@ public class MazeDrawing extends JPanel { } } - public void paintShortestPath(Graphics2D g, int width, int height) { + private void paintShortestPath(Graphics2D g, int width, int height) { g.setColor(Color.ORANGE); int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); @@ -53,4 +53,8 @@ public class MazeDrawing extends JPanel { g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight); } } + + public void paintShortestPath() { + paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); + } } diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java new file mode 100644 index 0000000..d8348a6 --- /dev/null +++ b/src/ui/MazeMenu.java @@ -0,0 +1,17 @@ +package ui; + +import javax.swing.JMenu; + +public class MazeMenu extends JMenu { + + private final MazeApp mazeApp; + private final DrawShortestPathMenuItem drawMenuItem; + + public MazeMenu(MazeApp mazeApp) { + + super("Maze"); + this.mazeApp = mazeApp; + + add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp)); + } +} diff --git a/src/ui/MazeMenuBar.java b/src/ui/MazeMenuBar.java index 60fba7a..afe76da 100644 --- a/src/ui/MazeMenuBar.java +++ b/src/ui/MazeMenuBar.java @@ -6,6 +6,7 @@ public class MazeMenuBar extends JMenuBar { private final MazeApp mazeApp; private final FileMenu fileMenu; + private final MazeMenu mazeMenu; public MazeMenuBar(MazeApp mazeApp) { @@ -13,5 +14,6 @@ public class MazeMenuBar extends JMenuBar { this.mazeApp = mazeApp; add(fileMenu = new FileMenu(mazeApp)); + add(mazeMenu = new MazeMenu(mazeApp)); } } diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 0599a33..8ff60ca 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -19,4 +19,7 @@ public class WindowPanel extends JPanel drawingPanelGrid.notifyForUpdates(param); } + public void paintShortestPath() { + drawingPanelGrid.paintShortestPath(); + } } -- 2.56.1 From 7242872b834a9c04037d353e13a51fd54dd06164 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 24 Dec 2017 21:27:20 +0100 Subject: [PATCH 25/37] Add a button to clear path model: * Add an attribute to MazeAppModel to keep track of if the path is drawn or not. For future use to enable/disable the Clear button depending on if the path is drawn or not ui: * Add a "Clear shortest path" button to the Maze menu * Add a method to clear the path in MazeApp (just trigger a repainting of the MazeApp, which erase the path, since its not drawn by default) and update MazeAppModel accordingly --- src/model/MazeAppModel.java | 9 +++++++++ src/ui/ClearShortestPathMenuItem.java | 25 +++++++++++++++++++++++++ src/ui/MazeApp.java | 6 ++++++ src/ui/MazeMenu.java | 2 ++ 4 files changed, 42 insertions(+) create mode 100644 src/ui/ClearShortestPathMenuItem.java diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index b782287..ddbd438 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -20,6 +20,7 @@ public class MazeAppModel extends Observable { private ArrayList shortest = previous.getShortestPathTo(arrival); //private ArrayList boxList = maze.getVertexes(); private Boolean modified = false; + private boolean pathDrawn = false; public void setMaze(Maze maze) { this.maze = new Maze(maze); @@ -44,6 +45,14 @@ public class MazeAppModel extends Observable { return modified; } + public boolean isPathDrawn() { + return pathDrawn; + } + + public void setPathDrawn(boolean pathDrawn) { + this.pathDrawn = pathDrawn; + } + public void saveToFile() { maze.saveToTextFile("data/labyOutput.txt"); } diff --git a/src/ui/ClearShortestPathMenuItem.java b/src/ui/ClearShortestPathMenuItem.java new file mode 100644 index 0000000..e5bff7b --- /dev/null +++ b/src/ui/ClearShortestPathMenuItem.java @@ -0,0 +1,25 @@ +package ui; + +import javax.swing.JMenuItem; + +import model.MazeAppModel; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ClearShortestPathMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public ClearShortestPathMenuItem(MazeApp mazeApp) { + + super("Clear shortest path"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + public void actionPerformed(ActionEvent evt) { + mazeApp.clearShortestPath(); + } +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 13e350d..9a2ce6a 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -43,6 +43,12 @@ public class MazeApp extends JFrame } public void drawShortestPath() { + mazeAppModel.setPathDrawn(true); windowPanel.paintShortestPath(); } + + public void clearShortestPath() { + mazeAppModel.setPathDrawn(false); + this.repaint(); + } } diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index d8348a6..e257339 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -6,6 +6,7 @@ public class MazeMenu extends JMenu { private final MazeApp mazeApp; private final DrawShortestPathMenuItem drawMenuItem; + private final ClearShortestPathMenuItem clearMenuItem; public MazeMenu(MazeApp mazeApp) { @@ -13,5 +14,6 @@ public class MazeMenu extends JMenu { this.mazeApp = mazeApp; add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp)); + add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp)); } } -- 2.56.1 From 9b272e5f25f9985f44e5def436a9d9038c0156c8 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Tue, 26 Dec 2017 14:45:11 +0100 Subject: [PATCH 26/37] Ask to save before loading a new maze model: * Deprecate modified from MazeAppModel, which was misused and misunderstood * Add saved to MazeAppModel to keep track if a maze (or some modification) was saved into a file or not ui: * Use the new saved attribute to know if the maze was saved or not * Move the box asking whether to save the maze or not from QuitMenuItem to SaveBox and call SaveBox from QuitMenuItem * SaveBox now checks if the maze has a source file or not, if not it prompts a box to ask where to save the maze * If the maze was modified, the Load Maze button will pop SaveBox before erasing the current maze --- src/model/MazeAppModel.java | 11 ++++----- src/ui/LoadMazeMenuItem.java | 5 +++++ src/ui/QuitMenuItem.java | 22 +++++------------- src/ui/SaveBox.java | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 src/ui/SaveBox.java diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 7ab7954..4214be0 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -21,7 +21,7 @@ public class MazeAppModel extends Observable { private Previous previous; private ArrayList shortest; private ArrayList boxList; - private Boolean modified = false; + private boolean saved = true; public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); @@ -45,7 +45,7 @@ public class MazeAppModel extends Observable { this.filename = new String(filename); maze = new Maze(this.filename); initModelFromMaze(); - modified = true; + saved = true; setChanged(); notifyObservers(MazeAppModelMessage.MazeRenewal); } @@ -58,18 +58,19 @@ public class MazeAppModel extends Observable { return filename; } - public boolean isModified() { - return modified; + public boolean isSaved() { + return saved; } public void saveToFile() { maze.saveToTextFile(filename); + saved = true; } public void saveToFile(String filename) { this.filename = new String(filename); maze.saveToTextFile(this.filename); - modified = true; + saved = true; setChanged(); notifyObservers(MazeAppModelMessage.FileChange); } diff --git a/src/ui/LoadMazeMenuItem.java b/src/ui/LoadMazeMenuItem.java index 40d4a49..05cf17e 100644 --- a/src/ui/LoadMazeMenuItem.java +++ b/src/ui/LoadMazeMenuItem.java @@ -23,6 +23,11 @@ public class LoadMazeMenuItem extends JMenuItem public void actionPerformed(ActionEvent evt) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){ + return; + } + } String filename = FileBox.load(mazeApp); if (filename != null) { mazeApp.getMazeAppModel().loadMaze(filename); diff --git a/src/ui/QuitMenuItem.java b/src/ui/QuitMenuItem.java index 13250a0..c8632ce 100644 --- a/src/ui/QuitMenuItem.java +++ b/src/ui/QuitMenuItem.java @@ -26,23 +26,11 @@ public class QuitMenuItem extends JMenuItem MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - if (mazeAppModel.isModified()) { - int response = JOptionPane.showInternalOptionDialog(this, - "Maze not saved. Save it ?", - "Quit application", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.WARNING_MESSAGE, - null, null, null); - switch (response) { - case JOptionPane.CANCEL_OPTION: - return; - case JOptionPane.OK_OPTION: - mazeAppModel.saveToFile(); - break; - case JOptionPane.NO_OPTION: - break; - } - } + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) { + return; + } + } System.exit(0); } } diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java new file mode 100644 index 0000000..d333279 --- /dev/null +++ b/src/ui/SaveBox.java @@ -0,0 +1,43 @@ +package ui; + +import javax.swing.JOptionPane; +import java.awt.Component; + +import model.MazeAppModel; + +public class SaveBox { + private static String message = "Maze not saved. Save it ?"; + + public static boolean promptAndContinue(Component parent, + MazeApp mazeApp, + String title) { + MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); + int response = JOptionPane.showInternalOptionDialog(parent, + message, + title, + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.WARNING_MESSAGE, + null, null, null); + switch (response) { + case JOptionPane.CANCEL_OPTION: + return false; + case JOptionPane.OK_OPTION: + if (mazeAppModel.getFilename() != null) { + mazeAppModel.saveToFile(); + } + else { + String filename = FileBox.save(mazeApp); + if (filename != null) { + mazeApp.getMazeAppModel().saveToFile(filename); + } + else { + return promptAndContinue(null, mazeApp, title); + } + } + break; + case JOptionPane.NO_OPTION: + break; + } + return true; + } +} -- 2.56.1 From 8bef42e2a6d5836e26fb56edfebd5e2bde3ef864 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Tue, 26 Dec 2017 16:41:11 +0100 Subject: [PATCH 27/37] Closing the window now does the same as Quit ui: * Add a WindowListener catching the event of closing the window to ask the user if the maze must be saved or not (if it's not already saved) (same behavior as the Quit button) (see #19) --- src/ui/MazeApp.java | 40 ++++++++++++++++++++++++++++++++++++++-- src/ui/SaveBox.java | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 2e7b37d..de0f36a 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -6,9 +6,11 @@ import model.MazeAppModel; import java.util.Observable; import java.util.Observer; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; public class MazeApp extends JFrame - implements Observer { + implements Observer, WindowListener { private final MazeMenuBar mazeMenuBar; private final WindowPanel windowPanel; @@ -21,10 +23,11 @@ public class MazeApp extends JFrame this.mazeAppModel = new MazeAppModel(); mazeAppModel.addObserver(this); + addWindowListener(this); setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); setContentPane(windowPanel = new WindowPanel(this)); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); pack(); setVisible(true); @@ -40,4 +43,37 @@ public class MazeApp extends JFrame System.out.println("New maze detected"); windowPanel.notifyForUpdates(param); } + + public void windowClosing(WindowEvent e) { + if (! mazeAppModel.isSaved()) { + if (! SaveBox.promptAndContinue(null, this, "Quit application")) { + return; + } + } + System.exit(0); + } + + public void windowDeactivated(WindowEvent e) { + return; + } + + public void windowActivated(WindowEvent e) { + return; + } + + public void windowDeiconified(WindowEvent e) { + return; + } + + public void windowIconified(WindowEvent e) { + return; + } + + public void windowClosed(WindowEvent e) { + return; + } + + public void windowOpened(WindowEvent e) { + return; + } } diff --git a/src/ui/SaveBox.java b/src/ui/SaveBox.java index d333279..ae354f2 100644 --- a/src/ui/SaveBox.java +++ b/src/ui/SaveBox.java @@ -12,7 +12,7 @@ public class SaveBox { MazeApp mazeApp, String title) { MazeAppModel mazeAppModel = mazeApp.getMazeAppModel(); - int response = JOptionPane.showInternalOptionDialog(parent, + int response = JOptionPane.showOptionDialog(parent, message, title, JOptionPane.YES_NO_CANCEL_OPTION, -- 2.56.1 From 11200ebed29760b5601cb35a4384c5920d808b79 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Tue, 26 Dec 2017 22:16:40 +0100 Subject: [PATCH 28/37] Fix repainting when the path is drawn ui: * Fix repainting when the path is supposed to be drawn. At any repainting (window resizing, menu unfolding), the maze was repainted without the shortest path. We now use the attribute stored in the model to know whether the path should be drawn or not when we repaint the panel (see #24) --- src/ui/MazeDrawing.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index b5ca367..32874f0 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -32,6 +32,9 @@ public class MazeDrawing extends JPanel { int height = getHeight(); mazeApp.getMazeAppModel().paintMaze(g2d, width, height); + if (mazeApp.getMazeAppModel().isPathDrawn()) { + paintShortestPath(g2d, width, height); + } } public void notifyForUpdates(Object param) { -- 2.56.1 From c7134e9e913d3c1206b83de0e1d6a1631f0e41bd Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Wed, 27 Dec 2017 15:13:40 +0100 Subject: [PATCH 29/37] Fix infinite path in Dijkstra algorithm --- src/dijkstra/Dijkstra.java | 2 +- src/dijkstra/VertexInterface.java | 9 ++++++++- src/maze/Box.java | 8 ++++---- src/maze/Maze.java | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index 1ed1786..5dc2371 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -22,7 +22,7 @@ public class Dijkstra { int n = g.getVertexNumber(); for (int j = 0; j < n; j++) { - for (VertexInterface vertex: g.generateNeighbours(pivot)) { + for (VertexInterface vertex: pivot.generateNeighbours()) { if (!a.contains(vertex)) { if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) { pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index 51d147d..32e1e44 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -1,5 +1,7 @@ package dijkstra; +import java.util.ArrayList; + /** * This describes a vertex as seen in graphs (see INF101 course). * @@ -10,5 +12,10 @@ public interface VertexInterface { * @return The label of the element. */ public String getLabel() ; - + + /** + * Updates the attribute of the element. + * @return The neighbourList of the element. + */ + public ArrayList generateNeighbours(); } diff --git a/src/maze/Box.java b/src/maze/Box.java index 3a665e7..0c17d17 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -22,7 +22,7 @@ public abstract class Box private int y ; private String label = null ; private Maze maze ; - private ArrayList neighbourList = new ArrayList() ; + private ArrayList neighbourList; public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation @@ -33,9 +33,9 @@ public abstract class Box } - public ArrayList generateNeighbors() { + public ArrayList generateNeighbours() { + this.neighbourList = maze.generateNeighbours(this); return maze.generateNeighbours(this) ; - } public boolean isWalkable() { @@ -50,7 +50,7 @@ public abstract class Box return false ; } - public ArrayList getNeighbourList() { + public ArrayList getNeighbourList() { return neighbourList; } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 8e6edad..2f22ccb 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -141,7 +141,7 @@ public class Maze return neighbourList ; } - + /**Constructs the maze from a text file, according to each single character on each line. * @param filename * The path to the file containing the representation of the maze. -- 2.56.1 From 37def85f5b23ef5793bc21f37c538b94abd9a0b5 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Thu, 28 Dec 2017 18:26:20 +0100 Subject: [PATCH 30/37] 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); } } } -- 2.56.1 From 81df80e7ea9c206d56b7e65d1eabdec41777fee6 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Fri, 29 Dec 2017 12:04:15 +0100 Subject: [PATCH 31/37] 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); + } +} -- 2.56.1 From 286023c5d8b1695a7e13d281cd32861dd6743982 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 30 Dec 2017 00:19:41 +0100 Subject: [PATCH 32/37] 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) { -- 2.56.1 From 024a4f70c384ebdc08375a70afb1dd4c36bb920d Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 31 Dec 2017 20:25:16 +0100 Subject: [PATCH 33/37] 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()); - } } -- 2.56.1 From 43d58c815b770f579a25604688a404e6cc167a4e Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Mon, 1 Jan 2018 18:32:38 +0100 Subject: [PATCH 34/37] 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; } -- 2.56.1 From 43feeaf93f7186f26cfde78df5dbe9d0f0348779 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 30 Dec 2017 00:19:41 +0100 Subject: [PATCH 35/37] 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) { -- 2.56.1 From e7c2ecb3532226fefa1e98ae3f1f998669bc8598 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 31 Dec 2017 13:05:17 +0100 Subject: [PATCH 36/37] Add path drawing interaction & animation feature --- src/MainTest.java | 1 + src/dijkstra/Dijkstra.java | 3 +- src/dijkstra/Previous.java | 7 +- src/dijkstra/VertexInterface.java | 12 +++ src/maze/Box.java | 13 ++- src/model/MazeAppModel.java | 28 +++++- src/ui/AnimatePathMenuItem.java | 27 ++++++ src/ui/MazeApp.java | 11 ++- src/ui/MazeDrawing.java | 141 ++++++++++++++++++++++++++---- src/ui/MazeMenu.java | 2 + src/ui/WindowPanel.java | 15 +++- 11 files changed, 227 insertions(+), 33 deletions(-) create mode 100644 src/ui/AnimatePathMenuItem.java diff --git a/src/MainTest.java b/src/MainTest.java index 66cdb93..06b5dd6 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,4 +1,5 @@ import java.util.ArrayList; + import dijkstra.*; import maze.*; import ui.*; diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index 5dc2371..0b907ce 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -27,10 +27,11 @@ public class Dijkstra { if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) { pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; previous.setPrevious(pivot, vertex) ; + pivot.addCandidate(vertex); } } } - + int piMax = Integer.MAX_VALUE ; for (VertexInterface vertex: vertexList) { if (! a.contains(vertex) && pi.getPi(vertex) < piMax) { diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 9f321e8..15f2280 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -2,6 +2,9 @@ package dijkstra; import java.util.ArrayList; import java.util.Hashtable; +import java.util.List; + +import maze.Box; public class Previous implements PreviousInterface { @@ -17,13 +20,11 @@ public class Previous implements PreviousInterface { public VertexInterface getPrevious(VertexInterface vertex) { return hashPrevious.get(vertex) ; - } public void setPrevious(VertexInterface father, VertexInterface son) { hashPrevious.put(son, father) ; - } public ArrayList getShortestPathTo(VertexInterface vertex) { @@ -31,7 +32,7 @@ public class Previous implements PreviousInterface { VertexInterface father = getPrevious(vertex) ; shortestPath.add(father) ; - if (getPrevious(father) == null) return shortestPath ; + if (((Box) getPrevious(father)).isDeparture()) return shortestPath ; else { return getShortestPathTo(father) ; } diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index 32e1e44..9496ecb 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -18,4 +18,16 @@ public interface VertexInterface { * @return The neighbourList of the element. */ public ArrayList generateNeighbours(); + + /** + * Adds all vertexes not in the ASet and candidate for an eventual path. + * @param vertex + */ + public void addCandidate(VertexInterface vertex); + + /** + * Returns the list of the candidates for an eventual path. + * @return + */ + public ArrayList getCandidates(); } diff --git a/src/maze/Box.java b/src/maze/Box.java index 0c17d17..e3b982e 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -23,6 +23,7 @@ public abstract class Box private String label = null ; private Maze maze ; private ArrayList neighbourList; + private ArrayList candidates; public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation @@ -30,14 +31,22 @@ public abstract class Box this.x = x ; this.y = y ; this.maze = maze ; - + this.candidates = new ArrayList(); } public ArrayList generateNeighbours() { this.neighbourList = maze.generateNeighbours(this); return maze.generateNeighbours(this) ; } - + + public void addCandidate(VertexInterface vertex) { + candidates.add(vertex); + } + + public ArrayList getCandidates() { + return candidates; + } + public boolean isWalkable() { return true ; } diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 027064e..fba34aa 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -19,8 +19,16 @@ public class MazeAppModel extends Observable { private Previous previous; private ArrayList shortest; private ArrayList boxList; - private boolean saved = true; - private boolean pathDrawn = false; + private boolean saved = true; + /* + * There are three states for pathDrawn : + * 0 : path isn't drawn + * 1 : path has been animated + * 2 : path has already been animated, but needs to be re-drawn + * because of window resizing. Therefore there is no need to + * animate the path again + */ + private int pathDrawn = 0; public MazeAppModel() { maze = new Maze(Maze.emptyMaze(10)); @@ -66,11 +74,19 @@ public class MazeAppModel extends Observable { return shortest; } - public boolean isPathDrawn() { + public ArrayList getShortestDeepCopy() { + ArrayList deepCopy = new ArrayList(); + for (VertexInterface vertex : this.shortest) { + deepCopy.add(vertex); + } + return deepCopy; + } + + public int isPathDrawn() { return pathDrawn; } - public void setPathDrawn(boolean pathDrawn) { + public void setPathDrawn(int pathDrawn) { this.pathDrawn = pathDrawn; } @@ -132,6 +148,10 @@ public class MazeAppModel extends Observable { saved = false; } + public VertexInterface getDeparture() { + return departure; + } + public void newClearMaze(int numberOfLines, int numberOfRows) { Box[][] newClearMaze = new Box[numberOfLines][numberOfRows]; diff --git a/src/ui/AnimatePathMenuItem.java b/src/ui/AnimatePathMenuItem.java new file mode 100644 index 0000000..f1f2f08 --- /dev/null +++ b/src/ui/AnimatePathMenuItem.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 AnimatePathMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + + public AnimatePathMenuItem(MazeApp mazeApp) { + + super("Animate shortest path"); + this.mazeApp = mazeApp; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent arg0) { + + mazeApp.animateShortestPath(); + } +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 48c54c5..bf01f0a 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -80,11 +80,18 @@ public class MazeApp extends JFrame public void drawShortestPath() { windowPanel.paintShortestPath(); - mazeAppModel.setPathDrawn(true); + mazeAppModel.setPathDrawn(2); } public void clearShortestPath() { - mazeAppModel.setPathDrawn(false); + windowPanel.clearShortestPath(); + mazeAppModel.setPathDrawn(0); this.repaint(); } + + public void animateShortestPath() { + + windowPanel.animateShortestPath(); + mazeAppModel.setPathDrawn(1); + } } diff --git a/src/ui/MazeDrawing.java b/src/ui/MazeDrawing.java index f32cae6..aea6f9e 100644 --- a/src/ui/MazeDrawing.java +++ b/src/ui/MazeDrawing.java @@ -5,7 +5,10 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseListener; +import java.util.Collections; import javax.swing.JPanel; import javax.swing.JOptionPane; import java.awt.event.MouseListener; @@ -18,10 +21,18 @@ import dijkstra.VertexInterface; public class MazeDrawing extends JPanel implements MouseListener { private final MazeApp mazeApp; + private boolean pathDrawingMode; + private ArrayList shortestPath; + private ArrayList finalPath; + private int pathCounter; public MazeDrawing(MazeApp mazeApp) { this.mazeApp = mazeApp; + this.pathDrawingMode = false; + this.shortestPath = new ArrayList(); + this.finalPath = null; + this.pathCounter = -1; setBackground(Color.BLACK); setPreferredSize(new Dimension(500,500)); addMouseListener(this); @@ -37,7 +48,9 @@ public class MazeDrawing extends JPanel implements MouseListener { int height = getHeight(); paintMaze(g2d, width, height); - if (mazeApp.getMazeAppModel().isPathDrawn()) { + if (mazeApp.getMazeAppModel().isPathDrawn() == 1) { + animateShortestPath(g2d, width, height); + } else if (mazeApp.getMazeAppModel().isPathDrawn() == 2) { paintShortestPath(g2d, width, height); } } @@ -77,20 +90,27 @@ public class MazeDrawing extends JPanel implements MouseListener { public void notifyForUpdates(Object param) { if (param == MazeAppModelMessage.MazeRenewal) { + if (this.mazeApp.getMazeAppModel().getShortest() != null) { + this.finalPath + = this.mazeApp.getMazeAppModel().getShortestDeepCopy(); + Collections.reverse(finalPath); + } this.repaint(); } } private void paintShortestPath(Graphics2D g, int width, int height) { - g.setColor(Color.ORANGE); + g.setColor(Color.WHITE); int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); int boxWidth = width / dimensionX; int boxHeight = height / dimensionY; + int factorWidth = boxWidth / 3; + int factorHeight = boxHeight / 3; ArrayList shortest = mazeApp.getMazeAppModel().getShortest(); - if (shortest == null && ! mazeApp.getMazeAppModel().isPathDrawn()) { + if (shortest == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { mazeApp.clearShortestPath(); JOptionPane.showMessageDialog(this, "Check that your graph contains a departure and an arrival," @@ -105,14 +125,107 @@ public class MazeDrawing extends JPanel implements MouseListener { } for (VertexInterface vertex : shortest) { EBox box = (EBox) vertex; - g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight); + g.fillOval(box.getX() * boxWidth + factorWidth, + box.getY()*boxHeight + factorHeight, + boxWidth - 2 * factorWidth, + boxHeight - 2 * factorHeight); } } + private void animateShortestPath(Graphics2D g, int width, int height) { + + g.setColor(Color.WHITE); + int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); + int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); + int boxWidth = width / dimensionX; + int boxHeight = height / dimensionY; + int factorWidth = boxWidth / 3; + int factorHeight = boxHeight / 3; + + if (this.finalPath == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) { + 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 (this.finalPath == null) { + mazeApp.clearShortestPath(); + return; + } + + pathDrawingMode = true; + + if (pathCounter == -1) { + JOptionPane.showMessageDialog(this, + "Click on the maze to see the shortest path evolve"); + pathCounter = 0; + } + + for (VertexInterface vertex : shortestPath) { + EBox box = (EBox) vertex; + g.fillOval(box.getX() * boxWidth + factorWidth, + box.getY() * boxHeight + factorHeight, + boxWidth - 2 * factorWidth, + boxHeight - 2 * factorHeight); + } +// Timer timer = new Timer(16, new ActionListener(){ +// int compteur = shortestPath.size()-1; +// VertexInterface vertex; +// @Override +// public void actionPerformed(ActionEvent e) { +// if (compteur < 0) { +// ((Timer)e.getSource()).stop(); +// } else { +// vertex = shortestPath.get(compteur); +// EBox box = (EBox) vertex; +// g.fillOval(box.getX() * boxWidth + factorWidth, +// box.getY()*boxHeight + factorHeight, +// boxWidth - 2 * factorWidth, +// boxHeight - 2 * factorHeight); +// compteur -= 1; +// } +// } +// }); +// timer.start(); + + if (pathCounter >= finalPath.size()) { + pathDrawingMode = false; + mazeApp.getMazeAppModel().setPathDrawn(2); + } + } + + public void mouseClicked(MouseEvent e) { + int boxRow = getBoxRow(e.getX()); + int boxLine = getBoxLine(e.getY()); + if (! pathDrawingMode) { + 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); + } + } else { + shortestPath.add(this.finalPath.get(pathCounter)); + repaint(); + pathCounter += 1; + return; + } + return; + } + public void paintShortestPath() { paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); } + public void animateShortestPath() { + animateShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); + } + public void mousePressed(MouseEvent e) { return; } @@ -129,19 +242,6 @@ public class MazeDrawing extends JPanel implements MouseListener { 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; } @@ -153,4 +253,9 @@ public class MazeDrawing extends JPanel implements MouseListener { public void mouseEntered(MouseEvent e) { return; } + + public void clearShortestPath() { + this.pathCounter = -1; + this.shortestPath = new ArrayList(); + } } diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index 91339f1..d6e2bd7 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -11,6 +11,7 @@ public class MazeMenu extends JMenu { private final ClearMazeMenuItem clearMazeMenuItem; private final AddRowMenuItem addRowMenuItem; private final AddLineMenuItem addLineMenuItem; + private final AnimatePathMenuItem animatePathMenuItem; public MazeMenu(MazeApp mazeApp) { @@ -18,6 +19,7 @@ public class MazeMenu extends JMenu { this.mazeApp = mazeApp; add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp)); + add(animatePathMenuItem = new AnimatePathMenuItem(mazeApp)); add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp)); add(newClearMazeMenuItem = new NewClearMazeMenuItem(mazeApp)); add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp)); diff --git a/src/ui/WindowPanel.java b/src/ui/WindowPanel.java index 368c0b2..4219cda 100644 --- a/src/ui/WindowPanel.java +++ b/src/ui/WindowPanel.java @@ -19,7 +19,16 @@ public class WindowPanel extends JPanel { drawingPanelGrid.notifyForUpdates(param); } - public void paintShortestPath() { - drawingPanelGrid.paintShortestPath(); - } + public void paintShortestPath() { + drawingPanelGrid.paintShortestPath(); + } + + public void clearShortestPath() { + drawingPanelGrid.clearShortestPath(); + } + + public void animateShortestPath() { + + drawingPanelGrid.animateShortestPath(); + } } -- 2.56.1 From f3a1f51d10d1a6e38cedc38315c952b7eb309833 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Wed, 3 Jan 2018 10:35:09 +0100 Subject: [PATCH 37/37] Fix shortestPath NullPointerException Fix exception when the departure and the arrival are next to each other. I redefined the path by including the arrival in the path. --- src/dijkstra/Previous.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 15f2280..461263a 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -28,12 +28,14 @@ public class Previous implements PreviousInterface { } public ArrayList getShortestPathTo(VertexInterface vertex) { - + + shortestPath.add(vertex); VertexInterface father = getPrevious(vertex) ; - shortestPath.add(father) ; - if (((Box) getPrevious(father)).isDeparture()) return shortestPath ; - else { + if (((Box) getPrevious(father)) == null) { + shortestPath.add(father); + return shortestPath ; + } else { return getShortestPathTo(father) ; } } -- 2.56.1