Probably an easy question, but I am just learning. I need to: a- have a container that can hold a variable number of objects depending on the user actions, eg. every time the screen is clicked on the coordinates of the click point are added to a list, but this list can hold a "infinite" number of points.
b-create multiple objects, each with a different name (I think?) eg, in the above example, each mouse click causes a new object "x" to be created so that I have "x1,x2,..xn"
Use java.util.Vector . Look at the API for documentation. Just on hint: Vector stores instances of "Object", what means it can store any class. But it does not know of what class the stored Object is. So if later you want to access the Object, you must first cast it. For example:
Point2D.Double p = new Point2D.Double(15, 16);
Vector v = new Vector();
v.add(p);
//the following line will result in a compile error, since you cannot assign Object to a Point2D.Double variable
Point2D.Double p2 = v.get(0);
//this is the correct way:
Point2D.Double p3 = (Point2D.Double)v.get(0);
//This is the way to acces a property directly
double x2 = ((Point2D.Double)v.get(0)).getX();
"different names" des not make much sense, since at runtime there exist no "names" for variables. Maybe you need a description or something.
In this case create a class containing a description and a Point and add instances of this class to the vector.
For getting the coordinates add an actionlistener to the panel (you are using a panel, right?), ask for Mousclick and get the coordinates from the event variable.
Just a point. Vector is not usually the best choice for a collection class as all it's methods are synchronised and therefore slow (unless you need the synchronisation between threads). You are better to use a type of List.