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!
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.
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);
Joined: Mar 11, 2013
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:
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.
Joined: Aug 07, 2007
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.
Joined: Aug 07, 2007
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.