Using G4P

edited April 2018 in Library Questions

What value does a button return? What are the commands to use this value?

Answers

  • Can somebody please help me?

  • A button (or any G4P control) does not return values rather they generate events which you must catch to execute some code.

    In this example there 2 buttons which make an ellipse either bigger or smaller depending on which one you click.

    import g4p_controls.*;
    
    GButton btnSmaller, btnBigger;
    float dx, dy;
    
    void setup() {
      size(480, 320);
      dx = width/4;
      dy = height/4;
      // Create 2 buttons on the main sketch window 
      //                              x   y    w  h   face text
      btnSmaller = new GButton(this, 160, 10, 60, 30, "Smaller");
      btnBigger =  new GButton(this, 260, 10, 60, 30, "Bigger");
    }
    
    void draw() {
      background(200, 180, 255);
      // Draw elipse in centre of the screen
      stroke(128, 0, 0);
      strokeWeight(3);
      fill(192, 64, 64);
      ellipse(width/2, height/2, dx, dy);
    }
    
    void handleButtonEvents(GButton button, GEvent event) { 
      // Clicked on 'bigger'
      if (button == btnBigger && event == GEvent.CLICKED) {
        dx += 10; // make ellispe bigger
        dy += 10;
      }
      // Clicked on 'smaller'
      if (button == btnSmaller && event == GEvent.CLICKED) {
        dx -= 10; // make ellispe smaller
        dy -= 10;
      }
    }
    
Sign In or Register to comment.