mishap_n mishap_n - 2 months ago 19
Java Question

Constructor not defined [Processing]

I'm doing a really basic tutorial on ArrayList / Particle systems. I keep getting a "constructor is undefined error" and I can't figure out why. Googling brings up a lot of more complicated question/answers. What am I missing? Did this change in the last year?

ArrayList<Particle> plist;

void setup(){
size(640, 360);
plist = new ArrayList<Particle>();
println(plist);
plist.add(new Particle());
}

void draw(){
background(255);


}


class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;

Particle(PVector l){
// For demonstration purposes we assign the Particle an initial velocity and constant acceleration.
acceleration = new PVector(0,0.05);
velocity = new PVector(random(-1,1),random(-2,0));
location = l.get();
lifespan = 255;
}

void run(){
update();
display();
}

void update(){
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}

void display(){
stroke(0, lifespan);
fill(175, lifespan);
ellipse(location.x, location.y,8,8);
}

boolean isDead(){
if(lifespan < 0.0){
return true;
}else{
return false;
}
}
}

Answer

This is your Particle constructor:

Particle(PVector l){

Notice that it takes a PVector argument.

This is how you're calling the Particle constructor:

plist.add(new Particle());

This line has an error: the constructorParticle()does not exist. And that's exactly what your problem is. The constructor Particle() doesn't exist. Only Particle(PVector) exists.

In other words, notice that you aren't giving it a PVector argument. That's what your error is telling you.

To fix this, you either need to supply a PVector argument, or you need to change the constructor so it no longer needs one.