i'm trying to access the information in a single linked list from another class under the same package, not sure how to or if i even can. i can't seem to find a direct answer. i hope its not something silly, so bear with me if i'm overlooking a detail that i should have seen. i think i just need another perspective on it.
so the point of the program thus far is to read in a series of 400 coordinates from a test file and draw the resulting polygon in a gui.
so here's the read in/link creation:
and here's relevant portion of the display class:
so that obviously doesn't compile because the linked list is instantiated in the FileIn class.
i want to access the variables stored in the points list that was created so that i can draw lines between the points as the list is traversed. i'm actually getting an idea as i'm typing this but if anyone can take a look i'd surely appreciate it.
i've hit a wall and any help would be greatly appreciated.
I'm not sure I understand the flow of control in your program--it seems a little odd having your main method inside the FileIn class. But in any event, the paintComponent() method just needs to receive the SingleLinkedList points as a parameter, and you should be good to go.
If you want to access a variable in one class from another, you either need to give access to it via a getter function, or pass it in as a parameter in a constructor or a setter function.
In your case, you would either have a getPoints method in the FileIn class, or a setPoints method (or pass the list into the constructor) in the Display class. Which one you want depends on how you are creating these classes, and which has access to which.
Joined: Sep 28, 2010
Kurt Van Etten wrote:it seems a little odd having your main method inside the FileIn class.
I could agree with that.
Kurt Van Etten wrote:But in any event, the paintComponent() method just needs to receive the SingleLinkedList points as a parameter, and you should be good to go.
No, this is almost definitely wrong. The display class is probably a subclass of JPanel, and paintComponent is a very specific method that is called by the parent class for repainting. Adding a parameter to the paintComponent method would break that fundamental functionality.
The Display class does need access to the list, but not via the paintComponent method. It either needs to call a getter function of FileIn, or more likely have a setPoints method or take the list as a parameter in the constructor.