The purpose of the Java applet is as such: A ball is bouncing around the screen. The size and speed of this ball can be changed via scrollbars. The user can press and drag the mouse on the screen to draw rectangles. The ball will bounce off of these rectangles as well. The bounds of these rectangles are stored in a vector. When a rectangle is clicked, it (and all other rectangles at that point) are removed from the vector (and the screen).
The problem I’m currently having is that clicking to remove an object doesn’t work. What I’m doing is getting the point of where I clicked, and stepping through each element of the vector and seeing if it contains the point, and if it does, remove it from the vector. Here’s the method.
public void mouseClicked(MouseEvent m)
{
if (!ball.flag)
{
Vector<Rectangle> v;
v = (Vector<Rectangle>)ball.r;
Point p;
p = new Point(m.getPoint());
boolean done = false;
int i = 0;
for (Rectangle rect : v)
{
if(rect.contains(p))
{
v.removeElement(i);
System.err.print("Element removed");
continue;
}
i++;
if(i>=v.size())
done=true;
}
ball.r = v;
}
}
What you need is to use an Iterator. Like so:
This will remove elements from the list in a safe way.