File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes access rights variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "access rights variables" Watch "access rights variables" New topic
Author

access rights variables

Peter Steglich
Greenhorn

Joined: Jul 18, 2009
Posts: 5
Hey everybody,

I try to write an algorithm for creating a maze (or more precise the path through the maze). My idea is that I have a loop that will crawl trough the array from a start Point. From every point the path reaches it can crawl in a random amount of directions and set the value for the maze = 1 (which stands for the path and 0 for a wall).

My problem now is that I have to save the old points in a ArrayList when I go from one point into multiple directions so I can later read from the list to come back and to some more crawling form that point which will go to several new points which then will be saved in the list and so on.
But whenever I save a new point in the ArrayList and then change the Point back to its original value (since I may have to go to more directions from this point) it changes the Value of the Point saved in the List as well. I am pretty sure that my whole understanding of who has which access rights here is mixed up.
How can I change the code so that the line 29 does not change the value of the point in the ArrayList.
I hope I make a little bit of sense.
Thank you very much for your help.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
what if you change this:


to this:


Your ArrayList doesn't hold a list of Points but rather a list of references to Points. When you extract a reference and change the underlying point, this will also be reflected in the ArrayList. To prevent this from happening, you can use the Point extracted from the ArrayList to create a new Point object, and changes to this new object won't be reflected in the arraylist's point.
Peter Steglich
Greenhorn

Joined: Jul 18, 2009
Posts: 5
Thank you very much for your reply. I now at least understand what the problem is.

Your solution does not solve it, though. "goAhead" actually runs in a loop so when I execute it more than once I get the same problem with the reference.

My question is probably how can I create a Vector/List/Array (whatever is best) that holds the Points I give it. They shall not change upon changing the Point Loc of the method which is change constantly throughout the program.

Thank you again!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you don't want to modify the original instance then you'll need to make a copy of it.
Peter Steglich
Greenhorn

Joined: Jul 18, 2009
Posts: 5
Hi,
thanks for the answer.
I changed my code into:

and it works. But I am not sure whether this is what you meant by make a copy. I am very open for any cleaner approach^^

I have another problem with my maze, but am not sure whether I should start a new topic...So here it goes ;)
I am drawing my maze[][]-Array onto a JPanel. This is initialized by the mazeGUI.java as follows:


Then it calls createMaze.java which part of it I already posted:


As you can see I have the createPath() from the first question in the paint method, which I now know is stupid. Right now every time the program paints on the JPanel it also produces a new Maze, right?
The examples I found online all used the paint method so I guess that is the way to go. But how do I change the code so that whenever the maze[][]-content is changed it also draws a new maze on the JPanel. I have read something about a repaint method which is called in that case. But how do I define it and where do I have to put the createPath()?

Thank you very much although it is admittedly a fairly large chunk of code.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
David Newton wrote:If you don't want to modify the original instance then you'll need to make a copy of it.


Which is what I believe my code example did.
Peter Steglich
Greenhorn

Joined: Jul 18, 2009
Posts: 5
pete stein wrote:
David Newton wrote:If you don't want to modify the original instance then you'll need to make a copy of it.


Which is what I believe my code example did.


As far as I understand it your code creates a copy pete and so does mine. But since I have a loop in CheckValueOfMazePoint I would have to give the index-variable to the method, right? The LocCont that I create should be a copy as well or am I wrong there?
It is as I said probably not the cleanest code but works for now.

More urgent is the question about where to put the paint method and how to handle it...

I know you are all very smart ;) please give me an answer.

Thank you so much and enjoy your Sunday!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You have several problems with this code. Those that I immediately recognize include:
1) You're overriding a JComponent's paint method:

Don't do this as you lose many Swing benefits such as double buffering. Instead override paintComponent, and be sure that the first method called in this method is the super's method so that the JPanel can "refresh" itself when you repaint it:


2) You're trying to do program logic from within the paint/paintComponent method:

You can only indirectly control when paint/paintComponent is called via a call to repaint(). Even if you call repaint, there's no guarantee that paint/paintComponent will be called. The methods are often called at the request of the operating system, something you have very little real control over. It is best to do just painting in these methods and leave the program logic elsewhere.

Also,
Peter Steglich wrote:I have read something about a repaint method which is called in that case. But how do I define it and where do I have to put the createPath()?

You'll need to read the Sun Swing graphics tutorials which will help you to learn when and where to call repaint(). Usually you do it after you've changed the state of something that has a visual representation in your program, for instance after you update the maze, you'll call repaint so that the changes can be seen. There are several overloads for repaint, and the tutorials will go into the details on the benefits and problems with each one.
Peter Steglich
Greenhorn

Joined: Jul 18, 2009
Posts: 5
Hey,

thanks to your answer and the tutorial I was now able to change the code so it works.

Thank you so much.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You're quite welcome. Best of luck on this project.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access rights variables
 
Similar Threads
Array overwritten
Thread.sleep question
Fill a shape based on a General Path
Making a diagram interactive???
.txt file to char array