Add Observer and Observable implementation
Add update method, notifyForUpdates methods Currently not working
This commit is contained in:
1 parent
892d7e6ff3
commit
f8623fdd7a
13 files changed
+165
-14
No files matched your search
@@ -1,11 +1,18 @@
|
||||
package ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class DepartureDrawingBox extends DrawingBox {
|
||||
|
||||
@@ -8,4 +9,10 @@ public class DepartureDrawingBox extends DrawingBox {
|
||||
super(mazeApp, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void paintComponent(Graphics g) {
|
||||
|
||||
super.paintComponent(g);
|
||||
setBackground(Color.GREEN);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public abstract class DrawingBox extends JPanel {
|
||||
@@ -17,4 +19,16 @@ public abstract class DrawingBox extends JPanel {
|
||||
setBackground(color);
|
||||
setPreferredSize(new Dimension(40,40));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
|
||||
super.paintComponent(g) ;
|
||||
setBackground(color);
|
||||
}
|
||||
|
||||
public void notifyForUpdates(Object param) {
|
||||
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
@@ -8,17 +8,19 @@ import maze.Maze;
|
||||
|
||||
public class DrawingPanelGrid extends JPanel {
|
||||
|
||||
private final MazeApp mazeApp;
|
||||
private Maze maze;
|
||||
private int dimensionX;
|
||||
private int dimensionY;
|
||||
private final MazeApp mazeApp;
|
||||
private Maze maze;
|
||||
private DrawingBox[][] drawingBoxMatrix;
|
||||
private int dimensionX;
|
||||
private int dimensionY;
|
||||
|
||||
public DrawingPanelGrid(MazeApp mazeApp) {
|
||||
|
||||
this.mazeApp = mazeApp;
|
||||
this.maze = mazeApp.getMazeAppModel().getMaze();
|
||||
this.mazeApp = mazeApp;
|
||||
this.maze = mazeApp.getMazeAppModel().getMaze();
|
||||
this.dimensionX = maze.getDimensionX();
|
||||
this.dimensionY = maze.getDimensionY();
|
||||
this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY];
|
||||
|
||||
setLayout(new GridLayout(this.dimensionX, this.dimensionY));
|
||||
initFromMaze(maze);
|
||||
@@ -28,27 +30,49 @@ public class DrawingPanelGrid extends JPanel {
|
||||
|
||||
private void initFromMaze(Maze maze) {
|
||||
|
||||
int dimensionX = maze.getDimensionX();
|
||||
int dimensionY = maze.getDimensionY();
|
||||
|
||||
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()) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyForUpdates(Object 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();
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[j][i].notifyForUpdates();
|
||||
}
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[j][i].notifyForUpdates();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class EmptyDrawingBox extends DrawingBox {
|
||||
|
||||
@@ -8,4 +9,10 @@ public class EmptyDrawingBox extends DrawingBox {
|
||||
super(mazeApp, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void paintComponent(Graphics g) {
|
||||
|
||||
super.paintComponent(g);
|
||||
setBackground(Color.WHITE);
|
||||
}
|
||||
}
|
||||
+11
-1
@@ -4,7 +4,11 @@ import javax.swing.*;
|
||||
import maze.Maze;
|
||||
import model.MazeAppModel;
|
||||
|
||||
public class MazeApp extends JFrame {
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
public class MazeApp extends JFrame
|
||||
implements Observer {
|
||||
|
||||
private final MazeMenuBar mazeMenuBar;
|
||||
private final WindowPanel windowPanel;
|
||||
@@ -26,4 +30,10 @@ public class MazeApp extends JFrame {
|
||||
public MazeAppModel getMazeAppModel() {
|
||||
return mazeAppModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable obs, Object param) {
|
||||
|
||||
windowPanel.notifyForUpdates(param);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class WallDrawingBox extends DrawingBox {
|
||||
|
||||
@@ -8,4 +9,10 @@ public class WallDrawingBox extends DrawingBox {
|
||||
super(mazeApp, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void paintComponent(Graphics g) {
|
||||
|
||||
super.paintComponent(g);
|
||||
setBackground(Color.BLACK);
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,10 @@ public class WindowPanel extends JPanel
|
||||
|
||||
add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
public void notifyForUpdates(Object param) {
|
||||
|
||||
drawingPanelGrid.notifyForUpdates(param);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user