Results 1 to 5 of 5
  1. #1
    el9335 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default 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:

    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[])
        {
            
        }
    }
    and here's relevant portion of the display 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);
    
             }
        }
    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.

    Thank you!
    Emily

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    el9335 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    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?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

Similar Threads

  1. Accessing class functions in a list..
    By ruerric in forum New To Java
    Replies: 6
    Last Post: 05-25-2010, 03:12 AM
  2. access class members with linked list
    By billq in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 05:04 PM
  3. Linked List node class help!!
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-03-2010, 01:21 AM
  4. Need help using the Linked List class
    By wlc in forum New To Java
    Replies: 1
    Last Post: 08-13-2009, 03:38 AM
  5. Accessing list out another class
    By Preethi in forum New To Java
    Replies: 23
    Last Post: 10-26-2008, 02:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •