Add UI to the project #29

Manually merged
regar42 merged 45 commits from mvc into master 2018-01-03 11:03:09 +00:00
8 changed files with 97 additions and 21 deletions
Showing only changes of commit 37def85f5b - Show all commits

No files matched your search

+2 -2
View File
@@ -5,9 +5,9 @@ package maze;
*/ */
public class ABox extends EBox{ public class ABox extends EBox{
public ABox (String label, int x, int y, Maze maze) { public ABox (String label, int y, int x, Maze maze) {
super(label,x,y,maze) ; super(label, y, x, maze) ;
} }
+2 -2
View File
@@ -6,9 +6,9 @@ package maze;
public class DBox extends EBox{ public class DBox extends EBox{
public DBox (String label, int x, int y, Maze maze) { public DBox (String label, int y, int x, Maze maze) {
super(label,x,y,maze) ; super(label, y, x, maze) ;
} }
+2 -4
View File
@@ -7,10 +7,8 @@ package maze;
public class EBox extends Box{ public class EBox extends Box{
public EBox (String label, int x, int y, Maze maze) { public EBox (String label, int y, int x, Maze maze) {
super(label,x,y,maze) ;
super(label, y, x, maze) ;
} }
} }
+2 -4
View File
@@ -6,11 +6,9 @@ package maze;
public class WBox extends Box { public class WBox extends Box {
public WBox (String label, int y, int x, Maze maze) {
super(label, y, x, maze) ;
public WBox (String label, int x, int y, Maze maze) {
super(label,x,y,maze) ;
} }
+35 -4
View File
@@ -7,10 +7,8 @@ import java.util.ArrayList;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import dijkstra.VertexInterface; import dijkstra.*;
import dijkstra.Dijkstra; import maze.*;
import dijkstra.Previous;
import maze.Maze;
public class MazeAppModel extends Observable { public class MazeAppModel extends Observable {
@@ -87,4 +85,37 @@ public class MazeAppModel extends Observable {
setChanged(); setChanged();
notifyObservers(MazeAppModelMessage.FileChange); 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);
}
} }
+1 -1
View File
@@ -79,8 +79,8 @@ public class MazeApp extends JFrame
} }
public void drawShortestPath() { public void drawShortestPath() {
mazeAppModel.setPathDrawn(true);
windowPanel.paintShortestPath(); windowPanel.paintShortestPath();
mazeAppModel.setPathDrawn(true);
} }
public void clearShortestPath() { public void clearShortestPath() {
+52 -3
View File
@@ -1,17 +1,21 @@
package ui; package ui;
import java.util.ArrayList;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.GridLayout; import java.awt.GridLayout;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import maze.Maze; import maze.Maze;
import model.MazeAppModelMessage; import model.MazeAppModelMessage;
import maze.EBox; import maze.EBox;
import dijkstra.VertexInterface; import dijkstra.VertexInterface;
public class MazeDrawing extends JPanel { public class MazeDrawing extends JPanel implements MouseListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
@@ -19,7 +23,8 @@ public class MazeDrawing extends JPanel {
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
setBackground(Color.BLACK); setBackground(Color.BLACK);
setPreferredSize(new Dimension(500,500)) ; setPreferredSize(new Dimension(500,500));
addMouseListener(this);
} }
@Override @Override
@@ -83,7 +88,22 @@ public class MazeDrawing extends JPanel {
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY(); int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int boxWidth = width / dimensionX; int boxWidth = width / dimensionX;
int boxHeight = height / dimensionY; int boxHeight = height / dimensionY;
for (VertexInterface vertex : mazeApp.getMazeAppModel().getShortest()) { ArrayList<VertexInterface> shortest =
mazeApp.getMazeAppModel().getShortest();
if (shortest == null && ! mazeApp.getMazeAppModel().isPathDrawn()) {
mazeApp.clearShortestPath();
JOptionPane.showMessageDialog(this,
"Check that your graph contains a departure and an arrival,"
+ " and that a path exists between them.",
"No shortest path",
JOptionPane.ERROR_MESSAGE);
return;
}
else if (shortest == null) {
mazeApp.clearShortestPath();
return;
}
for (VertexInterface vertex : shortest) {
EBox box = (EBox) vertex; EBox box = (EBox) vertex;
g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight); g.fillOval(box.getX()*boxWidth, box.getY()*boxHeight, boxWidth, boxHeight);
} }
@@ -92,4 +112,33 @@ public class MazeDrawing extends JPanel {
public void paintShortestPath() { public void paintShortestPath() {
paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight()); paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight());
} }
public void mousePressed(MouseEvent e) {
return;
}
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int boxWidth = getWidth() / dimensionX;
int boxHeight = getHeight() / dimensionY;
int boxX = x / boxWidth;
int boxY = y / boxHeight;
mazeApp.getMazeAppModel().toggleBox(boxX, boxY);
return;
}
public void mouseReleased(MouseEvent e) {
return;
}
public void mouseExited(MouseEvent e) {
return;
}
public void mouseEntered(MouseEvent e) {
return;
}
} }
+1 -1
View File
@@ -41,7 +41,7 @@ public class SaveMazeMenuItem extends JMenuItem
this.setEnabled(true); this.setEnabled(true);
} }
else { else {
this.setEnabled(true); this.setEnabled(false);
} }
} }
} }