Add loading options in JMenuBar
Need to fix bug when loading new maze with different dimensions
This commit is contained in:
1 parent
79ed877d9d
commit
7d5b2938fc
13 files changed
+51
-84
No files matched your search
@@ -8,11 +8,4 @@ public class ArrivalDrawingBox extends DrawingBox {
|
||||
public ArrivalDrawingBox(MazeApp mazeApp, Color color) {
|
||||
super(mazeApp, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void paintComponent(Graphics g) {
|
||||
|
||||
super.paintComponent(g);
|
||||
setBackground(Color.RED);
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,4 @@ public class DepartureDrawingBox extends DrawingBox {
|
||||
public DepartureDrawingBox(MazeApp mazeApp, Color color) {
|
||||
super(mazeApp, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void paintComponent(Graphics g) {
|
||||
|
||||
super.paintComponent(g);
|
||||
setBackground(Color.GREEN);
|
||||
}
|
||||
}
|
||||
+10
-7
@@ -16,21 +16,24 @@ public abstract class DrawingBox extends JPanel {
|
||||
|
||||
this.mazeApp = mazeApp;
|
||||
this.color = color;
|
||||
|
||||
setBackground(color);
|
||||
setPreferredSize(new Dimension(40,40));
|
||||
setBorder(BorderFactory.createLineBorder(Color.orange));
|
||||
|
||||
setOpaque(true);
|
||||
setPreferredSize(new Dimension(50,50));
|
||||
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
setBackground(color);
|
||||
}
|
||||
|
||||
public void notifyForUpdates(Object param) {
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
repaint();
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
@@ -22,57 +22,57 @@ public class DrawingPanelGrid extends JPanel {
|
||||
this.dimensionY = maze.getDimensionY();
|
||||
this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY];
|
||||
|
||||
setLayout(new GridLayout(this.dimensionX, this.dimensionY));
|
||||
setLayout(new GridLayout(this.dimensionY, this.dimensionX)); //inverted coordinates
|
||||
initFromMaze(maze);
|
||||
|
||||
setPreferredSize(new Dimension(256, 256));
|
||||
}
|
||||
|
||||
private void initFromMaze(Maze maze) {
|
||||
|
||||
for (int i = 0; i < dimensionX; i++) {
|
||||
for (int j = 0; j < dimensionY; j++) {
|
||||
for (int i = 0; i < dimensionY; i++) {
|
||||
for (int j = 0; j < dimensionX; j++) {
|
||||
if (maze.getMaze()[j][i].isWalkable()) {
|
||||
if (maze.getMaze()[j][i].isDeparture()) {
|
||||
add(new DepartureDrawingBox(mazeApp, Color.GREEN));
|
||||
drawingBoxMatrix[j][i] = new DepartureDrawingBox(mazeApp, Color.GREEN);
|
||||
}
|
||||
else if (maze.getMaze()[j][i].isArrival()) {
|
||||
add(new ArrivalDrawingBox(mazeApp, Color.RED));
|
||||
drawingBoxMatrix[j][i] = new ArrivalDrawingBox(mazeApp, Color.RED);
|
||||
}
|
||||
else {
|
||||
add(new EmptyDrawingBox(mazeApp, Color.BLACK));
|
||||
drawingBoxMatrix[j][i] = new EmptyDrawingBox(mazeApp, Color.BLACK);
|
||||
}
|
||||
}
|
||||
else {
|
||||
add(new WallDrawingBox(mazeApp, Color.WHITE));
|
||||
drawingBoxMatrix[j][i] = new WallDrawingBox(mazeApp, Color.WHITE);
|
||||
}
|
||||
|
||||
add(drawingBoxMatrix[j][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyForUpdates(Object param) {
|
||||
if (param instanceof Maze) {
|
||||
maze = (Maze) 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();
|
||||
for (int j = 0; j < dimensionX; j++) {
|
||||
for (int i = 0; i < dimensionY; i++) {
|
||||
if (maze.getMaze()[j][i].isWalkable()) {
|
||||
if (maze.getMaze()[j][i].isDeparture()) {
|
||||
drawingBoxMatrix[j][i].setColor(Color.GREEN);
|
||||
}
|
||||
else if (maze.getMaze()[j][i].isArrival()) {
|
||||
drawingBoxMatrix[j][i].setColor(Color.RED);
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[j][i].setColor(Color.BLACK);
|
||||
}
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[j][i].notifyForUpdates();
|
||||
drawingBoxMatrix[j][i].setColor(Color.WHITE);
|
||||
}
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[j][i].notifyForUpdates();
|
||||
}
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,4 @@ public class EmptyDrawingBox extends DrawingBox {
|
||||
public EmptyDrawingBox(MazeApp mazeApp, Color color) {
|
||||
super(mazeApp, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void paintComponent(Graphics g) {
|
||||
|
||||
super.paintComponent(g);
|
||||
setBackground(Color.WHITE);
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -12,12 +12,15 @@ public class MazeApp extends JFrame
|
||||
|
||||
private final MazeMenuBar mazeMenuBar;
|
||||
private final WindowPanel windowPanel;
|
||||
private MazeAppModel mazeAppModel = new MazeAppModel();
|
||||
private MazeAppModel mazeAppModel;
|
||||
|
||||
public MazeApp() {
|
||||
|
||||
super("Maze Solving Application");
|
||||
|
||||
this.mazeAppModel = new MazeAppModel();
|
||||
mazeAppModel.addObserver(this);
|
||||
|
||||
setJMenuBar(mazeMenuBar = new MazeMenuBar(this));
|
||||
setContentPane(windowPanel = new WindowPanel(this));
|
||||
|
||||
@@ -34,6 +37,7 @@ public class MazeApp extends JFrame
|
||||
@Override
|
||||
public void update(Observable obs, Object param) {
|
||||
|
||||
System.out.println("New maze detected");
|
||||
windowPanel.notifyForUpdates(param);
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,4 @@ public class WallDrawingBox extends DrawingBox {
|
||||
public WallDrawingBox(MazeApp mazeApp, Color color) {
|
||||
super(mazeApp, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void paintComponent(Graphics g) {
|
||||
|
||||
super.paintComponent(g);
|
||||
setBackground(Color.BLACK);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@ public class WindowPanel extends JPanel
|
||||
}
|
||||
|
||||
public void notifyForUpdates(Object param) {
|
||||
|
||||
drawingPanelGrid.notifyForUpdates(param);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user