G4P main window close event

edited November 2015 in Library Questions

Is there a simple example on how to run code when the G4P main window is closing ?


  • edited November 2015 Answer ✓

    This will create a new window when you close it with the red X if will execute the method windowCloseMethod.

    The code inside exit will execute when the main window closes but this does not require G4P, it is just Processing/Java

    import g4p_controls.*;
    GWindow window;
    void setup() {
      size(300, 300);
      window = GWindow.getWindow(this, "My Window", 100, 50, 480, 320, JAVA2D);
      window.addDrawHandler(this, "windowDraw");
      window.addOnCloseHandler(this, "windowCloseMethod");
    void draw() {
      background(200, 200, 255); // blue
    void windowDraw(PApplet app, GWinData data) {
      app.background(255); // white
      // draw black line to current mouse position
      app.line(app.width / 2, app.height/2, app.mouseX, app.mouseY);
    void windowCloseMethod(PApplet applet, GWinData windata) {
      println("========== WINDOW CLOSING =============================");
      window = null; // clear reference
    void exit() {
      println("========== MAIN WINDOW CLOSING ========================");
  • Thank you Sir for the fast reply. Your library and tools are awesome.....thank you for making it available.

Sign In or Register to comment.