PShape disableStyle() does not work in class
in
Programming Questions
•
7 months ago
I have a Particle class that uses a SVG file to display graphics. What I want to do is to randomize the stroke and fill color for each particle using disableStyle(), but it doesn't seem to be working. It always displays the original svg style. When I have a simple setup and draw structure without using a class, disableStyle works just fine.
Any help would be appreciated!!
MacOS 10.8.2, Processing 2.0b8
- import java.util.Iterator;
- ArrayList<Particle> particles;
- PShape s; //source file
- void setup() {
- size(1200, 800, P2D);
- frameRate(100);
- s = loadShape("candy_02.svg");
- particles = new ArrayList<Particle>();
- }
- void draw() {
- blendMode(ADD);
- background(0);
- particles.add( new Particle(s, new PVector(width>>1, height>>1)));
- PVector gravity = new PVector(0, 0.5);
- Iterator<Particle> it = particles.iterator();
- while (it.hasNext ()) {
- Particle p = it.next();
- p.applyForce(gravity);
- p.update();
- p.display();
- if (p.isDead()) {
- it.remove();
- }
- }
- frame.setTitle(int(frameRate) + " FPS");
- }
- class Particle {
- PShape s;
- PVector loc;
- PVector vel;
- PVector acc;
- float lifespan;
- float mass;
- color fc; //fill color
- color sc; //stroke color
- float theta;
- float rotInc;
- Particle(PShape _s, PVector l) {
- s = _s;
- loc = l.get();
- vel = new PVector();
- acc = new PVector( random(-2, 2), random(-2, 2) );
- lifespan = 255;
- mass = random(1, 5);
- fc = color(random(255), random(255), random(255));
- sc = color(random(255), random(255), random(255));
- theta = 0.0;
- rotInc = random(-.1, .1);
- }
- boolean isDead() {
- if (lifespan <= 0.0) return true;
- else return false;
- }
- void applyForce(PVector force) {
- PVector f = PVector.div(force, mass);
- acc.add(f);
- }
- void update() {
- vel.add(acc);
- loc.add(vel);
- acc.mult(0);
- lifespan--;
- theta += rotInc;
- }
- void display() {
- pushMatrix();
- translate(loc.x, loc.y);
- rotate(theta);
- s.disableStyle(); //this is not working
- stroke(sc);
- fill(fc);
- shape(s, 0, 0, mass*5, mass*5);
- popMatrix();
- }
- }
1