简体   繁体   中英

Iterating through a Linked List in another class (Java Applet)

I have this code for when an applet button is pressed:

public void actionPerformed(ActionEvent e)
  {
    oval p;
    int x, y, height, width, fill;
    x = Integer.parseInt(xfield.getText());
    y = Integer.parseInt(yfield.getText());
    height = Integer.parseInt(heightf.getText());
    width = Integer.parseInt(widthf.getText());
    list.add(new oval(x,y,height,width));
    repaint();
}

I need to iterate through the list of ovals and draw them in this class:

public void draw(Graphics g)
 {
   ListIterator li;
   li = list.listIterator();
   while(li.hasNext())
   {
   g.drawOval(x, y, height, width);
   } 
}

How do I get my list into this class? Is there a way I can pass it to the class as a parameter?

You could add a private List<oval> ovals; list to the class that contains the actionPerformed method, and add the ovals to that list (as it could like you're doing already).

Then you create a public List<oval> getOvals() method, and use that to access the list from your draw method in the other class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM