• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating a maze in java  RSS feed

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I'm trying to create a maze using java reading from a file,

The file is a text file with 1's and 0's. Each group of 4 numbers make a Square(a 1 means there is a wall), for example if I read from the file the sequence 1011, it means that I will have a wall in the north of the square, and in the south and west too.

I'm wondering if there is a way of creating a Square with edges (the walls), because I can't think of any.

Thanks!
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What context are you working in? Console or GUI?
 
Les Morgan
Rancher
Posts: 741
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are doing this by hand, not a problem, you just have to remember that where you have a 0, you have to have a corresponding 0 in any adjacent square, so to continue you have to have a opening in the south of the square on top or you will dead end there, and a opening in north of the square on the bottom or you will dead end there.

With this in mind, then a turn would be an opening in two directions say in the example you gave, an opening to the north and then an opening to the east representing a continuation of the corridor and the turn the right.

Johnny Januan wrote:Hello guys,

I'm trying to create a maze using java reading from a file,

The file is a text file with 1's and 0's. Each group of 4 numbers make a Square(a 1 means there is a wall), for example if I read from the file the sequence 1011, it means that I will have a wall in the north of the square, and in the south and west too.

I'm wondering if there is a way of creating a Square with edges (the walls), because I can't think of any.

Thanks!
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working in GUI

But how do I "paint" the walls?? Is there any method?

I've tried to create the walls as if they were rectangles (Rectangle2D.Float), but it doesn't work.

Any ideas on how to do it?
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I can't visualize what you want to display. Can you make an image and post it?

You might look at custom painting to draw the shapes you want. That's done by using a component like a JPanel and overriding its paintComponent method which would contain your drawing code.
 
Les Morgan
Rancher
Posts: 741
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny,
It's like Norm said to get your graphic to display. You need to render to a Buffered Image and use Graphics Class' drawImage to render to your Graphic context of your display object. There is a nice discussion here that took place a couple weeks ago in the forums, pretty much showing how to do animation in Java. Take a look, the examples are commented on almost every line.
Les
 
Les Morgan
Rancher
Posts: 741
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a code snippet to supplement the example in the other thread. It should be the paintComponent method of your class used in displaying your animation--usually JPanel.
 
Piet Souris
Rancher
Posts: 1769
51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Johnny,

in addition to all the advices given above, I would like to point you to the Path2D class, and its two concrete implmenetations -.Double and -.Float.
These are ideally suited for the Squares requirements. It lets you define the outline of any shape you want, by having methods like moveTo, lineTo and even curveTo.

Your sixteen combinations of zeroes and ones enable you to set up 16 of these Shapes, and that makes creating Les' Image very easy.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I attach an example of what I want to do.

Thanks
^271BAC226A4AE1BAF243C55B9B00D263C2C01EC93F0E8C835E^pimgpsh_thumbnail_win_distr.jpg
[Thumbnail for ^271BAC226A4AE1BAF243C55B9B00D263C2C01EC93F0E8C835E^pimgpsh_thumbnail_win_distr.jpg]
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to write code that will create an image like in your last post from the groups of 4 digits read from a file?
Other questions about it:
how large are the squares (in pixels)
how many squares go on each row?
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help, but I've already solved the problem.

Now I'm going to try to create a piece and move inside the maze.
 
Knute Snortum
Sheriff
Posts: 3438
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you solve it? How would you feel about posting the code here?
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can try to explain it.
However, I'm spanish and the name of the variables are in spanish.



How do I put the piece in the maze, and move it with the keyboard? Do I need to create a separate class called Piece or something like that? And how does the piece know where are the walls, in order to avoid them?

Thanks
 
Les Morgan
Rancher
Posts: 741
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny,
Your override of the paintComponent is NOT right. Get all the IO and other things out of there, it should be as tight as possible, probably 3 or 4 lines at most. Do your rendering to a BufferedImage and then paint it an your Graphics context--too much done in the paintComponent method is a great way to get your game to be slow and the graphics to flicker.

To get your game piece on the screen, you do it like in the examples I gave, you either use a pre-existing class, like oval, or make your own.

To do movement, you need to have 2 arrays: your graphics, which you will have rendered to a BufferedImage and render to your Graphics Context in your display object paintComponent override, and an array that holds what type of room you are in. So following along with your original 4 bit design and bit masks:

Walls:
NORTH: 1000 = 8
EAST: 0100 = 4
SOUTH: 0010 = 2
WEST: 0001 = 1

These are bit masks, so each represent a power of 2. So if you have a opening to the North and no where else, the value would be 0111 or 7, bit masks are significant because now if you want to move North, you do a bitwise "&" with the North big mask and the room value:


So use 2 arrays--one to display (your BuferedImage) and one to check movement your bit masked room configurations.

Actual movement is covered in the examples and discussion I already gave you a link to.

Johnny Januan wrote:I can try to explain it.
However, I'm spanish and the name of the variables are in spanish.



How do I put the piece in the maze, and move it with the keyboard? Do I need to create a separate class called Piece or something like that? And how does the piece know where are the walls, in order to avoid them?

Thanks
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another way to proceed:
Define an image to draw the maze on and get a Graphics object for it:
Rename your current paintComponent() method to something like: buildMazeImage() and call it with the Graphics from above:
Now mazeImage has the maze drawn on it.

Create a paintComponent method from what Les posted.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!