Why is my sketch showing up blank?

Hello! I’ve been trying to put this code into separate tabs with classes so I can play around with changing the mouseX and mouseY to Kinect. I was wondering what’s going wrong here? Would really appreciate some help on this :slight_smile: It is just showing up blank whenever I run it, but I don’t understand why.

TAB1

//ArrayList <Particle> particles;
//int num = 300;
ParticleSystem p;


void setup() {
  size(800, 600);
  smooth();
  
  p = new ParticleSystem();
  //particles = new ArrayList<Particle>();
  //for (int i = 0; i < num; i++) {
  //  particles.add( new Particle() );
  //}
}

void draw() {
  background(255);
  //for (int i = 0; i < num; i++) {
  // particles.get(i).move();
  // particles.get(i).draw();
  //}
  
  p.addParticle(width/2,height/2);
   
  //for (Particle p : particles) {
  //  p.move();
  //  p.sketch();
  //  p.checkMouse();
  //}
}

TAB2

class Particle {
  float px, py;
  float dx, dy;
  float damp;
  color sc;


  Particle(float x, float y) {
 
    px = width / 2;
    py = height/2;
    dx = random(width);
    dy = random(height);
    damp = random(0.01, 0.04);
    sc = color(random(100, 255), 0, random(15, 25));
  }

  void move() {
    dx += random(-5, +5);
    dy += random(-5, +5);
    px += (dx - px) * damp;
    py += (dy - py) * damp;
  }

  void sketch() {

    fill(sc, 30);
    noStroke();
    ellipse(px, py, 20, 20);
    fill(100);
    noStroke();
    ellipse(px, py, 5, 5);
  }

  void mouse() {
    dx = mouseX;
    dy = mouseY;
  }

  void checkMouse() {
    float mouseRadius = 50;
    float d = dist(mouseX, mouseY, px, py);
    if (d<mouseRadius && d > 1) {
      dx += (px-mouseX) / d*mouseRadius;
      dy += (py-mouseY) / d*mouseRadius;
    }
  }
}

TAB3


class ParticleSystem {
  ArrayList <Particle> particles;
//int num = 300;

ParticleSystem(){
particles = new ArrayList<Particle>();
}

int num = 300;
   void addParticle(float x, float y) {
       for (int i = 0; i < num; i++) {
    particles.add(new Particle(x,y));
  }
}
}

You need a for loop (in draw()) to display particles but you commented it out using //

consider

for (Particle p1 : p.particles) {
      p1.move();
      p1.sketch();
      p1.checkMouse();
  }

but this could also be a method “display” in the class ParticleSystem

(edited)

Hey, thanks so much for replying! I’ve been following this code below as a kind of blueprint for it, and the main sketch has no loops or anything. Which loop that I’d commented out should I have commented back in? I tried the two I saw in the main sketch and it would say “num cannot be resolved to a variable” and “particles cannot be resolved to a variable” when I did the other.

TAB1 (Main Sketch)

ParticleSystem ps;

void setup() {
  size(640,360);
  ps = new ParticleSystem(new PVector(width/2,50));
}

void draw() {
  background(0);
  ps.addParticle(mouseX, mouseY);
  ps.run();
}

TAB2 (Particles)

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

  Particle(float x, float y) {
    acceleration = new PVector(0, 0.05);
    velocity = new PVector(random(-1, 1), random(-2, 0));
    location = new PVector(x, y);
    lifespan = 255.0;
  }

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

  // Method to update location
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 1.0;
  }

  // Method to display
  void display() {
    stroke(255, lifespan);
    fill(255, lifespan);
    ellipse(location.x, location.y, 8, 8);
  }

  // Is the particle still useful?
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}

TAB 3 (ParticleSystem)

class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;

  ParticleSystem(PVector location) {
    origin = location.get();
    particles = new ArrayList<Particle>();
  }

  void addParticle(float x, float y) {
    particles.add(new Particle(x,y));
  }

  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}
1 Like

Thank you! With that, it’s now just saying “particles cannot be resolved to a variable” :frowning:

you changed some code now

you need this in draw()

in run() you can see is a for loop

That code os Shiffman’s I’m using as a guide for my own, the code I’m using is the one in the initial question I posted :slight_smile: I tried the

for (Particle p1 : p.particles) {
      p1.move();
      p1.sketch();
      p1.checkMouse();
  }

But it’s giving me NullPointerException :frowning:

The working version was originally this:

ArrayList <Particle> particles;
int num = 300;

void setup() {
  size(800, 600);
  smooth();
  particles = new ArrayList<Particle>();
  for (int i = 0; i < num; i++) {
    particles.add( new Particle() );
  }
}

