Change the way of drawing the maze #22
No files matched your search
+4
-4
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -32,13 +32,13 @@ public class LoadMazeMenuItem extends JMenuItem
|
|||||||
new FileNameExtensionFilter("Text files", "txt");
|
new FileNameExtensionFilter("Text files", "txt");
|
||||||
fileChooser.setFileFilter(filter);
|
fileChooser.setFileFilter(filter);
|
||||||
|
|
||||||
|
String filename;
|
||||||
int returnVal = fileChooser.showOpenDialog(mazeApp);
|
int returnVal = fileChooser.showOpenDialog(mazeApp);
|
||||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
System.out.println("You chose to open this file: " +
|
System.out.println("You chose to open this file: " +
|
||||||
fileChooser.getSelectedFile().getName());
|
fileChooser.getSelectedFile().getName());
|
||||||
}
|
filename = fileChooser.getSelectedFile().getPath();
|
||||||
|
|
||||||
String filename = fileChooser.getSelectedFile().getPath();
|
|
||||||
mazeApp.getMazeAppModel().setMaze(new Maze(filename));
|
mazeApp.getMazeAppModel().setMaze(new Maze(filename));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,9 +34,8 @@ public class SaveMazeMenuItem extends JMenuItem
|
|||||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
System.out.println("You chose to save to this file: " +
|
System.out.println("You chose to save to this file: " +
|
||||||
fileChooser.getSelectedFile().getName());
|
fileChooser.getSelectedFile().getName());
|
||||||
}
|
|
||||||
|
|
||||||
String filename = fileChooser.getSelectedFile().getPath();
|
String filename = fileChooser.getSelectedFile().getPath();
|
||||||
mazeApp.getMazeAppModel().saveToFile(filename);
|
mazeApp.getMazeAppModel().saveToFile(filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in new issue
Block a user