Add UI to the project #29
No files matched your search
+2
-2
@@ -5,9 +5,9 @@ package maze;
|
||||
*/
|
||||
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
@@ -6,9 +6,9 @@ package maze;
|
||||
|
||||
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
@@ -7,10 +7,8 @@ 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) ;
|
||||
}
|
||||
|
||||
}
|
||||
+2
-4
@@ -6,11 +6,9 @@ package maze;
|
||||
|
||||
public class WBox extends Box {
|
||||
|
||||
public WBox (String label, int y, int x, Maze maze) {
|
||||
|
||||
|
||||
public WBox (String label, int x, int y, Maze maze) {
|
||||
|
||||
super(label,x,y,maze) ;
|
||||
super(label, y, x, maze) ;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -79,8 +79,8 @@ public class MazeApp extends JFrame
|
||||
}
|
||||
|
||||
public void drawShortestPath() {
|
||||
mazeAppModel.setPathDrawn(true);
|
||||
windowPanel.paintShortestPath();
|
||||
mazeAppModel.setPathDrawn(true);
|
||||
}
|
||||
|
||||
public void clearShortestPath() {
|
||||
|
||||
+51
-2
@@ -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;
|
||||
|
||||
@@ -20,6 +24,7 @@ public class MazeDrawing extends JPanel {
|
||||
this.mazeApp = mazeApp;
|
||||
setBackground(Color.BLACK);
|
||||
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<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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class SaveMazeMenuItem extends JMenuItem
|
||||
this.setEnabled(true);
|
||||
}
|
||||
else {
|
||||
this.setEnabled(true);
|
||||
this.setEnabled(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user