Closing only PApplet with exit() code

Hello, i want to close only PApplet, with close button and with self exit() event when i do somethink, for exemple in this, i just click, and want to end only “AnotherWindow” applet, how to do it ?

PApplet cf;

void settings() {
  size(800, 450, P3D);
}

void setup() {
  cf = new AnotherWindow();
}

void draw() {
  background((sin(millis()/750.0)+1)/2*255);
}

class AnotherWindow extends PApplet {
  int w, h;
  public AnotherWindow() {
    super();   
    this.w = 350;
    this.h = 350;
    PApplet.runSketch(new String[]{this.getClass().getSimpleName()}, this);
  }
  public void settings() {
    size(w, h, P3D);
  }
  void setup() {
  }
  void draw() {
    background(0, 255, 0);
  }
  void mousePressed() {
    this.exit();
  }
}
1 Like
1 Like

When i want to exit in mousePressed, it stop and get error and freez

CODE:

AnotherWindow cf;

void settings() {
  size(800, 450, P3D);
}

void setup() {
  cf = new AnotherWindow();
  setDefaultClosePolicy(this, true);
}

void draw() {
  background((sin(millis()/750.0)+1)/2*255);
}

class AnotherWindow extends PApplet {
  int w, h;
  public AnotherWindow() {
    super();   
    this.w = 350;
    this.h = 350;
    PApplet.runSketch(new String[]{this.getClass().getSimpleName()}, this);
  }
  public void settings() {
    size(w, h, P3D);
  }
  void setup() {
    setDefaultClosePolicy(this, true);
  }
  void draw() {
    background(0, 255, 0);
  }
  void mousePressed() {
    cf.exit();
  }
}

void keyPressed() {
  final int k = keyCode;
  if (k == ESC)  key = 0;
}

static final void setDefaultClosePolicy(PApplet pa, boolean keepOpen) {
  final Object surf = pa.getSurface().getNative();
  final PGraphics canvas = pa.getGraphics();

  if (canvas.isGL()) {
    final com.jogamp.newt.Window w = (com.jogamp.newt.Window) surf;

    for (com.jogamp.newt.event.WindowListener wl : w.getWindowListeners())
      if (wl.toString().startsWith("processing.opengl.PSurfaceJOGL"))
        w.removeWindowListener(wl); 

    w.setDefaultCloseOperation(keepOpen?
      com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode
      .DO_NOTHING_ON_CLOSE :
      com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode
      .DISPOSE_ON_CLOSE);
  } else if (canvas instanceof processing.awt.PGraphicsJava2D) {
    final javax.swing.JFrame f = (javax.swing.JFrame)
      ((processing.awt.PSurfaceAWT.SmoothCanvas) surf).getFrame(); 

    for (java.awt.event.WindowListener wl : f.getWindowListeners())
      if (wl.toString().startsWith("processing.awt.PSurfaceAWT"))
        f.removeWindowListener(wl);

    f.setDefaultCloseOperation(keepOpen?
      f.DO_NOTHING_ON_CLOSE : f.DISPOSE_ON_CLOSE);
  }
}

1 Like

Running multiple windows using the OpenGL renderer has a tendency to crash, particularly when closing one of the PApplets. This is mainly due to use of shared static state. I reported and offered to help fix this ages ago but was told it’s unsupported and a won’t-fix. :roll_eyes:

4 Likes

Soo ? How to make other window with PApplet or draw func. with other solution ?

3 Likes