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 403 additions and 135 deletions
Showing only changes of commit 024a4f70c3 - Show all commits

No files matched your search

-22
View File
@@ -1,30 +1,8 @@
import java.util.ArrayList;
import dijkstra.*;
import maze.*;
import ui.*; import ui.*;
public class MainTest { public class MainTest {
public static void main(String[] args) { public static void main(String[] args) {
Maze mazeTest = new Maze("data/labyrinthe.txt") ;
System.out.println(mazeTest);
mazeTest.saveToTextFile("data/labyOutput.txt");
VertexInterface departure = mazeTest.getDeparture();
VertexInterface arrival = mazeTest.getArrival();
Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, departure);
ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
//Print shortest path to arrival
System.out.println(arrival);
for (VertexInterface vertex : shortest) {
System.out.println(vertex);
}
MazeApp app = new MazeApp(); MazeApp app = new MazeApp();
} }
} }
+194 -26
View File
@@ -43,51 +43,89 @@ public class Maze
} }
public Maze(Box[][] newMaze) { public Maze(Box[][] newMaze) {
this.dimensionX = newMaze[0].length;
this.dimensionY = newMaze.length; this.dimensionY = newMaze.length;
if (dimensionY > 0) {
this.dimensionX = newMaze[0].length;
}
else {
this.dimensionX = 0;
}
this.maze = matrixDeepCopy(newMaze); this.maze = matrixDeepCopy(newMaze);
} }
private Box[][] matrixDeepCopy (Box[][] original) { private Box[][] matrixDeepCopy(Box[][] original,
int numberOfLines = 0; Box[][] destination,
int numberOfRows = 0; int numberOfLines,
Box[][] copy; int numberOfRows,
/* int lineOffset,
* We first get the dimensions of the matrix int rowOffset)
*/ {
for (Box[] lineBoxes: original) { if (destination == null) {
numberOfLines++; destination = new Box[numberOfLines][numberOfRows];
} }
if (numberOfLines > 0) { if (original.length < 1) {
for (Box box: original[0]) { return destination;
numberOfRows++;
}
} }
copy = new Box[numberOfLines][numberOfRows]; for (int line = lineOffset; line < lineOffset + numberOfLines; line++) {
for (int row = rowOffset; row < rowOffset + numberOfRows; row++) {
/*
* Now we proceed to the deep copy of each box
*/
for (int line = 0; line < numberOfLines; line++) {
for (int row = 0; row < numberOfRows; row++) {
Box box = original[line][row]; Box box = original[line][row];
if (box.isWalkable()) { if (box.isWalkable()) {
if (box.isDeparture()) { if (box.isDeparture()) {
copy[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); destination[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
} }
else if (box.isArrival()) { else if (box.isArrival()) {
copy[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); destination[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
} }
else { else {
copy[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); destination[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
} }
} }
else { else {
copy[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); destination[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
} }
} }
} }
return copy; return destination;
}
private Box[][] matrixDeepCopy(Box[][] original,
int numberOfLines,
int numberOfRows,
int lineOffset,
int rowOffset,
int numberOfDestLines,
int numberOfDestRows) {
return matrixDeepCopy(original,
new Box[numberOfDestLines][numberOfDestRows],
numberOfLines,
numberOfRows,
lineOffset,
rowOffset);
}
private Box[][] matrixDeepCopy(Box [][] original,
int numberOfLines,
int numberOfRows,
int lineOffset,
int rowOffset) {
return matrixDeepCopy(original,
numberOfLines,
numberOfRows,
lineOffset,
rowOffset,
numberOfLines,
numberOfRows);
}
private Box[][] matrixDeepCopy(Box[][] original, int numberOfLines, int numberOfRows) {
return matrixDeepCopy(original, numberOfLines, numberOfRows, 0, 0);
}
private Box[][] matrixDeepCopy(Box[][] original) {
if (original.length < 1) {
return null;
}
return matrixDeepCopy(original, original.length, original[0].length);
} }
public static Maze emptyMaze(int dimension) { public static Maze emptyMaze(int dimension) {
@@ -130,6 +168,136 @@ public class Maze
return new Maze(numberOfLines, numberOfRows, maze); return new Maze(numberOfLines, numberOfRows, maze);
} }
public void addEmptyColumn(int posColumn) {
Box[][] newMaze = matrixDeepCopy(maze,
dimensionY,
posColumn,
0,
0,
dimensionY,
dimensionX + 1);
for (int line = 0; line < dimensionY; line++) {
newMaze[line][posColumn] = new EBox("E", line, posColumn, this);
}
for (int line = 0; line < dimensionY; line++) {
for (int row = posColumn; row < dimensionX; row++) {
Box box = maze[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
newMaze[line][row + 1] = new DBox(box.getLabel(), box.getY(), box.getX() + 1, this);
}
else if (box.isArrival()) {
newMaze[line][row + 1] = new ABox(box.getLabel(), box.getY(), box.getX() + 1, this);
}
else {
newMaze[line][row + 1] = new EBox(box.getLabel(), box.getY(), box.getX() + 1, this);
}
}
else {
newMaze[line][row + 1] = new WBox(box.getLabel(), box.getY(), box.getX() + 1, this);
}
}
}
maze = newMaze;
dimensionX = dimensionX + 1;
}
public void delColumn(int posColumn) {
Box[][] newMaze = matrixDeepCopy(maze,
dimensionY,
posColumn,
0,
0,
dimensionY,
dimensionX - 1);
for (int line = 0; line < dimensionY; line++) {
for (int row = posColumn + 1; row < dimensionX; row++) {
Box box = maze[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
newMaze[line][row - 1] = new DBox(box.getLabel(), box.getY(), box.getX() - 1, this);
}
else if (box.isArrival()) {
newMaze[line][row - 1] = new ABox(box.getLabel(), box.getY(), box.getX() - 1, this);
}
else {
newMaze[line][row - 1] = new EBox(box.getLabel(), box.getY(), box.getX() - 1, this);
}
}
else {
newMaze[line][row - 1] = new WBox(box.getLabel(), box.getY(), box.getX() - 1, this);
}
}
}
maze = newMaze;
dimensionX = dimensionX - 1;
}
public void addEmptyLine(int posLine) {
Box[][] newMaze = matrixDeepCopy(maze,
posLine,
dimensionX,
0,
0,
dimensionY + 1,
dimensionX);
for (int row = 0; row < dimensionX; row++) {
newMaze[posLine][row] = new EBox("E", posLine, row, this);
}
for (int line = posLine; line < dimensionY; line++) {
for (int row = 0; row < dimensionX; row++) {
Box box = maze[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
newMaze[line + 1][row] = new DBox(box.getLabel(), box.getY() + 1, box.getX(), this);
}
else if (box.isArrival()) {
newMaze[line + 1][row] = new ABox(box.getLabel(), box.getY() + 1, box.getX(), this);
}
else {
newMaze[line + 1][row] = new EBox(box.getLabel(), box.getY() + 1, box.getX(), this);
}
}
else {
newMaze[line + 1][row] = new WBox(box.getLabel(), box.getY() + 1, box.getX(), this);
}
}
}
dimensionY = dimensionY + 1;
maze = newMaze;
}
public void delLine(int posLine) {
Box[][] newMaze = matrixDeepCopy(maze,
posLine,
dimensionX,
0,
0,
dimensionY - 1,
dimensionX);
for (int line = posLine + 1; line < dimensionY; line++) {
for (int row = 0; row < dimensionX; row++) {
Box box = maze[line][row];
if (box.isWalkable()) {
if (box.isDeparture()) {
newMaze[line - 1][row] = new DBox(box.getLabel(), box.getY() - 1, box.getX(), this);
}
else if (box.isArrival()) {
newMaze[line - 1][row] = new ABox(box.getLabel(), box.getY() - 1, box.getX(), this);
}
else {
newMaze[line - 1][row] = new EBox(box.getLabel(), box.getY() - 1, box.getX(), this);
}
}
else {
newMaze[line - 1][row] = new WBox(box.getLabel(), box.getY() - 1, box.getX(), this);
}
}
}
maze = newMaze;
dimensionY = dimensionY - 1;
}
public int getCost(VertexInterface start, VertexInterface end) { public int getCost(VertexInterface start, VertexInterface end) {
Box startBox = (Box) start ; Box startBox = (Box) start ;
+33 -40
View File
@@ -134,59 +134,52 @@ public class MazeAppModel extends Observable {
public void newClearMaze(int numberOfLines, int numberOfRows) { public void newClearMaze(int numberOfLines, int numberOfRows) {
Box[][] newClearMaze = new Box[numberOfLines][numberOfRows]; maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows));
for (int line = 0; line < numberOfLines; line++) {
for (int row = 0; row < numberOfRows; row++) {
newClearMaze[line][row] = new EBox("E", line, row, this.maze);
}
}
this.maze = new Maze(newClearMaze);
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
} }
public void addEmptyColumn() { public void addEmptyColumn() {
addEmptyColumn(maze.getDimensionX());
}
Box[][] previousMaze = maze.getMaze(); public void addEmptyColumn(int posColumn) {
int numberOfLines = maze.getDimensionY(); maze.addEmptyColumn(posColumn);
int numberOfRows = maze.getDimensionX()+1;
newClearMaze(numberOfLines, numberOfRows);
for (int line = 0; line < numberOfLines; line++) {
for (int row = 0; row < numberOfRows; row++) {
if (row == numberOfRows-1) {
maze.getMaze()[line][row] = new EBox("E", line, row, maze);
} else {
maze.getMaze()[line][row] = previousMaze[line][row];
}
}
}
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
} }
public void delColumn(int posColumn) {
maze.delColumn(posColumn);
initModelFromMaze();
saved = false;
}
public void delColumn() {
delColumn(maze.getDimensionX() - 1);
}
public void addEmptyLine() { public void addEmptyLine() {
addEmptyLine(maze.getDimensionY());
}
Box[][] previousMaze = maze.getMaze(); public void addEmptyLine(int posLine) {
int numberOfLines = maze.getDimensionY()+1; maze.addEmptyLine(posLine);
int numberOfRows = maze.getDimensionX();
newClearMaze(numberOfLines, numberOfRows);
for (int line = 0; line < numberOfLines; line++) {
for (int row = 0; row < numberOfRows; row++) {
if (line == numberOfLines-1) {
maze.getMaze()[line][row] = new EBox("E", line, row, maze);
} else {
maze.getMaze()[line][row] = previousMaze[line][row];
}
}
}
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
} }
public void delLine(int posLine) {
maze.delLine(posLine);
initModelFromMaze();
saved = false;
}
public void delLine() {
delLine(maze.getDimensionY() - 1);
}
public void clearMaze() {
newClearMaze(maze.getDimensionY(), maze.getDimensionX());
}
} }
+16 -3
View File
@@ -9,17 +9,30 @@ public class AddLineMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final int lineNum;
public AddLineMenuItem(MazeApp mazeApp) { public AddLineMenuItem(MazeApp mazeApp) {
super("Add line"); super("Add line");
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
lineNum = -1;
addActionListener(this);
}
public AddLineMenuItem(MazeApp mazeApp, int lineNum) {
super("Add line");
this.mazeApp = mazeApp;
this.lineNum = lineNum;
addActionListener(this); addActionListener(this);
} }
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent e) {
if (lineNum == -1) {
this.mazeApp.getMazeAppModel().addEmptyLine(); mazeApp.addEmptyLine();
}
else {
mazeApp.addEmptyLine(lineNum);
}
} }
} }
+17 -5
View File
@@ -3,25 +3,37 @@ package ui;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
public class AddRowMenuItem extends JMenuItem public class AddRowMenuItem extends JMenuItem
implements ActionListener { implements ActionListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private final int rowNum;
public AddRowMenuItem(MazeApp mazeApp) { public AddRowMenuItem(MazeApp mazeApp) {
super("Add row"); super("Add row");
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
rowNum = -1;
addActionListener(this);
}
public AddRowMenuItem(MazeApp mazeApp, int rowNum) {
super("Add row");
this.mazeApp = mazeApp;
this.rowNum = rowNum;
addActionListener(this); addActionListener(this);
} }
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent e) {
if (rowNum == -1) {
this.mazeApp.getMazeAppModel().addEmptyColumn(); mazeApp.addEmptyColumn();
}
else {
mazeApp.addEmptyColumn(rowNum);
}
} }
} }
+9
View File
@@ -9,6 +9,10 @@ public class BoxTypeEditionMenu extends JPopupMenu {
JMenuItem setEBox; JMenuItem setEBox;
JMenuItem setDBox; JMenuItem setDBox;
JMenuItem setABox; JMenuItem setABox;
JMenuItem addRow;
JMenuItem addLine;
JMenuItem delRow;
JMenuItem delLine;
public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) { public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) {
add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow)); add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow));
@@ -21,5 +25,10 @@ public class BoxTypeEditionMenu extends JPopupMenu {
setWBox.setEnabled(box.isWalkable()); setWBox.setEnabled(box.isWalkable());
setDBox.setEnabled(! box.isDeparture()); setDBox.setEnabled(! box.isDeparture());
setABox.setEnabled(! box.isArrival()); setABox.setEnabled(! box.isArrival());
add(addRow = new AddRowMenuItem(mazeApp, boxRow));
add(addLine = new AddLineMenuItem(mazeApp, boxLine));
add(delRow = new DelRowMenuItem(mazeApp, boxRow));
add(delLine = new DelLineMenuItem(mazeApp, boxLine));
} }
} }
+1 -10
View File
@@ -3,8 +3,6 @@ package ui;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
@@ -23,13 +21,6 @@ public class ClearMazeMenuItem extends JMenuItem
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
mazeApp.clearMaze();
int numberOfLines = mazeApp.getMazeAppModel()
.getMaze().getDimensionY();
int numberOfRows = mazeApp.getMazeAppModel()
.getMaze().getDimensionX();
this.mazeApp.getMazeAppModel()
.newClearMaze(numberOfLines, numberOfRows);
System.out.println("test");
} }
} }
+38
View File
@@ -0,0 +1,38 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class DelLineMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int lineNum;
public DelLineMenuItem(MazeApp mazeApp) {
super("Delete line");
this.mazeApp = mazeApp;
lineNum = -1;
addActionListener(this);
}
public DelLineMenuItem(MazeApp mazeApp, int lineNum) {
super("Delete line");
this.mazeApp = mazeApp;
this.lineNum = lineNum;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (lineNum == -1) {
mazeApp.delLine();
}
else {
mazeApp.delLine(lineNum);
}
}
}
+37
View File
@@ -0,0 +1,37 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class DelRowMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int rowNum;
public DelRowMenuItem(MazeApp mazeApp) {
super("Delete row");
this.mazeApp = mazeApp;
rowNum = -1;
addActionListener(this);
}
public DelRowMenuItem(MazeApp mazeApp, int rowNum) {
super("Delete row");
this.mazeApp = mazeApp;
this.rowNum = rowNum;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (rowNum == -1) {
mazeApp.delColumn();
}
else {
mazeApp.delColumn(rowNum);
}
}
}
+37
View File
@@ -87,4 +87,41 @@ public class MazeApp extends JFrame
mazeAppModel.setPathDrawn(false); mazeAppModel.setPathDrawn(false);
this.repaint(); this.repaint();
} }
public void addEmptyColumn(int posColumn) {
mazeAppModel.addEmptyColumn(posColumn);
}
public void addEmptyColumn() {
mazeAppModel.addEmptyColumn();
}
public void delColumn(int posColumn) {
mazeAppModel.delColumn(posColumn);
}
public void delColumn() {
mazeAppModel.delColumn();
}
public void addEmptyLine(int posLine) {
mazeAppModel.addEmptyLine(posLine);
}
public void addEmptyLine() {
mazeAppModel.addEmptyLine();
}
public void delLine(int posLine) {
mazeAppModel.delLine(posLine);
}
public void delLine() {
mazeAppModel.delLine();
}
public void clearMaze() {
mazeAppModel.clearMaze();
this.repaint();
}
} }
+4
View File
@@ -11,6 +11,8 @@ public class MazeMenu extends JMenu {
private final ClearMazeMenuItem clearMazeMenuItem; private final ClearMazeMenuItem clearMazeMenuItem;
private final AddRowMenuItem addRowMenuItem; private final AddRowMenuItem addRowMenuItem;
private final AddLineMenuItem addLineMenuItem; private final AddLineMenuItem addLineMenuItem;
private final DelRowMenuItem delRowMenuItem;
private final DelLineMenuItem delLineMenuItem;
public MazeMenu(MazeApp mazeApp) { public MazeMenu(MazeApp mazeApp) {
@@ -23,5 +25,7 @@ public class MazeMenu extends JMenu {
add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp)); add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp));
add(addRowMenuItem = new AddRowMenuItem(mazeApp)); add(addRowMenuItem = new AddRowMenuItem(mazeApp));
add(addLineMenuItem = new AddLineMenuItem(mazeApp)); add(addLineMenuItem = new AddLineMenuItem(mazeApp));
add(delRowMenuItem = new DelRowMenuItem(mazeApp));
add(delLineMenuItem = new DelLineMenuItem(mazeApp));
} }
} }
+17 -29
View File
@@ -15,23 +15,17 @@ import javax.swing.JSpinner;
import javax.swing.SpinnerModel; import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel; import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout; import javax.swing.SpringLayout;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class NewMazeSpinnerBox extends JPanel public class NewMazeSpinnerBox extends JPanel
implements ActionListener, implements ActionListener {
ChangeListener {
private final MazeApp mazeApp; private final MazeApp mazeApp;
private JDialog spinnerDialog; private JDialog spinnerDialog;
private int numberOfLines;
private int numberOfRows;
private SpinnerModel lineSpinnerModel; private SpinnerModel lineSpinnerModel;
private SpinnerModel rowSpinnerModel; private SpinnerModel rowSpinnerModel;
public NewMazeSpinnerBox(MazeApp mazeApp) { public NewMazeSpinnerBox(MazeApp mazeApp) {
this.mazeApp = mazeApp; this.mazeApp = mazeApp;
JPanel spinnerDialog = new JPanel(new SpringLayout()); JPanel spinnerDialog = new JPanel(new SpringLayout());
String[] labels = {"Number of lines :", "Number of rows :"}; String[] labels = {"Number of lines :", "Number of rows :"};
@@ -40,25 +34,23 @@ public class NewMazeSpinnerBox extends JPanel
int maximumValue = 200; int maximumValue = 200;
this.lineSpinnerModel = this.lineSpinnerModel =
new SpinnerNumberModel(initialValue, //initial value new SpinnerNumberModel(initialValue, //initial value
initialValue, //min initialValue, //min
maximumValue, //max maximumValue, //max
1); 1);
this.lineSpinnerModel.addChangeListener(this);
JSpinner lineSpinner = addLabeledSpinner(this, labels[0], lineSpinnerModel); JSpinner lineSpinner = addLabeledSpinner(this, labels[0], lineSpinnerModel);
this.rowSpinnerModel = this.rowSpinnerModel =
new SpinnerNumberModel(initialValue, //initial value new SpinnerNumberModel(initialValue, //initial value
initialValue, //min initialValue, //min
maximumValue, //max maximumValue, //max
1); 1);
this.rowSpinnerModel.addChangeListener(this); JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel);
JSpinner rowSpinner = addLabeledSpinner(this, labels[0], rowSpinnerModel);
} }
static protected JSpinner addLabeledSpinner(Container c, static protected JSpinner addLabeledSpinner(Container c,
String label, String label,
SpinnerModel model) { SpinnerModel model) {
JLabel l = new JLabel(label); JLabel l = new JLabel(label);
c.add(l); c.add(l);
@@ -76,8 +68,8 @@ public class NewMazeSpinnerBox extends JPanel
spinnerDialog.setLayout(new BorderLayout()); spinnerDialog.setLayout(new BorderLayout());
spinnerDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); spinnerDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
spinnerDialog.add(new NewMazeSpinnerBox(this.mazeApp), spinnerDialog.add(this,
BorderLayout.CENTER); BorderLayout.CENTER);
JButton OKButton = new JButton("OK"); JButton OKButton = new JButton("OK");
OKButton.addActionListener(this); OKButton.addActionListener(this);
@@ -89,13 +81,9 @@ public class NewMazeSpinnerBox extends JPanel
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
mazeApp.getMazeAppModel(
).newClearMaze((int) lineSpinnerModel.getValue(),
(int) rowSpinnerModel.getValue());
spinnerDialog.dispose(); spinnerDialog.dispose();
} }
@Override
public void stateChanged(ChangeEvent arg0) {
this.mazeApp.getMazeAppModel()
.newClearMaze((int) this.lineSpinnerModel.getValue(),
(int) this.rowSpinnerModel.getValue());
}
} }