• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a maze from a text file

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I'm pretty new to Java as well as these forums and I was hoping someone could point me in the right direction with my current project. I'm supposed to be creating a maze from a text file. The text file consists of an arrangement of 4 characters: *, ' ' (open space), F, and S. I successfully got my program to read the file line by line. I know this because it prints the file out to the console when I use print statements. My big problem is getting it to display on my JPanel. Each character in the file is supposed to be replaced with a 12 x 12 colored rectangle. This is what each character in the text file is supposed to represent:

* - A wall, will be Black
space - open space, will be white
S - Start point, will be Cyan
F - Finish point, will be Red

I have an array of colors which looks like this (there's 7 colors in here because I need to add more things later):



I've also declared the following variables:



Here's what my file reader looks like:



And here's what my paint method looks like:



Every time I run my program, it immediately shows a large black square, then when I load my text file nothing happens. So I'm having a lot of trouble getting elements in the text file to display as the colored bricks I need. Can anyone see what I'm doing wrong? Thank you so much for anyone's time!


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why have you got so many public fields? That is usually very bad design.
I think you are going to have to get a shjeet of paper and write down how you intend to fill in the maze with blocks. I am pretty sure your paint method won’t do it. By the way: surely you should be putting that code into the overridden paintComponent method, which starts like this:-Note protected, not public. The super call makes the display revert to normal every time it is repainted, otherwise old painting will remain visible after it ought to have vanished. Not the paint method at all.

I am going to duplicate this thread in the GUIs forum, and might remove it from this forum later.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basic code looks reasonable to me, but we can't see how you tie it all together and in what order it all executes.

So you need to add some println statements to your code.

Maybe start with a println() in the painting code to see if you are attempting to paint squares of different colors, since they all appear to be black. If they are all black then you need to look at the code where you read in the values to make sure your logic is assigning the proper int value to each square.

As a side note, final static variable should be all upper case charecters (WALL, OPEN, START, END);
 
nicole anderson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

So the public fields were what my instructor has in the file template. I remember him saying that this is generally bad practice, but for our purposes it would be easier to do it this way for now. Not sure why. Anyway, this is what I did to display my maze in case it helps anyone (I have yet to solve it using stacks and queues). Also, thanks for everyone's input, it helped:

File Reader Code:



Paint code:


}


 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard to give advice without seeing more of the code. As Campbell has already said the paint method looks wrong but without knowing what class it is in it's hard to be sure.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kieran Webster, please note this:-

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

…which you will find here. I would usually delete your answer, but I think it has enough errors in that I can safely leave it.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Kieran Webster, please note this:-

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

…which you will find here. I would usually delete your answer, but I think it has enough errors in that I can safely leave it.



This is not a solution, this is more... Guidance. A simple base that could be used to load images from a text file.

Of course if he puts this into his program he will have errors because he is missing classes/methods. I was focusing more on the loops/drawing.

@OP, take a look at my example and look at the for loops. This is drawing a 14x14 grid of images which are defined in a separate class.
It loops through the lines of the text file and draws them accordingly.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is not a solution, this is more... Guidance.


I beg to differ. Guidance is explaining to the OP where they have gone wrong and giving them information which will allow them to solve the problem for themselves without directly telling them how to do it.
Beginners generally learn very little from looking at code examples and learn an awful lot from trying to write some code themselves.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nicole can you post the code for the class that is supposed to paint the maze so we can see what you are doing and advice you on how to fix it.
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic