Results 1 to 5 of 5
- 10-07-2010, 02:40 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
accessing single linked list from another class
hey there,
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:Java Code:package javaapplication11; import java.io.*; import java.util.*; /* implements methods of Node and SingleLinkedList classes to create a linked list of coordinates read in from the text file of points */ public class FileIn { int x; int y; int i; Node current; SingleLinkedList points = new SingleLinkedList(); public FileIn (String in) { try { BufferedReader inFile = new BufferedReader(new FileReader(in)); String str; while ((str = inFile.readLine()) != null) { Scanner darkly = new Scanner(str); System.out.println(str); i++; while (darkly.hasNext()) { x = darkly.nextInt(); y = darkly.nextInt(); System.out.println("got here"); } System.out.println("hio, i am node " + i); points.addFirst(x,y); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String args[]) { } }
so that obviously doesn't compile because the linked list is instantiated in the FileIn class.Java Code:public void paintComponent(Graphics g) { g.setColor(Color.darkGray); g.fillRect(0, 0, 500, 500); for (i = 0; i < 400; i++) { x1= getX(i); x2= getnextX(i); y1=getY(i); y2=getnextY(i); g.drawLine(x1,y1,x2,y2); } }
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.
Thank you!
Emily
- 10-07-2010, 02:54 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Somewhere or other (not shown in the code) you must actually create an instance of FileIn.
That instance could be passed to the display class so that paintComponent() has some data to work with.
- 10-07-2010, 02:59 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
it's instantiated in the main method of FileIn, i forgot to include it in this snippet, i was moving code around. how do i pass that instance to display?
- 10-07-2010, 07:04 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
I don't think it matters too much where the FileIn instance is instantiated - do that whereever is convenient.
As for letting the display know about what data it was supposed to show: one way would be if the display had a constructor that had a FileIn instance as an argument:
Java Code:class FileInView { // extends JComponent or whatever private FileIn data public FileInView(FileIn data) { this.data = data; } public void paintComponent(Graphics g) { // use data to obtain the points // and draw them }
Another method would be to provide the FileInView (or whatever you're calling the display) a setData() method that did the same as the constructor above (and probably also caused a repaint to happen). That way the component - or whatever the display is - could be quite flexible and would allow one lot of point to be replaced with another.
- 10-07-2010, 01:52 PM #5
Similar Threads
-
Accessing class functions in a list..
By ruerric in forum New To JavaReplies: 6Last Post: 05-25-2010, 03:12 AM -
access class members with linked list
By billq in forum New To JavaReplies: 5Last Post: 05-09-2010, 05:04 PM -
Linked List node class help!!
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 05-03-2010, 01:21 AM -
Need help using the Linked List class
By wlc in forum New To JavaReplies: 1Last Post: 08-13-2009, 03:38 AM -
Accessing list out another class
By Preethi in forum New To JavaReplies: 23Last Post: 10-26-2008, 02:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks