Help with this code

edited April 2014 in Hello Processing

http://www.learningprocessing.com/examples/chapter-10/example-10-1/

Catcher catcher;

void setup() {
  size(400,400);
  catcher = new Catcher(32);
  smooth();
}

void draw() {
  background(255);
  catcher.setLocation(mouseX,mouseY);
  catcher.display();
}

class Catcher {
  float r;   // radius
  float x,y; // location

  Catcher(float tempR) {
    r = tempR;
  x = 0;
    y = 0;
  }

  void setLocation(float tempX, float tempY) {
    x = tempX;
    y = tempY;
  }

  void display() {
    stroke(0);
    fill(175);
    ellipse(x,y,r*2,r*2);
  }

}

What's the purpose of this part?

Catcher(float tempR) { r = tempR; ** x = 0; y = 0;** }

Isn't x = mouseX and y = mouseY? As seen on the void setLocation function.

Can't the constructor just be:

 Catcher (float tempR){
   r = tempR;

 }

Answers

  • edited April 2014

    What's the purpose of the part I highlighted?

    You tell us for we only see an underscore followed by a buncha asterisks!!! 8-}

  • edited April 2014

    Lol I edited it, and nice to see you. ;)

  • Answer ✓

    x = 0; y = 0;
    Can't the constructor just be: Catcher (float tempR){ r = tempR; }

    It's redundant since float fields starts out as 0.0f already! ;)
    So just field r really needs to be initialized! :P

    Isn't x = mouseX and y = mouseY?

    I think the idea of that class is that instead of auto-animate via a method like update(),
    it needs to be updated from outside via setLocation() in order to have its fields x & y synchronized!
    So it's not much diff. than a regular PVector after all!

    In this particular case, the overseer sketch passes the current mouse coods. via setLocation()
    to the Catcher instance each frame.

  • edited April 2014

    As a bonus, a tweaked version: :ar!

    //forum.processing.org/two/discussion/4761/help-with-this-code
    
    final static int DIAM = 0100, FPS = 60;
    final static color BG = 0300;
    
    final Catcher catcher = new Catcher(DIAM);
    
    void setup() {
      size(600, 400, JAVA2D);
      frameRate(FPS);
      smooth(4);
      noCursor();
    
      ellipseMode(Catcher.MODE);
      fill(Catcher.INK);
      stroke(Catcher.OUTLINE);
      strokeWeight(Catcher.SOLID);
    }
    
    void draw() {
      background(BG);
      catcher.setXY(mouseX, mouseY).moveXY(-50, 50).display();
    }
    
    class Catcher {
      static final short MODE  = CENTER;
      static final color INK   = #0080F0, OUTLINE = 0;
      static final float SOLID = 3.5;
    
      float x, y;
      int d;
    
      Catcher(int diam) {
        d = diam;
      }
    
      Catcher setXY(float xx, float yy) {
        x = xx;
        y = yy;
    
        return this;
      }
    
      Catcher moveXY(float xx, float yy) {
        x += xx;
        y += yy;
    
        return this;
      }
    
      Catcher display() {
        ellipse(x, y, d, d);
        return this;
      }
    }
    
Sign In or Register to comment.