Open app on empty maze and improve load/save
model: * Add a filename attribute to MazeAppModel to keep track of the file currently being used by the app * Add saveToFile(filename) to save the maze into a specific file * Rewrite saveToFile() to use the stored filename attribute to know into which file is to be used to save the maze, instead of the hardcoded path * Remove unused models * MazeAppModel is now initialized with an empty maze, instead of a maze read from a hardcoded path (see #13) * Add a MazeAppModelMessage class to pass proper messages to observers * Replace setMaze(maze) by loadMaze(filename) for now * Move paintMaze into ui/MazeApp ui: * Add a Save As button (just a new name for the previous Save button) and implement an actual Save button (which saves into the file currently being used), disabled when no file is currently in use. (see #16) * Create the FileBox class to handle file choices for the load, save, save as buttons * Add keystrokes to trigger Load, Save or Save As actions from the menu maze: * Add a method to Maze to perform a deep copy of a matrix of boxes * Add a static method to generate an empty maze of a fixed size (10*10) This behavior might change in the future * Add a constructor to make a Maze from a matrix of boxes
This commit is contained in:
12 files changed
+232
-119
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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,16 @@ 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));
|
||||
String filename = FileBox.load(mazeApp);
|
||||
if (filename != null) {
|
||||
mazeApp.getMazeAppModel().loadMaze(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
-4
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -26,11 +28,11 @@ public class QuitMenuItem extends JMenuItem
|
||||
|
||||
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);
|
||||
"Maze not saved. Save it ?",
|
||||
"Quit application",
|
||||
JOptionPane.YES_NO_CANCEL_OPTION,
|
||||
JOptionPane.WARNING_MESSAGE,
|
||||
null, null, null);
|
||||
switch (response) {
|
||||
case JOptionPane.CANCEL_OPTION:
|
||||
return;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user