I am creating a Room maze and am having trouble setting the scale and also getti
ID: 656113 • Letter: I
Question
I am creating a Room maze and am having trouble setting the scale and also getting it to correctly draw the 2D array. The issue is that it is not showing the connections that I have created and the size is too large. Below is my code, this is in java. Thanks for you help!
Draw drawing = new Draw("Rectangle Labyrinth");
drawing.setXscale(0, height);
drawing.setYscale(0, width);
double r = 0.5;
//divide height by number of window boxes
for(i = 0; i < length; i++)
for (int j = 0; j < maze[i].length; j++)
for (double x = 0.25; i < length; x++)
for (double y = 0.25; y < width; y++)
maze[i][j].draw(drawing, x, y, r);
Explanation / Answer
import java.awt.*; import java.awt.event.*; class Maze extends Frame { public static void main(String[] args) { Frame window = new Maze(); // The program can be terminated by closing the frame. window.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); window.setSize(500, 500); window.setTitle("Maze"); MazeCanvas maze = new MazeCanvas(); window.add(maze); window.show(); maze.run(); } } class MazeCanvas extends Canvas { // Description of state of maze. The value of maze[i][j] is // one of the constants WALL, PATH, EMPTY, or VISITED. (Value // can also be negative, temporarily, inside createMaze().) // A maze is made up of walls and corridors. maze[i][j] is // either part of a wall or part of a corridor. A cell that // is part of a corridor is represented by PATH if it is part // of the current path through the maze, by VISITED if it has // already been explored without finding a solution, and by // EMPTY if it has not yet been explored. int[][] maze; final static int BACKGROUND = 0; final static int WALL = 1; final static int PATH = 2; final static int EMPTY = 3; final static int VISITED = 4; // Colors associated with the preceding 5 constants Color[] color = {Color.brown, Color.black, new Color(204, 0, 0), // red new Color(51, 102, 204), // blue new Color(51, 102, 204)}; // Number of rows and columns of cells in maze, including // a wall around edges. Should be odd numbers. int rows = 45; int columns = 45; // Short delay between steps in making and solving maze int speedSleep = 30; // Graphics context for canvas, created in putSquare() Graphics me = null; // The following fields are set by checkSize() int width = -1; // width of canvas int height = -1; // height of canvas int cellWidth; // width of cell int cellHeight; // height of cell int left; // left edge of maze, allowing for border int top; // top edge of maze, allowing for border public MazeCanvas() { super(); setBackground(color[BACKGROUND]); maze = new int[rows][columns]; } // Called every time something is about to be drawn to check // the canvas size and adjust variables that depend on the size. // Returns true if the size has changed. boolean checkSize() { if (getSize().width != width || getSize().height != height) { width = getSize().width; height = getSize().height; cellWidth = width / columns; cellHeight = height / rows; left = (width - cellWidth*columns) / 2; top = (height - cellHeight*rows) / 2; return true; } return false; } // Draws the entire maze. public void paint(Graphics g) { checkSize(); for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.