How does G4P draw buttons without anything being in the draw method?

edited February 2018 in Library Questions

Hi all,

Question is as in title. I'm curious about how buttons are displayed but can't see anything in the code that hints why nothing is needed in the draw method.

Thanks, Jordan

Answers

  • Processing supports a number of callbacks which a library can use. One of them is executed at the end of the draw() method but before the frame is rendered. G4P uses this to draw its controls.

  • Answer ✓

    This sketch demonstrates how G4P does it.

    Ball b;
    
    void setup() {
      size(400, 400);
      b = new Ball();
      registerMethod("draw", b);
    }
    
    void draw() {
      background(150, 240, 150);
      fill(150, 150, 200);
      noStroke();
      rect(0.25*width, 0.25*height, 0.5*width, 0.5*height);
      b.x += 1.1;
      b.x %= width;
    }
    
    public class Ball {
      float x = 200;
      float y = 200;
    
      Ball() {
      }
    
      public void draw() {
        pushMatrix();
        translate(x, y);
        stroke(200, 0, 0, 128);
        strokeWeight(2);
        fill(255, 128, 128, 64);
        ellipse(0, 0, 100, 300);
        popMatrix();
      }
    }
    
  • Ah, ok. The key part is the register method and defining the public draw method in the class.

    Thanks for the help :)

  • The class also has to be declared public

Sign In or Register to comment.