Painting some particles stored in an ArrayList. This code works fine:
super.paintComponent(g); for (Particle b: particleArr){ g.setColor(b.getColor()); g.fillOval(b.getXCoor() + 5,b.getYCoor(), b.getParticleSize(),b.getParticleSize()); }
However this code throws a concurrent modification exception:
public void paintComponent(Graphics g){ //paint particles super.paintComponent(g); for (Particle b: particleArr){ g.setColor(b.getColor()); if (b.isDead()) particleArr.remove(b); else if (!b.isVanishing()) g.fillOval(b.getXCoor(),b.getYCoor(), b.getParticleSize(),b.getParticleSize()); else { g.fillOval(b.getXCoor() + 5,b.getYCoor(), b.getParticleSize(),b.getParticleSize()); g.fillOval(b.getXCoor() - 5,b.getYCoor(), b.getParticleSize(),b.getParticleSize()); g.fillOval(b.getXCoor(),b.getYCoor() + 5, b.getParticleSize(),b.getParticleSize()); g.fillOval(b.getXCoor(),b.getYCoor() - 5, b.getParticleSize(),b.getParticleSize()); } }
I confused. This is the garbled code with the iterator, it is running slow.
itr = particleArr.iterator();
super.paintComponent(g); while (itr.hasNext()){ particle=itr.next(); g.setColor(particle.getColor()); if (particle.isDead()) itr.remove(); else if (particle.isVanishing()) g.fillOval(particle.getXCoor(),particle.getYCoor(), particle.getParticleSize(),particle.getParticleSize()); else { g.fillOval(particle.getXCoor() + 5,particle.getYCoor(), particle.getParticleSize(),particle.getParticleSize()); g.fillOval(particle.getXCoor() - 5,particle.getYCoor(), particle.getParticleSize(),particle.getParticleSize()); g.fillOval(particle.getXCoor(),particle.getYCoor() + 5, particle.getParticleSize(),particle.getParticleSize()); g.fillOval(particle.getXCoor(),particle.getYCoor() - 5, particle.getParticleSize(),particle.getParticleSize()); }