Add load maze

This commit is contained in:
antograb committed 2017-12-20 21:05:56 +01:00
1 parent 9d6b6a286f
commit 3cdf425435
10 files changed
+81 -176

No files matched your search

+4 -4
View File
@@ -40,17 +40,17 @@ public class Maze
Box box = maze.getMaze()[line][row]; Box box = maze.getMaze()[line][row];
if (box.isWalkable()) { if (box.isWalkable()) {
if (box.isDeparture()) { if (box.isDeparture()) {
this.maze[line][row] = new DBox(box.getLabel(), box.getX(), box.getY(), this); this.maze[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
} }
else if (box.isArrival()) { else if (box.isArrival()) {
this.maze[line][row] = new ABox(box.getLabel(), box.getX(), box.getY(), this); this.maze[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
} }
else { else {
this.maze[line][row] = new EBox(box.getLabel(), box.getX(), box.getY(), this); this.maze[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
} }
} }
else { else {
this.maze[line][row] = new WBox(box.getLabel(), box.getX(), box.getY(), this); this.maze[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
} }
} }
} }
+33
View File
@@ -1,5 +1,8 @@
package model; package model;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
@@ -40,4 +43,34 @@ public class MazeAppModel extends Observable {
public void saveToFile(String filename) { public void saveToFile(String filename) {
maze.saveToTextFile(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);
}
}
}
} }
-11
View File
@@ -1,11 +0,0 @@
package ui;
import java.awt.Color;
import java.awt.Graphics;
public class ArrivalDrawingBox extends DrawingBox {
public ArrivalDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color);
}
}
-11
View File
@@ -1,11 +0,0 @@
package ui;
import java.awt.Color;
import java.awt.Graphics;
public class DepartureDrawingBox extends DrawingBox {
public DepartureDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color);
}
}
-39
View File
@@ -1,39 +0,0 @@
package ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
public abstract class DrawingBox extends JPanel {
private final MazeApp mazeApp;
private Color color;
public DrawingBox(MazeApp mazeApp, Color color) {
this.mazeApp = mazeApp;
this.color = color;
setOpaque(true);
setPreferredSize(new Dimension(50,50));
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(color);
}
public void notifyForUpdates(Object param) {
this.repaint();
}
public void setColor(Color color) {
this.color = color;
}
}
-87
View File
@@ -1,87 +0,0 @@
package ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JPanel;
import maze.Maze;
public class DrawingPanelGrid extends JPanel {
private final MazeApp mazeApp;
private Maze maze;
private DrawingBox[][] drawingBoxMatrix;
private int dimensionX;
private int dimensionY;
private GridLayout gridLayout;
public DrawingPanelGrid(MazeApp mazeApp) {
this.mazeApp = mazeApp;
this.maze = mazeApp.getMazeAppModel().getMaze();
this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY();
this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX];
this.gridLayout = new GridLayout(this.dimensionY, this.dimensionX); //inverted coordinates
setLayout(gridLayout);
initFromMaze(maze);
}
private void initFromMaze(Maze maze) {
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()) {
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN);
}
else if (maze.getMaze()[line][row].isArrival()) {
drawingBoxMatrix[line][row] = new ArrivalDrawingBox(mazeApp, Color.RED);
}
else {
drawingBoxMatrix[line][row] = new EmptyDrawingBox(mazeApp, Color.BLACK);
}
}
else {
drawingBoxMatrix[line][row] = new WallDrawingBox(mazeApp, Color.WHITE);
}
add(drawingBoxMatrix[line][row]);
}
}
}
public void notifyForUpdates(Object param) {
if (param instanceof Maze) {
// set all new parameters
this.maze = (Maze) param;
this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY();
this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX];
this.gridLayout = new GridLayout(dimensionY, dimensionX);
setLayout(this.gridLayout);
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()) {
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN);
}
else if (maze.getMaze()[line][row].isArrival()) {
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.RED);
}
else {
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.BLACK);
}
}
else {
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN);
}
}
}
repaint();
}
}
}
-11
View File
@@ -1,11 +0,0 @@
package ui;
import java.awt.Color;
import java.awt.Graphics;
public class EmptyDrawingBox extends DrawingBox {
public EmptyDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color);
}
}
+42
View File
@@ -0,0 +1,42 @@
package ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JPanel;
import maze.Maze;
public class MazeDrawing extends JPanel {
private final MazeApp mazeApp;
public MazeDrawing(MazeApp mazeApp) {
this.mazeApp = mazeApp;
setBackground(Color.BLACK);
setPreferredSize(new Dimension(500,500)) ;
}
@Override
protected final void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
int width = getWidth();
int height = getHeight();
mazeApp.getMazeAppModel().paintMaze(g2d, width, height);
}
public void notifyForUpdates(Object param) {
if (param instanceof Maze) {
this.repaint();
}
}
}
-11
View File
@@ -1,11 +0,0 @@
package ui;
import java.awt.Color;
import java.awt.Graphics;
public class WallDrawingBox extends DrawingBox {
public WallDrawingBox(MazeApp mazeApp, Color color) {
super(mazeApp, color);
}
}
+2 -2
View File
@@ -6,13 +6,13 @@ import java.awt.*;
public class WindowPanel extends JPanel public class WindowPanel extends JPanel
{ {
private final DrawingPanelGrid drawingPanelGrid; private final MazeDrawing drawingPanelGrid;
public WindowPanel(MazeApp mazeApp) { public WindowPanel(MazeApp mazeApp) {
setLayout(new BorderLayout()); setLayout(new BorderLayout());
add(drawingPanelGrid = new DrawingPanelGrid(mazeApp), BorderLayout.CENTER); add(drawingPanelGrid = new MazeDrawing(mazeApp), BorderLayout.CENTER);
} }
public void notifyForUpdates(Object param) { public void notifyForUpdates(Object param) {