Second ArrayList Won't Work

I’m trying to make particles fall from wherever the mouse is clicked and each mousebutton displays a different type of particle. My main code for just the right mouse button works perfectly with rainbow particles. I added a second class and just changed it to all white particles to see if it would work and I get an error message. My code works on processing (the program) but on repl.it (with a plugin to read processing) it does not work. here’s my code:


List<Particle> particles;
List<Right> rights;


void setup()
{
  size(500, 500);
  particles = new ArrayList<Particle>();
  rights = new ArrayList<Right>();
 
}

void draw()
{
  background(20, 25, 50);
  fill(255);
  text(particles.size() + rights.size(),250,50);
  if (mousePressed && (mouseButton == LEFT)){
    particles.add(new Particle(mouseX,mouseY));
  }
  if(mousePressed && (mouseButton == RIGHT)){
    rights.add(new Right(mouseX,mouseY));
  }
  for(int i=0; i < particles.size(); i++){
    particles.get(i).display();
    particles.get(i).update();
    particles.get(i).delete();
  }
  for(int i=0; i < rights.size(); i++){
    rights.get(i).display();
    rights.get(i).update();
    rights.get(i).delete();
  }

  
}
class Right
{
	float x;
	float y;
  float grav;
  float ySpeed;
  float xSpeed;
  float r1;
  float g1;
  float b1;

	Right(float x, float y)
	{
		this.x = x;
		this.y = y;
    xSpeed = random(-1,1);
    ySpeed = 1;
    grav = 0.2;
    r1=random(255);
    g1=random(255);
    b1=random(255);
	}
	
  void update(){
   x+= xSpeed;
   y+= ySpeed;
   ySpeed+= grav;
  }

  void display(){
    fill(255,255,255);
    ellipse(x,y,5,5);
  }
 

  void delete(){
    if(y > height || x > width || x < 0 || y<0){
      rights.remove(this);
    }
  }
}

I left out the other particle class and it is named Right because I tried renaming it a couple times thinking that was the issue.

Hi,

Looks like you forgot few things in your code :

  • the import java.util.List; in order to import the List class at the top of the program

  • Where is the Particle class? With the code that you gave us, we can’t really run it :wink:

Also note that on repl.it, some features might not work as expected compared to native Java Processing.

1 Like

The List class is basically the same as ArrayList in repl.it, when putting it into the processing IDE I change it to ArrayList, also here’s the particle class, it’s pretty much the same as the other class.

class Particle
{
	float x;
	float y;
  float grav;
  float ySpeed;
  float xSpeed;
  float r1;
  float g1;
  float b1;

	Particle(float x, float y)
	{
		this.x = x;
		this.y = y;
    xSpeed = random(-1,1);
    ySpeed = 1;
    grav = 0.2;
    r1=random(255);
    g1=random(255);
    b1=random(255);
	}
	
  void update(){
   x+= xSpeed;
   y+= ySpeed;
   ySpeed+= grav;
  }

  void display(){
    fill(r1,b1,g1);
    ellipse(x,y,5,5);
  }
 

  void delete(){
    if(y > height || x > width || x < 0 || y<0){
      particles.remove(this);
    }
  }
}

Hey, thanks for your help, but I figured it out. Repl.it requires HTML code with all of your classes declared for anything to run, and I misspelled my class name in it. Thanks though.

1 Like