Refactorize code

This commit is contained in:
antograb committed 2018-01-10 18:32:38 +01:00
1 parent 3520bfc4ce
commit eaba5b6106
12 files changed
+137 -43

No files matched your search

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