Add editing functionalities

This commit is contained in:
antograb authored and Sylvain Rager (regar42) committed 2018-01-02 22:07:42 +01:00
1 parent 81df80e7ea
commit 43feeaf93f
9 files changed
+298 -16

No files matched your search

+6
View File
@@ -42,6 +42,12 @@ public class Maze
this.maze = matrixDeepCopy(boxes);
}
public Maze(Box[][] newMaze) {
this.dimensionX = newMaze[0].length;
this.dimensionY = newMaze.length;
this.maze = matrixDeepCopy(newMaze);
}
private Box[][] matrixDeepCopy (Box[][] original) {
int numberOfLines = 0;
int numberOfRows = 0;
+63 -13
View File
@@ -12,15 +12,15 @@ import maze.*;
public class MazeAppModel extends Observable {
private String filename;
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;
private boolean pathDrawn = false;
private boolean saved = true;
private boolean pathDrawn = false;
public MazeAppModel() {
maze = new Maze(Maze.emptyMaze(10));
@@ -89,18 +89,10 @@ public class MazeAppModel extends Observable {
public void toggleBox(int boxLine, int boxRow) {
if (maze.getMaze()[boxLine][boxRow].isWalkable()) {
if (maze.getMaze()[boxLine][boxRow].isDeparture()) {
setABox(boxLine, boxRow);
}
else if (maze.getMaze()[boxLine][boxRow].isArrival()) {
setEBox(boxLine, boxRow);
}
else {
setWBox(boxLine, boxRow);
}
setWBox(boxLine, boxRow);
}
else {
setDBox(boxLine, boxRow);
setEBox(boxLine, boxRow);
}
}
@@ -139,4 +131,62 @@ public class MazeAppModel extends Observable {
initModelFromMaze();
saved = false;
}
public void newClearMaze(int numberOfLines, int numberOfRows) {
Box[][] newClearMaze = new Box[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();
saved = false;
}
public void addEmptyColumn() {
Box[][] previousMaze = maze.getMaze();
int numberOfLines = maze.getDimensionY();
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();
saved = false;
}
public void addEmptyLine() {
Box[][] previousMaze = maze.getMaze();
int numberOfLines = maze.getDimensionY()+1;
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();
saved = false;
}
}
+25
View File
@@ -0,0 +1,25 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class AddLineMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public AddLineMenuItem(MazeApp mazeApp) {
super("Add line");
this.mazeApp = mazeApp;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
this.mazeApp.getMazeAppModel().addEmptyLine();
}
}
+27
View File
@@ -0,0 +1,27 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JMenuItem;
public class AddRowMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public AddRowMenuItem(MazeApp mazeApp) {
super("Add row");
this.mazeApp = mazeApp;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
this.mazeApp.getMazeAppModel().addEmptyColumn();
}
}
+35
View File
@@ -0,0 +1,35 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class ClearMazeMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
public ClearMazeMenuItem(MazeApp mazeApp) {
super("Clear maze");
this.mazeApp = mazeApp;
this.setAccelerator(KeyStroke.getKeyStroke("control D"));
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
int numberOfLines = mazeApp.getMazeAppModel()
.getMaze().getDimensionY();
int numberOfRows = mazeApp.getMazeAppModel()
.getMaze().getDimensionX();
this.mazeApp.getMazeAppModel()
.newClearMaze(numberOfLines, numberOfRows);
System.out.println("test");
}
}
+8
View File
@@ -7,6 +7,10 @@ public class MazeMenu extends JMenu {
private final MazeApp mazeApp;
private final DrawShortestPathMenuItem drawMenuItem;
private final ClearShortestPathMenuItem clearMenuItem;
private final NewClearMazeMenuItem newClearMazeMenuItem;
private final ClearMazeMenuItem clearMazeMenuItem;
private final AddRowMenuItem addRowMenuItem;
private final AddLineMenuItem addLineMenuItem;
public MazeMenu(MazeApp mazeApp) {
@@ -15,5 +19,9 @@ public class MazeMenu extends JMenu {
add(drawMenuItem = new DrawShortestPathMenuItem(mazeApp));
add(clearMenuItem = new ClearShortestPathMenuItem(mazeApp));
add(newClearMazeMenuItem = new NewClearMazeMenuItem(mazeApp));
add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp));
add(addRowMenuItem = new AddRowMenuItem(mazeApp));
add(addLineMenuItem = new AddLineMenuItem(mazeApp));
}
}
+30
View File
@@ -0,0 +1,30 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
public class NewClearMazeMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private NewMazeSpinnerBox spinnerBox;
public NewClearMazeMenuItem(MazeApp mazeApp) {
super("New clear maze");
this.mazeApp = mazeApp;
this.spinnerBox = new NewMazeSpinnerBox(mazeApp);
this.setAccelerator(KeyStroke.getKeyStroke("control N"));
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
spinnerBox.createAndShowGUI();
}
}
+101
View File
@@ -0,0 +1,101 @@
package ui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class NewMazeSpinnerBox extends JPanel
implements ActionListener,
ChangeListener {
private final MazeApp mazeApp;
private JDialog spinnerDialog;
private int numberOfLines;
private int numberOfRows;
private SpinnerModel lineSpinnerModel;
private SpinnerModel rowSpinnerModel;
public NewMazeSpinnerBox(MazeApp mazeApp) {
this.mazeApp = mazeApp;
JPanel spinnerDialog = new JPanel(new SpringLayout());
String[] labels = {"Number of lines :", "Number of rows :"};
int len = labels.length;
int initialValue = 3;
int maximumValue = 200;
this.lineSpinnerModel =
new SpinnerNumberModel(initialValue, //initial value
initialValue, //min
maximumValue, //max
1);
this.lineSpinnerModel.addChangeListener(this);
JSpinner lineSpinner = addLabeledSpinner(this, labels[0], lineSpinnerModel);
this.rowSpinnerModel =
new SpinnerNumberModel(initialValue, //initial value
initialValue, //min
maximumValue, //max
1);
this.rowSpinnerModel.addChangeListener(this);
JSpinner rowSpinner = addLabeledSpinner(this, labels[0], rowSpinnerModel);
}
static protected JSpinner addLabeledSpinner(Container c,
String label,
SpinnerModel model) {
JLabel l = new JLabel(label);
c.add(l);
JSpinner spinner = new JSpinner(model);
l.setLabelFor(spinner);
c.add(spinner);
return spinner;
}
public void createAndShowGUI() {
spinnerDialog = new JDialog(spinnerDialog, "Create a new empty maze");
spinnerDialog.setLayout(new BorderLayout());
spinnerDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
spinnerDialog.add(new NewMazeSpinnerBox(this.mazeApp),
BorderLayout.CENTER);
JButton OKButton = new JButton("OK");
OKButton.addActionListener(this);
spinnerDialog.add(OKButton, BorderLayout.SOUTH);
spinnerDialog.pack();
spinnerDialog.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
spinnerDialog.dispose();
}
@Override
public void stateChanged(ChangeEvent arg0) {
this.mazeApp.getMazeAppModel()
.newClearMaze((int) this.lineSpinnerModel.getValue(),
(int) this.rowSpinnerModel.getValue());
}
}
+3 -3
View File
@@ -4,9 +4,9 @@ import javax.swing.*;
import maze.Maze;
import java.awt.*;
public class WindowPanel extends JPanel
{
private final MazeDrawing drawingPanelGrid;
public class WindowPanel extends JPanel {
private final MazeDrawing drawingPanelGrid;
public WindowPanel(MazeApp mazeApp) {