Merge branch 'boxTypeEdition' into mvc and cleanup

* Merge branch 'boxTypeEdition' into mvc
* Remove some useless spaces and blank lines
This commit was merged in pull request #18.
This commit is contained in:
regar42 committed 2017-12-16 20:40:27 +01:00
commit 79ed877d9d
12 files changed
+158 -13

No files matched your search

+28
View File
@@ -59,6 +59,34 @@ public class Maze
return vertexList ; return vertexList ;
} }
/**Method to determine the departure in the maze. Returns null if the departure is non-existent.
* @return Departure vertex
*/
public VertexInterface getDeparture() {
ArrayList<VertexInterface> vertexes = getVertexes();
for (VertexInterface vertex: vertexes) {
if (((Box) vertex).isDeparture()) {
return vertex;
}
}
return null;
}
/**Method to determine the arrival in the maze. Returns null if the arrival is non-existent.
* @return Arrival vertex
*/
public VertexInterface getArrival() {
ArrayList<VertexInterface> vertexes = getVertexes();
for (VertexInterface vertex: vertexes) {
if (((Box) vertex).isArrival()) {
return vertex;
}
}
return null;
}
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) { public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) {
Box box = (Box)vertex ; Box box = (Box)vertex ;
+10
View File
@@ -0,0 +1,10 @@
package model;
import maze.Maze;
public class Box extends maze.Box {
public Box(String label, int x, int y, Maze maze) {
super(label, x, y, maze);
}
}
+11 -2
View File
@@ -1,11 +1,20 @@
package model; package model;
import java.util.ArrayList;
import java.util.Observable; import java.util.Observable;
import dijkstra.VertexInterface;
import dijkstra.Dijkstra;
public class MazeAppModel extends Observable { public class MazeAppModel extends Observable {
private Maze maze = new Maze("data/labyrinthe.txt"); private Maze maze = new Maze("data/labyrinthe.txt");
private boolean modified = false; private VertexInterface departure = maze.getDeparture();
private VertexInterface arrival = maze.getArrival();
private Previous previous = (Previous) Dijkstra.dijkstra(maze, departure);
private ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
private ArrayList<VertexInterface> boxList = maze.getVertexes();
private Boolean modified = false;
public void setMaze(Maze maze) { public void setMaze(Maze maze) {
this.maze = maze; this.maze = maze;
+13
View File
@@ -0,0 +1,13 @@
package model;
import java.util.ArrayList;
import java.util.Hashtable;
import dijkstra.VertexInterface;
public class Previous extends dijkstra.Previous {
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) {
super(hashPrevious, shortestPath);
}
}
+8
View File
@@ -1,10 +1,18 @@
package ui; package ui;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics;
public class ArrivalDrawingBox extends DrawingBox { public class ArrivalDrawingBox extends DrawingBox {
public ArrivalDrawingBox(MazeApp mazeApp, Color color) { public ArrivalDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color); super(mazeApp, color);
} }
@Override
protected final void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.RED);
}
} }
+8
View File
@@ -1,10 +1,18 @@
package ui; package ui;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics;
public class DepartureDrawingBox extends DrawingBox { public class DepartureDrawingBox extends DrawingBox {
public DepartureDrawingBox(MazeApp mazeApp, Color color) { public DepartureDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color); super(mazeApp, color);
} }
@Override
protected final void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.GREEN);
}
} }
+12
View File
@@ -2,6 +2,8 @@ package ui;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
@@ -18,7 +20,17 @@ public abstract class DrawingBox extends JPanel {
setBackground(color); setBackground(color);
setPreferredSize(new Dimension(40,40)); setPreferredSize(new Dimension(40,40));
setBorder(BorderFactory.createLineBorder(Color.orange)); setBorder(BorderFactory.createLineBorder(Color.orange));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(color);
}
public void notifyForUpdates(Object param) {
repaint();
} }
} }
+34 -9
View File
@@ -8,17 +8,19 @@ import maze.Maze;
public class DrawingPanelGrid extends JPanel { public class DrawingPanelGrid extends JPanel {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private Maze maze; private Maze maze;
private int dimensionX; private DrawingBox[][] drawingBoxMatrix;
private int dimensionY; private int dimensionX;
private int dimensionY;
public DrawingPanelGrid(MazeApp mazeApp) { public DrawingPanelGrid(MazeApp mazeApp) {
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
this.maze = mazeApp.getMazeAppModel().getMaze(); this.maze = mazeApp.getMazeAppModel().getMaze();
this.dimensionX = maze.getDimensionX(); this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY(); this.dimensionY = maze.getDimensionY();
this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY];
setLayout(new GridLayout(this.dimensionX, this.dimensionY)); setLayout(new GridLayout(this.dimensionX, this.dimensionY));
initFromMaze(maze); initFromMaze(maze);
@@ -28,24 +30,47 @@ public class DrawingPanelGrid extends JPanel {
private void initFromMaze(Maze maze) { private void initFromMaze(Maze maze) {
int dimensionX = maze.getDimensionX();
int dimensionY = maze.getDimensionY();
for (int i = 0; i < dimensionX; i++) { for (int i = 0; i < dimensionX; i++) {
for (int j = 0; j < dimensionY; j++) { for (int j = 0; j < dimensionY; j++) {
if (maze.getMaze()[j][i].isWalkable()) { if (maze.getMaze()[j][i].isWalkable()) {
if (maze.getMaze()[j][i].isDeparture()) { if (maze.getMaze()[j][i].isDeparture()) {
add(new DepartureDrawingBox(mazeApp, Color.GREEN)); add(new DepartureDrawingBox(mazeApp, Color.GREEN));
drawingBoxMatrix[j][i] = new DepartureDrawingBox(mazeApp, Color.GREEN);
} }
else if (maze.getMaze()[j][i].isArrival()) { else if (maze.getMaze()[j][i].isArrival()) {
add(new ArrivalDrawingBox(mazeApp, Color.RED)); add(new ArrivalDrawingBox(mazeApp, Color.RED));
drawingBoxMatrix[j][i] = new ArrivalDrawingBox(mazeApp, Color.RED);
} }
else { else {
add(new EmptyDrawingBox(mazeApp, Color.BLACK)); add(new EmptyDrawingBox(mazeApp, Color.BLACK));
drawingBoxMatrix[j][i] = new EmptyDrawingBox(mazeApp, Color.BLACK);
} }
} }
else { else {
add(new WallDrawingBox(mazeApp, Color.WHITE)); add(new WallDrawingBox(mazeApp, Color.WHITE));
drawingBoxMatrix[j][i] = new WallDrawingBox(mazeApp, Color.WHITE);
}
}
}
}
public void notifyForUpdates(Object param) {
for (int i = 0; i < dimensionX; i++) {
for (int j = 0; j < dimensionY; j++) {
if (maze.getMaze()[j][i].isWalkable()) {
if (maze.getMaze()[j][i].isDeparture()) {
drawingBoxMatrix[j][i].notifyForUpdates();
}
else if (maze.getMaze()[j][i].isArrival()) {
drawingBoxMatrix[j][i].notifyForUpdates();
}
else {
drawingBoxMatrix[j][i].notifyForUpdates();
}
}
else {
drawingBoxMatrix[j][i].notifyForUpdates();
} }
} }
} }
+8
View File
@@ -1,10 +1,18 @@
package ui; package ui;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics;
public class EmptyDrawingBox extends DrawingBox { public class EmptyDrawingBox extends DrawingBox {
public EmptyDrawingBox(MazeApp mazeApp, Color color) { public EmptyDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color); super(mazeApp, color);
} }
@Override
protected final void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
}
} }
+12 -2
View File
@@ -4,7 +4,11 @@ import javax.swing.*;
import maze.Maze; import maze.Maze;
import model.MazeAppModel; import model.MazeAppModel;
public class MazeApp extends JFrame { import java.util.Observable;
import java.util.Observer;
public class MazeApp extends JFrame
implements Observer {
private final MazeMenuBar mazeMenuBar; private final MazeMenuBar mazeMenuBar;
private final WindowPanel windowPanel; private final WindowPanel windowPanel;
@@ -15,7 +19,7 @@ public class MazeApp extends JFrame {
super("Maze Solving Application"); super("Maze Solving Application");
setJMenuBar(mazeMenuBar = new MazeMenuBar(this)); setJMenuBar(mazeMenuBar = new MazeMenuBar(this));
setContentPane(windowPanel = new WindowPanel(this)) ; setContentPane(windowPanel = new WindowPanel(this));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -26,4 +30,10 @@ public class MazeApp extends JFrame {
public MazeAppModel getMazeAppModel() { public MazeAppModel getMazeAppModel() {
return mazeAppModel; return mazeAppModel;
} }
@Override
public void update(Observable obs, Object param) {
windowPanel.notifyForUpdates(param);
}
} }
+8
View File
@@ -1,10 +1,18 @@
package ui; package ui;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics;
public class WallDrawingBox extends DrawingBox { public class WallDrawingBox extends DrawingBox {
public WallDrawingBox(MazeApp mazeApp, Color color) { public WallDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color); super(mazeApp, color);
} }
@Override
protected final void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.BLACK);
}
} }
+6
View File
@@ -14,4 +14,10 @@ public class WindowPanel extends JPanel
add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER);
} }
public void notifyForUpdates(Object param) {
drawingPanelGrid.notifyForUpdates(param);
}
} }