Add UI to the project #29

Manually merged
regar42 merged 45 commits from mvc into master 2018-01-03 11:03:09 +00:00
14 changed files with 354 additions and 133 deletions
Showing only changes of commit a26f896c6d - Show all commits

No files matched your search

+77 -9
View File
@@ -29,31 +29,99 @@ public class Maze
}
public Maze(Maze maze){
public Maze(Maze maze) {
this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY();
this.maze = new Box[dimensionY][dimensionX];
this.maze = matrixDeepCopy(maze.getMaze());
}
for (int line = 0; line < dimensionY; line++) {
for (int row = 0; row < dimensionX; row++) {
Box box = maze.getMaze()[line][row];
public Maze(int numberOfLines, int numberOfRows, Box[][] boxes) {
this.dimensionY = numberOfLines;
this.dimensionX = numberOfRows;
this.maze = matrixDeepCopy(boxes);
}
private Box[][] matrixDeepCopy (Box[][] original) {
int numberOfLines = 0;
int numberOfRows = 0;
Box[][] copy;
/*
* We first get the dimensions of the matrix
*/
for (Box[] lineBoxes: original) {
numberOfLines++;
}
if (numberOfLines > 0) {
for (Box box: original[0]) {
numberOfRows++;
}
}
copy = new Box[numberOfLines][numberOfRows];
/*
* 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];
if (box.isWalkable()) {
if (box.isDeparture()) {
this.maze[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
copy[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
}
else if (box.isArrival()) {
this.maze[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
copy[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
}
else {
this.maze[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
copy[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
}
}
else {
this.maze[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
copy[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
}
}
}
return copy;
}
public static Maze emptyMaze(int dimension) {
Box[][] maze = new Box[dimension][dimension];
for (int line = 0; line < dimension; line++) {
for (int row = 0; row < dimension; row++) {
maze[line][row] = new EBox("E", line, row, null);
}
}
return new Maze(dimension, dimension, maze);
}
public static Maze emptyMaze(int numberOfLines, int numberOfRows) {
Box[][] maze = new Box[numberOfLines][numberOfRows];
for (int line = 0; line < numberOfLines; line++) {
for (int row = 0; row < numberOfRows; row++) {
maze[line][row] = new EBox("E", line, row, null);
}
}
return new Maze(numberOfLines, numberOfRows, maze);
}
public static Maze wallMaze(int dimension) {
Box[][] maze = new Box[dimension][dimension];
for (int line = 0; line < dimension; line++) {
for (int row = 0; row < dimension; row++) {
maze[line][row] = new WBox("W", line, row, null);
}
}
return new Maze(dimension, dimension, maze);
}
public static Maze wallMaze(int numberOfLines, int numberOfRows) {
Box[][] maze = new Box[numberOfLines][numberOfRows];
for (int line = 0; line < numberOfLines; line++) {
for (int row = 0; row < numberOfRows; row++) {
maze[line][row] = new WBox("W", line, row, null);
}
}
return new Maze(numberOfLines, numberOfRows, maze);
}
public int getCost(VertexInterface start, VertexInterface end) {
-10
View File
@@ -1,10 +0,0 @@
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);
}
}
-12
View File
@@ -1,12 +0,0 @@
package model;
public class Maze extends maze.Maze {
public Maze(String filename) {
super(filename);
}
public Maze(Maze maze) {
super(maze);
}
}
+46 -45
View File
@@ -10,67 +10,68 @@ import java.util.Observer;
import dijkstra.VertexInterface;
import dijkstra.Dijkstra;
import dijkstra.Previous;
import maze.Maze;
public class MazeAppModel extends Observable {
private Maze maze = new Maze("data/labyrinthe.txt");
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;
private String filename;
private Maze maze;
private VertexInterface departure;
private VertexInterface arrival;
private Previous previous;
private ArrayList<VertexInterface> shortest;
private ArrayList<VertexInterface> boxList;
private boolean saved = true;
public void setMaze(Maze maze) {
this.maze = new Maze(maze);
modified = true;
public MazeAppModel() {
maze = new Maze(Maze.emptyMaze(10));
initModelFromMaze();
}
private void initModelFromMaze() {
departure = maze.getDeparture();
arrival = maze.getArrival();
boxList = maze.getVertexes();
if (departure == null || arrival == null) {
previous = null;
shortest = null;
return;
}
previous = (Previous) Dijkstra.dijkstra(maze, departure);
shortest = previous.getShortestPathTo(arrival);
}
public void loadMaze(String filename) {
this.filename = new String(filename);
maze = new Maze(this.filename);
initModelFromMaze();
saved = true;
setChanged();
notifyObservers(maze);
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
public Maze getMaze() {
return maze;
}
public boolean isModified() {
return modified;
public String getFilename() {
return filename;
}
public boolean isSaved() {
return saved;
}
public void saveToFile() {
maze.saveToTextFile("data/labyOutput.txt");
maze.saveToTextFile(filename);
saved = true;
}
public void saveToFile(String filename) {
maze.saveToTextFile(filename);
}
public void paintMaze(Graphics2D g, int width, int height) {
int dimensionX = maze.getDimensionX();
int dimensionY = maze.getDimensionY();
int boxWidth = width/dimensionX;
int boxHeight = height/dimensionY;
for (int line = 0; line < dimensionY; line++) {
for (int row = 0; row < dimensionX; row++) {
if (maze.getMaze()[line][row].isWalkable()) {
if (maze.getMaze()[line][row].isDeparture()) {
g.setColor(Color.GREEN);
}
else if (maze.getMaze()[line][row].isArrival()) {
g.setColor(Color.RED);
}
else {
g.setColor(Color.BLACK);
}
}
else {
g.setColor(Color.WHITE);
}
g.fill3DRect(row*boxWidth, line*boxHeight,
boxWidth, boxHeight,
true);
}
}
this.filename = new String(filename);
maze.saveToTextFile(this.filename);
saved = true;
setChanged();
notifyObservers(MazeAppModelMessage.FileChange);
}
}
+6
View File
@@ -0,0 +1,6 @@
package model;
public class MazeAppModelMessage {
public final static String MazeRenewal = "MazeRenewal";
public final static String FileChange = "FileChange";
}
+36
View File
@@ -0,0 +1,36 @@
package ui;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileBox {
private static JFileChooser fileChooser = new JFileChooser();
private static FileNameExtensionFilter filter =
new FileNameExtensionFilter("Text files", "txt");
public static String load(MazeApp mazeApp) {
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showOpenDialog(mazeApp);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
fileChooser.getSelectedFile().getName());
String filename = fileChooser.getSelectedFile().getPath();
return filename;
}
return null;
}
public static String save(MazeApp mazeApp) {
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showSaveDialog(mazeApp);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to save to this file: " +
fileChooser.getSelectedFile().getName());
String filename = fileChooser.getSelectedFile().getPath();
return filename;
}
return null;
}
}
+4 -2
View File
@@ -5,17 +5,19 @@ import javax.swing.JMenu;
public class FileMenu extends JMenu {
private final MazeApp mazeApp;
private final QuitMenuItem quitMenuItem;
private final LoadMazeMenuItem loadMazeMenuItem;
private final SaveMazeMenuItem saveMazeMenuItem;
private final SaveMazeAsMenuItem saveMazeAsMenuItem;
private final QuitMenuItem quitMenuItem;
public FileMenu(MazeApp mazeApp) {
super("File");
this.mazeApp = mazeApp;
add(quitMenuItem = new QuitMenuItem(mazeApp));
add(loadMazeMenuItem = new LoadMazeMenuItem(mazeApp));
add(saveMazeMenuItem = new SaveMazeMenuItem(mazeApp));
add(saveMazeAsMenuItem = new SaveMazeAsMenuItem(mazeApp));
add(quitMenuItem = new QuitMenuItem(mazeApp));
}
}
+12 -20
View File
@@ -1,16 +1,12 @@
package ui;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import model.MazeAppModel;
import model.Maze;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import model.MazeAppModel;
public class LoadMazeMenuItem extends JMenuItem
implements ActionListener {
@@ -20,25 +16,21 @@ public class LoadMazeMenuItem extends JMenuItem
super("Load maze");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control O"));
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter =
new FileNameExtensionFilter("Text files", "txt");
fileChooser.setFileFilter(filter);
String filename;
int returnVal = fileChooser.showOpenDialog(mazeApp);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
fileChooser.getSelectedFile().getName());
filename = fileChooser.getSelectedFile().getPath();
mazeApp.getMazeAppModel().setMaze(new Maze(filename));
if (! mazeAppModel.isSaved()) {
if (! SaveBox.promptAndContinue(this, mazeApp, "Load maze")){
return;
}
}
String filename = FileBox.load(mazeApp);
if (filename != null) {
mazeApp.getMazeAppModel().loadMaze(filename);
}
}
}
+38 -2
View File
@@ -6,9 +6,11 @@ import model.MazeAppModel;
import java.util.Observable;
import java.util.Observer;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MazeApp extends JFrame
implements Observer {
implements Observer, WindowListener {
private final MazeMenuBar mazeMenuBar;
private final WindowPanel windowPanel;
@@ -21,10 +23,11 @@ public class MazeApp extends JFrame
this.mazeAppModel = new MazeAppModel();
mazeAppModel.addObserver(this);
addWindowListener(this);
setJMenuBar(mazeMenuBar = new MazeMenuBar(this));
setContentPane(windowPanel = new WindowPanel(this));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
pack();
setVisible(true);
@@ -40,4 +43,37 @@ public class MazeApp extends JFrame
System.out.println("New maze detected");
windowPanel.notifyForUpdates(param);
}
public void windowClosing(WindowEvent e) {
if (! mazeAppModel.isSaved()) {
if (! SaveBox.promptAndContinue(null, this, "Quit application")) {
return;
}
}
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
return;
}
public void windowActivated(WindowEvent e) {
return;
}
public void windowDeiconified(WindowEvent e) {
return;
}
public void windowIconified(WindowEvent e) {
return;
}
public void windowClosed(WindowEvent e) {
return;
}
public void windowOpened(WindowEvent e) {
return;
}
}
+34 -3
View File
@@ -7,6 +7,7 @@ import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JPanel;
import maze.Maze;
import model.MazeAppModelMessage;
public class MazeDrawing extends JPanel {
@@ -17,7 +18,6 @@ public class MazeDrawing extends JPanel {
this.mazeApp = mazeApp;
setBackground(Color.BLACK);
setPreferredSize(new Dimension(500,500)) ;
}
@Override
@@ -29,13 +29,44 @@ public class MazeDrawing extends JPanel {
int width = getWidth();
int height = getHeight();
mazeApp.getMazeAppModel().paintMaze(g2d, width, height);
paintMaze(g2d, width, height);
}
private void paintMaze(Graphics2D g, int width, int height) {
Maze maze = mazeApp.getMazeAppModel().getMaze();
int dimensionX = maze.getDimensionX();
int dimensionY = maze.getDimensionY();
int boxWidth = width / dimensionX;
int boxHeight = height / dimensionY;
for (int line = 0; line < dimensionY; line++) {
for (int row = 0; row < dimensionX; row++) {
if (maze.getMaze()[line][row].isWalkable()) {
if (maze.getMaze()[line][row].isDeparture()) {
g.setColor(Color.GREEN);
}
else if (maze.getMaze()[line][row].isArrival()) {
g.setColor(Color.RED);
}
else {
g.setColor(Color.BLACK);
}
}
else {
g.setColor(Color.WHITE);
}
g.fill3DRect(row * boxWidth,
line * boxHeight,
boxWidth,
boxHeight,
true);
}
}
}
public void notifyForUpdates(Object param) {
if (param instanceof Maze) {
if (param == MazeAppModelMessage.MazeRenewal) {
this.repaint();
}
}
+4 -14
View File
@@ -5,6 +5,7 @@ import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import model.MazeAppModel;
@@ -17,6 +18,7 @@ public class QuitMenuItem extends JMenuItem
super("Quit");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control Q"));
addActionListener(this);
}
@@ -24,21 +26,9 @@ public class QuitMenuItem extends JMenuItem
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
if (mazeAppModel.isModified()) {
int response = JOptionPane.showInternalOptionDialog(this,
"Maze not saved. Save it ?",
"Quit application",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE,
null,null,null);
switch (response) {
case JOptionPane.CANCEL_OPTION:
if (! mazeAppModel.isSaved()) {
if (! SaveBox.promptAndContinue(this, mazeApp, "Quit application")) {
return;
case JOptionPane.OK_OPTION:
mazeAppModel.saveToFile();
break;
case JOptionPane.NO_OPTION:
break;
}
}
System.exit(0);
+43
View File
@@ -0,0 +1,43 @@
package ui;
import javax.swing.JOptionPane;
import java.awt.Component;
import model.MazeAppModel;
public class SaveBox {
private static String message = "Maze not saved. Save it ?";
public static boolean promptAndContinue(Component parent,
MazeApp mazeApp,
String title) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
int response = JOptionPane.showOptionDialog(parent,
message,
title,
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE,
null, null, null);
switch (response) {
case JOptionPane.CANCEL_OPTION:
return false;
case JOptionPane.OK_OPTION:
if (mazeAppModel.getFilename() != null) {
mazeAppModel.saveToFile();
}
else {
String filename = FileBox.save(mazeApp);
if (filename != null) {
mazeApp.getMazeAppModel().saveToFile(filename);
}
else {
return promptAndContinue(null, mazeApp, title);
}
}
break;
case JOptionPane.NO_OPTION:
break;
}
return true;
}
}
+32
View File
@@ -0,0 +1,32 @@
package ui;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import model.MazeAppModel;
public class SaveMazeAsMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public SaveMazeAsMenuItem(MazeApp mazeApp) {
super("Save maze as");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control shift S"));
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
String filename = FileBox.save(mazeApp);
if (filename != null) {
mazeApp.getMazeAppModel().saveToFile(filename);
}
}
}
+22 -16
View File
@@ -1,16 +1,16 @@
package ui;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.KeyStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observer;
import java.util.Observable;
import model.MazeAppModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SaveMazeMenuItem extends JMenuItem
implements ActionListener {
implements ActionListener, Observer {
private final MazeApp mazeApp;
@@ -18,24 +18,30 @@ public class SaveMazeMenuItem extends JMenuItem
super("Save maze");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control S"));
mazeApp.getMazeAppModel().addObserver(this);
if (mazeApp.getMazeAppModel().getFilename() == null) {
this.setEnabled(false);
}
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter =
new FileNameExtensionFilter("Text files", "txt");
fileChooser.setFileFilter(filter);
if (mazeAppModel.getFilename() != null) {
mazeAppModel.saveToFile();
}
}
int returnVal = fileChooser.showSaveDialog(mazeApp);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to save to this file: " +
fileChooser.getSelectedFile().getName());
String filename = fileChooser.getSelectedFile().getPath();
mazeApp.getMazeAppModel().saveToFile(filename);
@Override
public void update(Observable obs, Object param) {
if (mazeApp.getMazeAppModel().getFilename() != null) {
this.setEnabled(true);
}
else {
this.setEnabled(true);
}
}
}