Slow in what sense? Compared to what? Your frame rate is low, it is normal to see the sketch quite slow.
[EDIT] Just see nikp's analysis, excellent. He is right, the sketch is slow as the frame rate isn't as high as it should be. Not much to do on this, as he said, but the ArrayList is certainly not the culprit!
Notes: avoid do delete an element of a collection while iterating on it with a classical for loop. Here it isn't critical as the grow rate is the same for all circles, so that's always the last one disappearing, but with random rates, for example, you can go for a surprise...
Here is a better way to handle deletion:
Code:ArrayList signals = new ArrayList();
void setup() {
size(600, 400);
frameRate(20);
colorMode(HSB, 360, 100, 100, 100);
smooth();
noStroke();
}
void draw() {
background(0, 0, 100);
for (int i = 0; i < signals.size(); i++) {
Circle currentCirc = (Circle) signals.get(i);
currentCirc.update();
}
for (int i = signals.size() - 1; i >= 0; i--) {
Circle currentCirc = (Circle) signals.get(i);
if (currentCirc.finished()) {
signals.remove(i);
}
}
int col = round( random(0, 360) );
signals.add(new Circle(mouseX, mouseY, 3, col));
if (frameCount % 20 == 0)
{
println(second() + " - " + signals.size());
}
}
void keyPressed(){
signals.clear();
}
class Circle {
float xPos;
float yPos;
float circSize;
float growth;
float circColor;
Circle(float xPos, float yPos, float growth, float circColor) {
this.xPos = xPos;
this.yPos = yPos;
this.growth = growth;
this.circColor = circColor;
}
void update() {
fill(circColor, 100, 100);
ellipse(xPos, yPos, circSize, circSize);
circSize += growth;
}
boolean finished(){
return circSize > width;
}
}
Note I loop in reverse order to ensure indexes remain valid (if you delete item n, item n+1 becomes item n and so on).
Note also the simplified finished() test...
I can't resist, I show the code using Java 1.5 syntax, as allowed by the release 0182 (not yet fully official):
Code:// Typed ArrayList, can contain only Circle now
ArrayList<Circle> signals = new ArrayList<Circle>();
void setup() {
size(600, 400);
frameRate(20);
colorMode(HSB, 360, 100, 100, 100);
smooth();
noStroke();
}
void draw() {
background(0, 0, 100);
// New for loop syntax
for (Circle currentCirc : signals) {
currentCirc.update();
}
for (int i = signals.size() - 1; i >= 0; i--) {
// No need for cast anymore!
Circle currentCirc = signals.get(i);
if (currentCirc.finished()) {
signals.remove(i);
}
}
int col = round( random(0, 360) );
signals.add(new Circle(mouseX, mouseY, 3, col));
if (frameCount % 20 == 0)
{
println(second() + " - " + signals.size());
}
}