ArrayList containing an array of objects?
in
Programming Questions
•
2 years ago
Hi,
I'm trying to figure out what's wrong with my code, and whether I'm doing it the right way or not (obviously not :))
What I'd like to do is to start with one moving array of arcs, then at each mouse click add a new array of arcs to the mouseX and mouseY coordinates. Now instead of creating a new object at the desired location AND keeping the previous one at its location, it seems that both of them are moved to the new location.
Thanks in advance for your help! ;)
I'm trying to figure out what's wrong with my code, and whether I'm doing it the right way or not (obviously not :))
What I'd like to do is to start with one moving array of arcs, then at each mouse click add a new array of arcs to the mouseX and mouseY coordinates. Now instead of creating a new object at the desired location AND keeping the previous one at its location, it seems that both of them are moved to the new location.
Thanks in advance for your help! ;)
- ArrayList circles;
Arcs[] particle = new Arcs[10];
float step = 0.005;
float hueStep = 0.1;
void setup() {
size(600,600);
strokeCap(SQUARE);
background(255);
smooth();
noFill();
colorMode(HSB);
circles = new ArrayList();
circles.add(new Circle(width/2, height/2));
}
void draw() {
background(255);
for (int i=circles.size()-1; i>=0; i--) {
Circle singleCircle = (Circle) circles.get(i);
singleCircle.move();
singleCircle.display();
}
}
class Circle {
Circle(float x_, float y_) {
constructParticle(x_, y_);
}
void move() {
for (int i=0; i<particle.length; i++) {
particle[i].move();
}
}
void display() {
for (int i=0; i<particle.length; i++) {
particle[i].display();
}
}
}
class Arcs {
float x, y;
float arcDiam, arcStrokeWeight;
float hueShift, brightnessShift, transparencyShift;
color arcStrokeColor;
float noiseOrigin, noiseOriginIncrement;
float noiseLength, noiseLengthIncrement;
float arcOrigin, arcLength;
Arcs(float x_, float y_) {
x = x_;
y = y_;
arcDiam = random(width/4, width/2);
arcStrokeWeight = random(width/64, width/8);
hueShift = int(random(40));
brightnessShift = int(random(230,255));
transparencyShift = int(random(50,140));
noiseOrigin = random(100);
noiseOriginIncrement = random(-step, step);
noiseLength = random(100);
noiseLengthIncrement = random(-step, step);
}
void move() {
noiseOrigin += noiseOriginIncrement;
noiseLength += noiseLengthIncrement;
arcOrigin = radians(noise(noiseOrigin)*360);
arcLength = radians(noise(noiseLength)*360);
}
void display() {
hueShift += hueStep;
hueShift %= 255;
arcStrokeColor = color(hueShift, 255, brightnessShift, transparencyShift);
strokeWeight(arcStrokeWeight);
stroke(arcStrokeColor);
arc(x, y, arcDiam, arcDiam, arcOrigin, arcOrigin+arcLength);
}
}
void constructParticle(float x_, float y_) {
for (int i=0; i<particle.length; i++) {
particle[i] = new Arcs(x_, y_);
}
}
void mousePressed() {
circles.add(new Circle(mouseX, mouseY));
}
1