I open a text file which contains a maze, that consists of # and spaces. Where a hash tag represents a "wall" and the spaces are the area that you can move through.
Currently I have opened the file and stored everything in a 2d array so that the console prints out in the exact format as the text file.
My next stage that I am trying to accomplish is, if the char is a hash tag, fill the rectangle with a dark grey, if its a space fill it with white. At the moment for some reason the maze is showing up but appearing really small.
I don't know if I took the right approach but I am trying to modify the method I have rather than creating another method.
Essentially I want to fill up that whole 600x400 with my maze rather than it being so small. I thought setting the size of the rectangle would achieve this.
What I am trying is:
public static void mazeFrame() {
JFrame f = new JFrame("Maze");
f.setSize(400, 600);
f.setLocationRelativeTo(null);
f.add(new Exercise4());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
// ...
int i = 0; //y axis
if (c == '#') {
cells[i][j] = true;
g.setColor(Color.DARK_GRAY);
g.drawRect(j,i, 50,50);
} else {
cells[i][j] = false;
g.setColor(Color.white);
g.drawRect(j,i, 50,50);
}
}
i++;
}
But my output is
You are not scaling your maze.
g.drawRect(j,i, 50,50);
The values for j
and i
both start at 0 and are incremented by one.
As a consequence, your 50x50 blocks overlap.
If you'd add a scale factor, your maze would be bigger:
g.drawRect(xScale * j, yScale * i, 50,50);
Since you intend to use 50x50 blocks, the right value for xScale
and yScale
would be 50.
You could go on from there and use:
g.drawRect(xScale * j, yScale * i, xScale, yScale);
Once you've got that working, you should look into the AffineTransform class, which will allow you a lot more options. But that's for later.