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);
}
}