void draw() {
  background(255);
  //for (int i = 0; i < num; i++) {
  // particles.get(i).move();
  // particles.get(i).draw();
  //}
  
  //p.addParticle(mouseX,mouseY);
   
  for (Particle p : particles) {
    p.move();
    p.sketch();
    p.checkMouse();
  }
}


class Particle {
  float px, py;
  float dx, dy;
  float damp;
  color sc;

  Particle() {
    px = width / 2;
    py = height/2;
    dx = random(width);
    dy = random(height);
    damp = random(0.01, 0.04);
    sc = color(random(100, 255), 0, random(15, 25));
  }

  void move() {
    dx += random(-5, +5);
    dy += random(-5, +5);
    px += (dx - px) * damp;
    py += (dy - py) * damp;
  }

  void sketch() {

    fill(sc, 30);
    noStroke();
    ellipse(px, py, 20, 20);
    fill(100);
    noStroke();
    ellipse(px, py, 5, 5);
  }

  void mouse() {
    dx = mouseX;
    dy = mouseY;
  }

  void checkMouse() {
    float mouseRadius = 50;
    float d = dist(mouseX, mouseY, px, py);
    if (d<mouseRadius && d > 1) {
      dx += (px-mouseX) / d*mouseRadius;
      dy += (py-mouseY) / d*mouseRadius;
    }
  }
}

But I wanted to make it like Shiffman’s so I can follow another tutorial. Sorry if I’m making this overcomplicated haha. I think basically what I mean is, I want the code above to have a ParticleSystem (thank you so much for answering btw)

not sure I understand your problem since you posted 3 different Sketches now (apart from your other thread)

this works:

ParticleSystem ps;

void setup() {
  size(640, 360);
  ps = new ParticleSystem(new PVector(width/2, 50));
}

void draw() {
  background(0);
  ps.addParticle(mouseX, mouseY);
  ps.run();
}

//============================================================================

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

  Particle(float x, float y) {
    acceleration = new PVector(0, 0.05);
    velocity = new PVector(random(-1, 1), random(-2, 0));
    location = new PVector(x, y);
    lifespan = 255.0;
  }

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

  // Method to update location
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 1.0;
  }

  // Method to display
  void display() {
    stroke(255, lifespan);
    fill(255, lifespan);
    ellipse(location.x, location.y, 8, 8);
  }

  // Is the particle still useful?
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}//class

//===============================================

class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;

  ParticleSystem(PVector location) {
    origin = location.get();
    particles = new ArrayList<Particle>();
  }

  void addParticle(float x, float y) {
    particles.add(new Particle(x, y));
  }

  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}//class
//


this works as well


//ArrayList <Particle> particles;
//int num = 300;
ParticleSystem p;


void setup() {
  size(800, 600);
  smooth();

  p = new ParticleSystem();
  //particles = new ArrayList<Particle>();
  //for (int i = 0; i < num; i++) {
  //  particles.add( new Particle() );
  //}
}

void draw() {
  background(255);
  //for (int i = 0; i < num; i++) {
  // particles.get(i).move();
  // particles.get(i).draw();
  //}

  p.addParticle(width/2, height/2);

  for (Particle p1 : p.particles) {
    p1.move();
    p1.sketch();
    p1.checkMouse();
  }
  //
}

//========================================================================= 

class Particle {
  float px, py;
  float dx, dy;
  float damp;
  color sc;


  Particle(float x, float y) {

    px = width / 2;
    py = height/2;
    dx = random(width);
    dy = random(height);
    damp = random(0.01, 0.04);
    sc = color(random(100, 255), 0, random(15, 25));
  }

  void move() {
    dx += random(-5, +5);
    dy += random(-5, +5);
    px += (dx - px) * damp;
    py += (dy - py) * damp;
  }

  void sketch() {

    fill(sc, 30);
    noStroke();
    ellipse(px, py, 20, 20);
    fill(100);
    noStroke();
    ellipse(px, py, 5, 5);
  }

  void mouse() {
    dx = mouseX;
    dy = mouseY;
  }

  void checkMouse() {
    float mouseRadius = 50;
    float d = dist(mouseX, mouseY, px, py);
    if (d<mouseRadius && d > 1) {
      dx += (px-mouseX) / d*mouseRadius;
      dy += (py-mouseY) / d*mouseRadius;
    }
  }
}

//
class ParticleSystem {
  ArrayList <Particle> particles;
  //int num = 300;

  ParticleSystem() {
    particles = new ArrayList<Particle>();
  }

  int num = 300;
  void addParticle(float x, float y) {
    for (int i = 0; i < num; i++) {
      particles.add(new Particle(x, y));
    }
  }
}
//

please post code in ONE go because otherwise I have to copy it together in one Sketch

Thank you so much!! The last one was ideal! Sorry, I was trying to be as helpful as I could but ended up making things 10x harder haha, have a nice night!

2 Likes