Processing resizing and changing sketch to and from fullScreen() mid sketch

This might not sound useful but It took me a some time to get this working so I figured someone might find it useful.

Here is the code to be able to resize the sketch and switch it to and from fullscreen while it is running:

void setup(){
   fullScreen();

   surface.setResizable(false);
   surface.setAlwaysOnTop(true);
}

// Sets the x and y position of the screen aswell as the width and height
void screen(int x, int y, int w, int h){
  surface.setLocation(x, y);
  surface.setSize(w, h);
}

//Makes sketch fullscreen
void fullscreen(){
  surface.setLocation(0, 0);
  surface.setSize(displayWidth, displayHeight);
}

Example code:

void setup(){
  fullScreen();
  
  surface.setResizable(false);
  surface.setAlwaysOnTop(true);
  
  screen(200, 200, 400, 400); // Sketch is windowed
}

void screen(int x, int y, int w, int h){
  surface.setLocation(x, y);
  surface.setSize(w, h);
}

void fullscreen(){
  surface.setLocation(0, 0);
  surface.setSize(displayWidth, displayHeight);
}

void draw(){
  if (frameCount < frameRate * 4){
    screen(200 + int(200 * (frameCount / (frameRate * 4))), 200, 400, 400); // Window is moving right
  }
  else if (frameCount >= frameRate * 4 && frameCount <= frameRate * 4 + 10) {
    fullscreen();
  }
}

Tested on windows processing 3.5.3

1 Like