Hello people,

I have a 2 dimensional array of integers where 0 indicates a road and 1 indicates a wall: can you give me a general idea on how to draw a maze according ot my matrix on JFrame window that has JPanel as its contente pane ?

Thank you very much for any suggestion

let us say you have a two dimensional array with dimensions 5, 5. now, you have 25 elements in it, right? every element holds either one or zero. for each element draw a panel. if it is zero, draw road, if it is one draw wall. put each of the 25 panels on a bigger panel. however, you need to specify the x and y position of each small panel within the bigger panel. note that panel on the same row have the same y position, but their x position differs by the width of the smaller panel. for the next row, increase the y position by the height of smaller panels (this is assuming that all small panels have the same dimensions.) hope this helps.

let us say you have a two dimensional array with dimensions 5, 5. now, you have 25 elements in it, right? every element holds either one or zero. for each element draw a panel. if it is zero, draw road, if it is one draw wall. put each of the 25 panels on a bigger panel. however, you need to specify the x and y position of each small panel within the bigger panel. note that panel on the same row have the same y position, but their x position differs by the width of the smaller panel. for the next row, increase the y position by the height of smaller panels (this is assuming that all small panels have the same dimensions.) hope this helps.

why have we to use lot of small panels on the original one ? can we just use g.fillRect() for drawing the rectangles with different colors ?

I used what I mentioned but it does not compile, it shows me errors i can't fix

put the code and error here then, and someone will help you... as far as "why have we to use lot of small panels on the original one ?" goes... It makes mental visualization of the program easier for me, so I thought it would do so for you too.

put the code and error here then, and someone will help you... as far as "why have we to use lot of small panels on the original one ?" goes... It makes mental visualization of the program easier for me, so I thought it would do so for you too.

thank you, i will try to use panels as you said, probably it will be easier,

why have we to use lot of small panels on the original one ? can we just use g.fillRect() for drawing the rectangles with different colors ?

Just for displaying the maze there's probably little difference between the two approaches. If you want to interact with it in any way then having separate JComponents for each square makes it easier to respond to mouse clicks etc. Also if you want to update individual squares that's a bit easier when each is a separate component.

This article has been dead for over six months. Start a new discussion instead.