Multiple Windows, closing and interactions

Hi,

For the last days, I have been playing with PApplet’s and creating multiple windows.
After some long research I found this post on the forum (Closing a second window)

Now, I tried to modify the code to reopen and close the second window using a button / clicking the main window. This is part of a bigger project but I just wanted to make sure that I am doing this correctly.

So, to “close” the second window (which in truth is hiding it), when pressing the close button I need to call the makeVisible() method correct and set it to false right? And this should stop the second window from running correct? I am a bit confused still with this and the setDefaultClosePolicy().
And to reopen I just simply make it visible again?
(I know the code is working, just wanted to make sure that things are correctly set)

Is there a way to actually close the window instead for good? Or hiding it does not consume processing time?

The code that I am using is the one below:

PWindow win;
Button b;

public void settings() {
  size(320, 240);
}

void setup() { 
  surface.setLocation(20, 20);
  win = new PWindow();
}

void draw() {
  background(255, 0, 0);
  fill(0);
  rect(0, 0, width/2, height);

  fill(240);
  rect(width/2, 0, width, height);

  textAlign(CENTER, CENTER);
  text("CLICK to reopen\nsecond window", width/4, height/2);

  fill(0);
  textAlign(CENTER, CENTER);
  text("CLICK to close\nsecond window", 3*width/4, height/2);
}

void mousePressed() {
  println("mousePressed in primary window");
  if (mouseX >= 0 && mouseX < width/2) win.makeVisible(true);
  else win.makeVisible(false);
} 

@Override 
  void exit() {
  win.dispose();
  super.exit();
}


class PWindow extends PApplet {
  static final String RENDERER = P2D;
  PFont TXTfont;
  Button b;

  PWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }

  void settings() {
    size(500, 200, RENDERER);
  }

  void setup() {
    background(0);
    setDefaultClosePolicy(this, false);
    TXTfont = createFont("Arial", 13);
    b = new Button("CLOSE", width/2, height/2, 50, 30, this);
  }

  void draw() {
    fill(240);
    b.display();
    println("running PWINDOW!", frameCount);
  }

  void mousePressed() {
    println("mousePressed in secondary window");
    if (b.over()) {
      println("BUTTON CLICKED");
      makeVisible(false);
    }
  }


  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);
    }
  }

  public void makeVisible(boolean state)
  {
    surface.setVisible(state);
  }
}

Button class:

class Button {

  String name;
  color defaultColor, rectColor, overColor;
  float xpos, ypos, rectWidth, rectHeight, roundedCornerSize;
  PFont buttonFont;
  color c2;
  PApplet parent;

  Button(String _name, float _xpos, float _ypos, float _rectWidth, float _rectHeight, PApplet parent) {
    this.parent = parent;
    this.name = _name;
    this.xpos = _xpos;
    this.ypos = _ypos;
    this.rectWidth = _rectWidth;
    this.rectHeight = _rectHeight;
    this.roundedCornerSize = 5;
    this.buttonFont = createFont("Calibri", 15);
  }

  // Display the button
  void display() {   
    parent.rectMode(CENTER); //Draw the rectangle representing the button
    if (over()) {
      c2=color(222, 242, 252);
    } else {
      c2=color(240, 240, 240);
    }

    parent.noStroke();               
    parent.fill(c2); 
    parent.rect(xpos, ypos, rectWidth, rectHeight, roundedCornerSize);

    // Place the name of the button 
    parent.fill(0);
    parent.textFont(buttonFont);
    parent.textAlign(CENTER, CENTER);
    parent.text(this.name, this.xpos, this.ypos-2);
  }  

  // Function to determine if mouser is over the button
  boolean over() {
    return (parent.mouseX >= xpos-rectWidth/2 && parent.mouseX <= xpos+rectWidth/2 && 
      parent.mouseY >= ypos-rectHeight/2 && parent.mouseY <= ypos+rectHeight/2) ;
  }
}

Thanks in advance!
Any improvements to the code are also welcome :slight_smile:
I am trying to learn as much as possible on this new topic

Best regards!

as this has not been answered I would just like to post a solution I found on stackOverflow

Frame frame = ( (SmoothCanvas) ((PSurfaceAWT)surface).getNative()).getFrame();
    frame.dispose();

and here is a complete solution with window creation and closing

be sure to place the closeWindow() method somewhere sensible.

import java.awt.Frame;
import processing.awt.PSurfaceAWT;
import processing.awt.PSurfaceAWT.SmoothCanvas;
PWindow win;

public void settings() {
  size(690, 700);
  
};

void setup() { 
  win = new PWindow(width-5,0,width,700,"Second Window");
  surface.setLocation(-5, 0);
};

void draw() {
  background(255, 0, 0);
  fill(255);
  rect(10, 10, frameCount, 10);
};

void mousePressed() {
  println("mousePressed in primary window");
};  
class PWindow extends PApplet {
  int x,y,w,h;
  boolean setLocation,setTitle,makeResizable;
  String title;
  
  PWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  };
  
  PWindow(int x_,int y_) {
    super();
    x = x_;
    y = y_;
    setLocation = true;
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  };
  
  PWindow(int x_,int y_,int ww, int hh) {
    super();
    x = x_;
    y = y_;
    w = ww;
    hh = hh;
    
    setLocation = true;
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  };
  
  PWindow(int x_,int y_,int ww,int hh,String s) {
    super();
    x = x_;
    y = y_;
    w = ww;
    h = hh;
    setLocation = true;
    title = s;
    setTitle = true;
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  };
  
  PWindow(int x_,int y_, String s, boolean k) {
    super();
    x = x_;
    y = y_;
    setLocation = true;
    title = s;
    setTitle = true;
    makeResizable = true;
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  };
  

  void settings() {
    if(w>0&&h>0)size(w, h);
    else size(500, 200);
  };

  void setup() {
    background(150);
    if(setLocation)surface.setLocation(x, y);
    if(setTitle)surface.setTitle(title);
    if(makeResizable)surface.setResizable(true);
  };

  void draw() {
    ellipse(random(width), random(height), random(50), random(50));
  };

  void mousePressed() {
    println("mousePressed in secondary window");
    closeWindow();
  };
  
  void closeWindow(){
    Frame frame = ( (SmoothCanvas) ((PSurfaceAWT)surface).getNative()).getFrame();
    frame.dispose();
  };
};
3 Likes