Add UI to the project #29
No files matched your search
@@ -22,7 +22,7 @@ public class Dijkstra {
|
||||
int n = g.getVertexNumber();
|
||||
|
||||
for (int j = 0; j < n; j++) {
|
||||
for (VertexInterface vertex: g.generateNeighbours(pivot)) {
|
||||
for (VertexInterface vertex: pivot.generateNeighbours()) {
|
||||
if (!a.contains(vertex)) {
|
||||
if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
|
||||
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package dijkstra;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This describes a vertex as seen in graphs (see INF101 course).
|
||||
*
|
||||
@@ -11,4 +13,9 @@ public interface VertexInterface {
|
||||
*/
|
||||
public String getLabel() ;
|
||||
|
||||
/**
|
||||
* Updates the attribute of the element.
|
||||
* @return The neighbourList of the element.
|
||||
*/
|
||||
public ArrayList<VertexInterface> generateNeighbours();
|
||||
}
|
||||
+4
-4
@@ -22,7 +22,7 @@ public abstract class Box
|
||||
private int y ;
|
||||
private String label = null ;
|
||||
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
|
||||
|
||||
@@ -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) ;
|
||||
|
||||
}
|
||||
|
||||
public boolean isWalkable() {
|
||||
@@ -50,7 +50,7 @@ public abstract class Box
|
||||
return false ;
|
||||
}
|
||||
|
||||
public ArrayList<Box> getNeighbourList() {
|
||||
public ArrayList<VertexInterface> getNeighbourList() {
|
||||
return neighbourList;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,8 @@ public class MazeAppModel extends Observable {
|
||||
private Previous previous;
|
||||
private ArrayList<VertexInterface> shortest;
|
||||
private ArrayList<VertexInterface> boxList;
|
||||
private boolean saved = true;
|
||||
private boolean saved = true;
|
||||
private boolean pathDrawn = false;
|
||||
|
||||
public MazeAppModel() {
|
||||
maze = new Maze(Maze.emptyMaze(10));
|
||||
@@ -62,6 +63,18 @@ public class MazeAppModel extends Observable {
|
||||
return saved;
|
||||
}
|
||||
|
||||
public ArrayList<VertexInterface> getShortest() {
|
||||
return shortest;
|
||||
}
|
||||
|
||||
public boolean isPathDrawn() {
|
||||
return pathDrawn;
|
||||
}
|
||||
|
||||
public void setPathDrawn(boolean pathDrawn) {
|
||||
this.pathDrawn = pathDrawn;
|
||||
}
|
||||
|
||||
public void saveToFile() {
|
||||
maze.saveToTextFile(filename);
|
||||
saved = true;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package ui;
|
||||
import javax.swing.*;
|
||||
import maze.Maze;
|
||||
import model.MazeAppModel;
|
||||
import maze.EBox;
|
||||
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
@@ -76,4 +77,14 @@ public class MazeApp extends JFrame
|
||||
public void windowOpened(WindowEvent e) {
|
||||
return;
|
||||
}
|
||||
|
||||
public void drawShortestPath() {
|
||||
mazeAppModel.setPathDrawn(true);
|
||||
windowPanel.paintShortestPath();
|
||||
}
|
||||
|
||||
public void clearShortestPath() {
|
||||
mazeAppModel.setPathDrawn(false);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import java.awt.GridLayout;
|
||||
import javax.swing.JPanel;
|
||||
import maze.Maze;
|
||||
import model.MazeAppModelMessage;
|
||||
import maze.EBox;
|
||||
import dijkstra.VertexInterface;
|
||||
|
||||
public class MazeDrawing extends JPanel {
|
||||
|
||||
@@ -30,6 +32,9 @@ public class MazeDrawing extends JPanel {
|
||||
int height = getHeight();
|
||||
|
||||
paintMaze(g2d, width, height);
|
||||
if (mazeApp.getMazeAppModel().isPathDrawn()) {
|
||||
paintShortestPath(g2d, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
private void paintMaze(Graphics2D g, int width, int height) {
|
||||
@@ -70,4 +75,21 @@ public class MazeDrawing extends JPanel {
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ public class MazeMenuBar extends JMenuBar {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
private final FileMenu fileMenu;
|
||||
private final MazeMenu mazeMenu;
|
||||
|
||||
public MazeMenuBar(MazeApp mazeApp) {
|
||||
|
||||
@@ -13,5 +14,6 @@ public class MazeMenuBar extends JMenuBar {
|
||||
this.mazeApp = mazeApp;
|
||||
|
||||
add(fileMenu = new FileMenu(mazeApp));
|
||||
add(mazeMenu = new MazeMenu(mazeApp));
|
||||
}
|
||||
}
|
||||
@@ -19,4 +19,7 @@ public class WindowPanel extends JPanel
|
||||
drawingPanelGrid.notifyForUpdates(param);
|
||||
}
|
||||
|
||||
public void paintShortestPath() {
|
||||
drawingPanelGrid.paintShortestPath();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user