Multiple Windows: How to close/exit only one or get it to only create one

Hello! I am currently trying to create my own version of paint. I want my menus to be popup windows. I have two separate windows created for the two different menus. The problem I currently have is that 1) I don’t know how to get them to minimize without physically clicking the minimize button and 2) when I click ‘p’ or ‘m’ (the keys for each of the windows) it creates a whole new window so basically you end up with an infinite amount of windows. Hopefully that is not too confusing. I am open to any help, my code is below! Thanks! :grinning:

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

  void settings() {

    size(1000, 300);
  }

  void setup() {
    background(150);
  }

  void draw() {
    if (focused != true) {

      redraw();
    }
  }





  void mousePressed() {
     //launch("%windir%/explorer.exe shell:::{3080F90D-D7AD-11D9-BD98-0000947B0257}"); minimize
  
  }

  void keyPressed() {
    if (key == 'm' || key == 'M') {
      win = new menuWindow();
    }
  }
}



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

  void settings() {

    size(300, 800);
  }

  void setup() {
    background(150);
  }

  void draw() {
    if (focused != true) {

      redraw();
    }
  }


  void mousePressed() {
  }

  void keyPressed() {
    if (key == 'h' || key == 'H') {
      win2 = new homeWindow();
    }
  }
}




menuWindow win;
homeWindow win2;

public void settings() {
  fullScreen();
}

void setup() {
  win = new menuWindow(); //Opens main menu on start
  background(255);//Set background color
}

void draw() {
  //Left Brush
  if (mousePressed && mouseButton == LEFT) { 
    strokeWeight(8); //Sets brush thickness
    stroke(255, 0, 0); //Sets brush color
    line(mouseX, mouseY, pmouseX, pmouseY);
  }

  //Right Brush
  if (mousePressed && mouseButton == RIGHT) { // Rectangular Eraser
    strokeWeight(1); //Resets to default weight
    fill(255); //Sets fill to background color
    stroke(255); //Sets stroke to background color
    ellipse(mouseX, mouseY, 55, 55);
  }
}


void keyPressed() {

  //Opens Main Menu
  if (key == 'm' || key == 'M') {
    win = new menuWindow();
  }

  if (key == 'h' || key == 'H') {
    win2 = new homeWindow();
  }
}

To clarify it would be helpful if there was either a way to close the specific windows so that I can call a new one or if there is a way to call the already existing window.

Hi.

Your infinite window problem is because you always create a new window when a key is pressed. You override the variable (which seems o be something like a pointer) so the old window stays and a new one is opened an will then be referenced by the variable.

To solve this you only need to create the windows once initially:

menuWindow win = new menuWindow();
homeWindow win2 = new homeWindow();

Then when a key is pressed you need to show / hide them (dont ask me how it works in your case, this is just an example):

void keyPressed() {

  //Opens Main Menu
  if (key == 'm' || key == 'M') {
    win.show();
  }

  if (key == 'h' || key == 'H') {
    win2.show();
  }
}