Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default help with saving objects in ArrayList

    I hope that someone can help me with my code.
    I'm trying to build a simple paint app.

    I want to save the Circle object in the ArrayList each time the constructor in class Circles runs so that i can save the arraylist.
    When i run the code the ArrayList has only one element, i'm guessing my logic is wrong and if i put the list.add() in the class Circles constructor it won't add a circle object each time i call the g.fillOval() on the JPanel.

    pan.addMouseListener(
    new MouseAdapter(){
    @Override
    public void mouseClicked(MouseEvent ev){
    Circles c = new Circles(ev.getX()-radius,ev.getY()-radius,radius);
    Graphics g = pan.getGraphics();
    c.draw(g);
    }

    }
    );
    pan.addMouseMotionListener(
    new MouseAdapter(){
    @Override
    public void mouseDragged(MouseEvent ev){
    Circles c = new Circles(ev.getX()-radius,ev.getY()-radius,radius);
    // c.list.add(c);
    Graphics g = pan.getGraphics();
    c.draw(g);

    }
    }
    );

    public class Circles implements Serializable{
    private int x,y,radius;
    ArrayList<Circles> list = new ArrayList<Circles>();

    public Circles(int x,int y,int radius){
    this.x = x;
    this.y = y;
    this.radius = radius;

    list.add(this);


    // System.out.println(list);
    System.out.println(list.size());
    }
    void draw(Graphics g){
    g.fillOval(x-radius, y-radius, radius*2, radius*2);

    }

    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Make the ArrayList static

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Why do a lot of people think the answer is to make things static. It isn't. The solution is to take the list of Circle objects out of the Circle class. Think about the real world. Do YOU as a Person object have a list of other Person objects inside you. Or perhaps you have an AddressBook to contain the information about other Persons.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Do YOU as a Person object have a list of other Person objects inside you
    Perhaps I am schizophrenic. I agree with you about the static thing though. I was being lazy with the solution.

  5. #5
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Saving objects problem
    By BennyJass in forum Networking
    Replies: 2
    Last Post: 01-15-2011, 10:49 PM
  2. Saving Objects Permanently?
    By ethann_91 in forum New To Java
    Replies: 1
    Last Post: 01-20-2010, 08:03 AM
  3. help needed in saving objects
    By sandeepsai17 in forum New To Java
    Replies: 6
    Last Post: 06-30-2009, 04:02 AM
  4. Saving ArrayList to File
    By nwboy74 in forum Advanced Java
    Replies: 6
    Last Post: 12-08-2008, 10:19 AM

Posting Permissions

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