Here is my applet class:
package DavidPackages.Prototypes.Samples.BubblesV2SSCCE;
import java.applet.Applet;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Random;
public class BubbleAppletV2SSCCE extends Applet implements Runnable{
private Thread thread;
private ArrayList<Ellipse2D> circles;
public void init(){
//Initialize bubbles with one entry so that we have a bubble to start out with
Random r = new Random();
circles = new ArrayList<Ellipse2D>();
circles.add(new Ellipse2D.Float(5, 5, 15, 15));
thread = new Thread(this);
thread.start();
}
public void run(){
while(true){
update();
repaint();
try{
Thread.sleep(10);
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
private void update(){
circles.add(new Ellipse2D.Float(5, 5, 15, 15));
}
public void paint(Graphics graphics){
for(Ellipse2D circle : circles){
((Graphics2D) graphics).draw(circle);
}
}
public void stop(){}
}
Here is the stack trace:
Exception in thread "AWT-EventQueue-1" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at DavidPackages.Prototypes.Samples.BubblesV2SSCCE.BubbleAppletV2SSCCE.paint(BubbleAppletV2SSCCE.java:43)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
at sun.awt.RepaintArea.paint(RepaintArea.java:241)
at apple.awt.ComponentModel.handleEvent(ComponentModel.java:268)
at java.awt.Component.dispatchEventImpl(Component.java:4159)
at java.awt.Container.dispatchEventImpl(Container.java:2068)
at java.awt.Component.dispatchEvent(Component.java:3918)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:501)
at java.awt.EventQueue.access$000(EventQueue.java:80)
at java.awt.EventQueue$1.run(EventQueue.java:462)
at java.awt.EventQueue$1.run(EventQueue.java:461)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:84)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:95)
at java.awt.EventQueue$2.run(EventQueue.java:476)
at java.awt.EventQueue$2.run(EventQueue.java:475)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:84)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:473)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
The only call here that’s in my code is:
at DavidPackages.Prototypes.Samples.BubblesV2SSCCE.BubbleAppletV2SSCCE.paint(BubbleAppletV2SSCCE.java:43)
which is this line:
for(Ellipse2D circle : circles){
More Details:
- I’m running this in IntelliJ IDEA 10.5.4
- I’m running this on a Mac running OSX 10.5.8
- The exception is more likely to be thrown when there are more instances of Bubble in bubbles
- The exception is more likely to be thrown when multiple instances of the applet are running
I’m confused because I don’t see how I’m modifying the contents of bubbles while control is in that for loop.
Does anyone have any ideas?
Bonus Questions:
- My intention is to eventually have created a game that runs on someones machine, and not in a web browser. What should I be using for this other than an applet?
- What should I be using instead of AWT?
In regard to the second bonus question up there. I found this article which says that:
AWT provides a rich graphics environment, especially in Java V1.2 and beyond. Through the Graphics2D object, and Java2D and Java3D services, many powerful graphical applications, such as drawing and charting packages and, combined with JavaSound, competitive interactive games, can be created.
This leads me to believe that AWT is indeed right for me. Or is the author mistaken? The article is from 2006 so it’s a little dated but not much.
UPDATES (stuff that wasn’t in the original post):
- Long block of commented out code removed
- Updated my example
- Added bonus questions
- Added quote from article comparing AWT and Swing and SWT
You are not changing the content of bubbles in the paint method, but you are changing them in the run method, and that method is executed in a separate thread. That explains why you don’t always get that exception, it only happens when paint just happens to be executed at the same time that you are changing the list bubbles in another thread.