Refactorize code #31

Merged
regar42 merged 5 commits from refactorize into master 2018-01-14 17:02:44 +00:00
12 changed files with 142 additions and 48 deletions
Showing only changes of commit eaba5b6106 - Show all commits

No files matched your search

+20
View File
@@ -296,6 +296,26 @@ public class Maze
dimensionY = dimensionY - 1;
}
public Box getBox(int boxLine, int boxRow) {
return this.maze[boxLine][boxRow];
}
public void setABox(int boxLine, int boxRow) {
this.maze[boxLine][boxRow] = new ABox("A", boxLine, boxRow, this);
}
public void setDBox(int boxLine, int boxRow) {
this.maze[boxLine][boxRow] = new DBox("D", boxLine, boxRow, this);
}
public void setEBox(int boxLine, int boxRow) {
this.maze[boxLine][boxRow] = new EBox("E", boxLine, boxRow, this);
}
public void setWBox(int boxLine, int boxRow) {
this.maze[boxLine][boxRow] = new WBox("W", boxLine, boxRow, this);
}
public int getCost(VertexInterface start, VertexInterface end) {
Box startBox = (Box) start;
+22 -8
View File
@@ -67,6 +67,14 @@ public class MazeAppModel extends Observable {
return maze;
}
public Box[][] getBoxMaze() {
return this.maze.getMaze();
}
public Box getBox(int boxLine, int boxRow) {
return this.maze.getBox(boxLine, boxRow);
}
public String getFilename() {
return filename;
}
@@ -118,14 +126,14 @@ public class MazeAppModel extends Observable {
}
public void setWBox(int boxLine, int boxRow) {
maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze);
maze.setWBox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
}
public void setEBox(int boxLine, int boxRow) {
maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze);
maze.setEBox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
@@ -135,10 +143,9 @@ public class MazeAppModel extends Observable {
if (departure != null) {
int departureRow = ((EBox) departure).getX();
int departureLine = ((EBox) departure).getY();
maze.getMaze()[departureLine][departureRow] =
new EBox("E", departureLine, departureRow, maze);
maze.setEBox(departureLine, departureRow);
}
maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze);
maze.setDBox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
@@ -148,10 +155,9 @@ public class MazeAppModel extends Observable {
if (arrival != null) {
int arrivalRow = ((EBox) arrival).getX();
int arrivalLine = ((EBox) arrival).getY();
maze.getMaze()[arrivalLine][arrivalRow] =
new EBox("E", arrivalLine, arrivalRow, maze);
maze.setEBox(arrivalLine, arrivalRow);
}
maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze);
maze.setABox(boxLine, boxRow);
maze.resetNeighbourLists();
initModelFromMaze();
saved = false;
@@ -211,4 +217,12 @@ public class MazeAppModel extends Observable {
public void clearMaze() {
newClearMaze(maze.getDimensionY(), maze.getDimensionX());
}
public int getDimensionX() {
return this.maze.getDimensionX();
}
public int getDimensionY() {
return this.maze.getDimensionX();
}
}
+3 -1
View File
@@ -5,6 +5,7 @@ import javax.swing.JMenuItem;
import maze.Box;
public class BoxTypeEditionMenu extends JPopupMenu {
JMenuItem setWBox;
JMenuItem setEBox;
JMenuItem setDBox;
@@ -15,11 +16,12 @@ public class BoxTypeEditionMenu extends JPopupMenu {
JMenuItem delLine;
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];
Box box = (Box) mazeApp.getModelBox(boxLine, boxRow);
setEBox.setEnabled((box.isWalkable() && (box.isDeparture() ||
box.isArrival())) || ! box.isWalkable());
setWBox.setEnabled(box.isWalkable());
+2 -3
View File
@@ -22,15 +22,14 @@ public class LoadMazeMenuItem extends JMenuItem
public void actionPerformed(ActionEvent evt) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
if (! mazeAppModel.isSaved()) {
if (mazeApp.isModelSaved()) {
if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){
return;
}
}
String filename = FileBox.load(mazeApp);
if (filename != null) {
mazeApp.getMazeAppModel().loadMaze(filename);
mazeApp.loadModelMaze(filename);
}
}
}
+65 -4
View File
@@ -1,10 +1,13 @@
package ui;
import javax.swing.*;
import dijkstra.VertexInterface;
import maze.Maze;
import model.MazeAppModel;
import maze.EBox;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.awt.event.WindowEvent;
@@ -40,7 +43,6 @@ public class MazeApp extends JFrame
@Override
public void update(Observable obs, Object param) {
System.out.println("New maze detected");
windowPanel.notifyForUpdates(param);
}
@@ -83,6 +85,11 @@ public class MazeApp extends JFrame
mazeAppModel.setPathDrawn(2);
}
public void animateShortestPath() {
windowPanel.animateShortestPath();
mazeAppModel.setPathDrawn(1);
}
public void clearShortestPath() {
windowPanel.clearShortestPath();
mazeAppModel.setPathDrawn(0);
@@ -126,9 +133,63 @@ public class MazeApp extends JFrame
this.repaint();
}
public void animateShortestPath() {
public boolean isModelSaved( ) {
return this.mazeAppModel.isSaved();
}
windowPanel.animateShortestPath();
mazeAppModel.setPathDrawn(1);
public void loadModelMaze(String filename) {
this.mazeAppModel.loadMaze(filename);
}
public maze.Box getModelBox(int boxLine, int boxRow) {
return this.mazeAppModel.getBox(boxLine, boxRow);
}
public void setModelABox(int boxLine, int boxRow) {
this.mazeAppModel.setABox(boxLine, boxRow);
}
public void setModelDBox(int boxLine, int boxRow) {
this.mazeAppModel.setDBox(boxLine, boxRow);
}
public void setModelEBox(int boxLine, int boxRow) {
this.mazeAppModel.setEBox(boxLine, boxRow);
}
public void setModelWBox(int boxLine, int boxRow) {
this.mazeAppModel.setWBox(boxLine, boxRow);
}
public int isPathDrawn() {
return this.mazeAppModel.isPathDrawn();
}
public Maze getModelMaze() {
return this.mazeAppModel.getMaze();
}
public ArrayList<VertexInterface> getModelShortest() {
return this.mazeAppModel.getShortest();
}
public ArrayList<VertexInterface> getModelShortestDeepCopy() {
return this.mazeAppModel.getShortestDeepCopy();
}
public int getModelDimensionX() {
return this.mazeAppModel.getDimensionX();
}
public int getModelDimensionY() {
return this.mazeAppModel.getDimensionY();
}
public void setPathDrawn(int i) {
this.mazeAppModel.setPathDrawn(i);
}
public void toggleModelBox(int boxLine, int boxRow) {
this.mazeAppModel.toggleBox(boxLine, boxRow);
}
}
+20 -19
View File
@@ -48,15 +48,16 @@ public class MazeDrawing extends JPanel implements MouseListener {
int height = getHeight();
paintMaze(g2d, width, height);
if (mazeApp.getMazeAppModel().isPathDrawn() == 1) {
if (mazeApp.isPathDrawn() == 1) {
animateShortestPath(g2d, width, height);
} else if (mazeApp.getMazeAppModel().isPathDrawn() == 2) {
} else if (mazeApp.isPathDrawn() == 2) {
paintShortestPath(g2d, width, height);
}
}
private void paintMaze(Graphics2D g, int width, int height) {
Maze maze = mazeApp.getMazeAppModel().getMaze();
Maze maze = mazeApp.getModelMaze();
int dimensionX = maze.getDimensionX();
int dimensionY = maze.getDimensionY();
int boxWidth = width / dimensionX;
@@ -64,11 +65,11 @@ public class MazeDrawing extends JPanel implements MouseListener {
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()) {
if (maze.getBox(line, row).isWalkable()) {
if (maze.getBox(line, row).isDeparture()) {
g.setColor(Color.GREEN);
}
else if (maze.getMaze()[line][row].isArrival()) {
else if (maze.getBox(line, row).isArrival()) {
g.setColor(Color.RED);
}
else {
@@ -90,9 +91,9 @@ public class MazeDrawing extends JPanel implements MouseListener {
public void notifyForUpdates(Object param) {
if (param == MazeAppModelMessage.MazeRenewal) {
if (this.mazeApp.getMazeAppModel().getShortest() != null) {
if (this.mazeApp.getModelShortest() != null) {
this.finalPath
= this.mazeApp.getMazeAppModel().getShortestDeepCopy();
= this.mazeApp.getModelShortestDeepCopy();
Collections.reverse(finalPath);
}
this.repaint();
@@ -102,15 +103,15 @@ public class MazeDrawing extends JPanel implements MouseListener {
private void paintShortestPath(Graphics2D g, int width, int height) {
g.setColor(Color.WHITE);
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int dimensionX = mazeApp.getModelDimensionX();
int dimensionY = mazeApp.getModelDimensionY();
int boxWidth = width / dimensionX;
int boxHeight = height / dimensionY;
int factorWidth = boxWidth / 3;
int factorHeight = boxHeight / 3;
ArrayList<VertexInterface> shortest =
mazeApp.getMazeAppModel().getShortest();
if (shortest == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) {
mazeApp.getModelShortest();
if (shortest == null && mazeApp.isPathDrawn() == 0) {
mazeApp.clearShortestPath();
JOptionPane.showMessageDialog(this,
"Check that your graph contains a departure and an arrival,"
@@ -135,14 +136,14 @@ public class MazeDrawing extends JPanel implements MouseListener {
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 dimensionX = mazeApp.getModelDimensionX();
int dimensionY = mazeApp.getModelDimensionY();
int boxWidth = width / dimensionX;
int boxHeight = height / dimensionY;
int factorWidth = boxWidth / 3;
int factorHeight = boxHeight / 3;
if (this.finalPath == null && mazeApp.getMazeAppModel().isPathDrawn() == 0) {
if (this.finalPath == null && mazeApp.isPathDrawn() == 0) {
mazeApp.clearShortestPath();
JOptionPane.showMessageDialog(this,
"Check that your graph contains a departure and an arrival,"
@@ -193,7 +194,7 @@ public class MazeDrawing extends JPanel implements MouseListener {
if (pathCounter >= finalPath.size()) {
pathDrawingMode = false;
mazeApp.getMazeAppModel().setPathDrawn(2);
mazeApp.setPathDrawn(2);
}
}
@@ -207,7 +208,7 @@ public class MazeDrawing extends JPanel implements MouseListener {
boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY());
}
else if (e.getButton() == MouseEvent.BUTTON1) {
mazeApp.getMazeAppModel().toggleBox(boxLine, boxRow);
mazeApp.toggleModelBox(boxLine, boxRow);
}
} else {
shortestPath.add(this.finalPath.get(pathCounter));
@@ -231,13 +232,13 @@ public class MazeDrawing extends JPanel implements MouseListener {
}
private int getBoxRow(int x) {
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
int dimensionX = mazeApp.getModelDimensionX();
int boxWidth = getWidth() / dimensionX;
return x / boxWidth;
}
private int getBoxLine(int y) {
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int dimensionY = mazeApp.getModelDimensionY();
int boxHeight = getHeight() / dimensionY;
return y / boxHeight;
}
-1
View File
@@ -24,7 +24,6 @@ public class NewClearMazeMenuItem extends JMenuItem
@Override
public void actionPerformed(ActionEvent arg0) {
spinnerBox.createAndShowGUI();
}
}
+6 -8
View File
@@ -24,13 +24,11 @@ public class QuitMenuItem extends JMenuItem
public void actionPerformed(ActionEvent evt) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
if (! mazeAppModel.isSaved()) {
if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) {
return;
}
}
System.exit(0);
if (! mazeApp.isModelSaved()) {
if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) {
return;
}
}
System.exit(0);
}
}
+1 -1
View File
@@ -21,6 +21,6 @@ public class SetABoxMenuItem extends JMenuItem
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setABox(boxLine, boxRow);
mazeApp.setModelABox(boxLine, boxRow);
}
}
+1 -1
View File
@@ -21,6 +21,6 @@ public class SetDBoxMenuItem extends JMenuItem
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setDBox(boxLine, boxRow);
mazeApp.setModelDBox(boxLine, boxRow);
}
}
+1 -1
View File
@@ -21,6 +21,6 @@ public class SetEBoxMenuItem extends JMenuItem
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setEBox(boxLine, boxRow);
mazeApp.setModelEBox(boxLine, boxRow);
}
}
+1 -1
View File
@@ -21,6 +21,6 @@ public class SetWBoxMenuItem extends JMenuItem
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setWBox(boxLine, boxRow);
mazeApp.setModelWBox(boxLine, boxRow);
}
}