How to include G4P controls with saveFrame

A solution:

import g4p_controls.*;
boolean takeScreenshot = false;
public void setup() {
  size(200, 200);

  GButton button = new GButton(this, 75, 75, 100, 50, "Click me");
  button.addEventHandler(this, "buttonClicked");
}

public void draw() {
  if(takeScreenshot) {
    save("screenshot.png");
    takeScreenshot = false;
  }
  
  background(255);
  fill(255, 0, 0);
  rect(25, 25, 100, 100);
}

public void buttonClicked(GButton source, GEvent event) { 
  takeScreenshot = true;
} 

The issue is that the gui gets drawn after the draw() method is called. So one way to solve this is to take the screenshot at the beginning of the next time draw() gets called, before drawing anything.

5 Likes