Add UI to the project #29

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

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@ public class Dijkstra {
int n = g.getVertexNumber(); int n = g.getVertexNumber();
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
for (VertexInterface vertex: g.generateNeighbours(pivot)) { for (VertexInterface vertex: pivot.generateNeighbours()) {
if (!a.contains(vertex)) { if (!a.contains(vertex)) {
if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) { if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ;
+8 -1
View File
@@ -1,5 +1,7 @@
package dijkstra; package dijkstra;
import java.util.ArrayList;
/** /**
* This describes a vertex as seen in graphs (see INF101 course). * This describes a vertex as seen in graphs (see INF101 course).
* *
@@ -10,5 +12,10 @@ public interface VertexInterface {
* @return The label of the element. * @return The label of the element.
*/ */
public String getLabel() ; public String getLabel() ;
/**
* Updates the attribute of the element.
* @return The neighbourList of the element.
*/
public ArrayList<VertexInterface> generateNeighbours();
} }
+4 -4
View File
@@ -22,7 +22,7 @@ public abstract class Box
private int y ; private int y ;
private String label = null ; private String label = null ;
private Maze maze ; private Maze maze ;
private ArrayList<Box> neighbourList = new ArrayList<Box>() ; private ArrayList<VertexInterface> neighbourList;
public Box (String label, int y , int x, Maze maze) { //y first because of graphical maze representation 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<VertexInterface> generateNeighbors() { public ArrayList<VertexInterface> generateNeighbours() {
this.neighbourList = maze.generateNeighbours(this);
return maze.generateNeighbours(this) ; return maze.generateNeighbours(this) ;
} }
public boolean isWalkable() { public boolean isWalkable() {
@@ -50,7 +50,7 @@ public abstract class Box
return false ; return false ;
} }
public ArrayList<Box> getNeighbourList() { public ArrayList<VertexInterface> getNeighbourList() {
return neighbourList; return neighbourList;
} }
+1 -1
View File
@@ -209,7 +209,7 @@ public class Maze
return neighbourList ; return neighbourList ;
} }
/**Constructs the maze from a text file, according to each single character on each line. /**Constructs the maze from a text file, according to each single character on each line.
* @param filename * @param filename
* The path to the file containing the representation of the maze. * The path to the file containing the representation of the maze.
+14 -1
View File
@@ -21,7 +21,8 @@ public class MazeAppModel extends Observable {
private Previous previous; private Previous previous;
private ArrayList<VertexInterface> shortest; private ArrayList<VertexInterface> shortest;
private ArrayList<VertexInterface> boxList; private ArrayList<VertexInterface> boxList;
private boolean saved = true; private boolean saved = true;
private boolean pathDrawn = false;
public MazeAppModel() { public MazeAppModel() {
maze = new Maze(Maze.emptyMaze(10)); maze = new Maze(Maze.emptyMaze(10));
@@ -62,6 +63,18 @@ public class MazeAppModel extends Observable {
return saved; return saved;
} }
public ArrayList<VertexInterface> getShortest() {
return shortest;
}
public boolean isPathDrawn() {
return pathDrawn;
}
public void setPathDrawn(boolean pathDrawn) {
this.pathDrawn = pathDrawn;
}
public void saveToFile() { public void saveToFile() {
maze.saveToTextFile(filename); maze.saveToTextFile(filename);
saved = true; saved = true;
+25
View File
@@ -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();
}
}
+25
View File
@@ -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();
}
}
+11
View File
@@ -3,6 +3,7 @@ package ui;
import javax.swing.*; import javax.swing.*;
import maze.Maze; import maze.Maze;
import model.MazeAppModel; import model.MazeAppModel;
import maze.EBox;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
@@ -76,4 +77,14 @@ public class MazeApp extends JFrame
public void windowOpened(WindowEvent e) { public void windowOpened(WindowEvent e) {
return; return;
} }
public void drawShortestPath() {
mazeAppModel.setPathDrawn(true);
windowPanel.paintShortestPath();
}
public void clearShortestPath() {
mazeAppModel.setPathDrawn(false);
this.repaint();
}
} }
+22
View File
@@ -8,6 +8,8 @@ import java.awt.GridLayout;
import javax.swing.JPanel; import javax.swing.JPanel;
import maze.Maze; import maze.Maze;
import model.MazeAppModelMessage; import model.MazeAppModelMessage;
import maze.EBox;
import dijkstra.VertexInterface;
public class MazeDrawing extends JPanel { public class MazeDrawing extends JPanel {
@@ -30,6 +32,9 @@ public class MazeDrawing extends JPanel {
int height = getHeight(); int height = getHeight();
paintMaze(g2d, width, height); paintMaze(g2d, width, height);
if (mazeApp.getMazeAppModel().isPathDrawn()) {
paintShortestPath(g2d, width, height);
}
} }
private void paintMaze(Graphics2D g, int width, int height) { private void paintMaze(Graphics2D g, int width, int height) {
@@ -70,4 +75,21 @@ public class MazeDrawing extends JPanel {
this.repaint(); this.repaint();
} }
} }
private 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);
}
}
public void paintShortestPath() {
paintShortestPath((Graphics2D) getGraphics(), getWidth(), getHeight());
}
} }
+19
View File
@@ -0,0 +1,19 @@
package ui;
import javax.swing.JMenu;
public class MazeMenu extends JMenu {
private final MazeApp mazeApp;
private final DrawShortestPathMenuItem drawMenuItem;
private final ClearShortestPathMenuItem clearMenuItem;
public MazeMenu(MazeApp mazeApp) {
super("Maze");
this.mazeApp = mazeApp;
add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp));
add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp));
}
}
+2
View File
@@ -6,6 +6,7 @@ public class MazeMenuBar extends JMenuBar {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final FileMenu fileMenu; private final FileMenu fileMenu;
private final MazeMenu mazeMenu;
public MazeMenuBar(MazeApp mazeApp) { public MazeMenuBar(MazeApp mazeApp) {
@@ -13,5 +14,6 @@ public class MazeMenuBar extends JMenuBar {
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
add(fileMenu = new FileMenu(mazeApp)); add(fileMenu = new FileMenu(mazeApp));
add(mazeMenu = new MazeMenu(mazeApp));
} }
} }
+3
View File
@@ -19,4 +19,7 @@ public class WindowPanel extends JPanel
drawingPanelGrid.notifyForUpdates(param); drawingPanelGrid.notifyForUpdates(param);
} }
public void paintShortestPath() {
drawingPanelGrid.paintShortestPath();
}
} }