Dynamically Setting the Window as Undecorated

Just as the title says:
I want to be able to set the window as ‘undecorated’, while the sketch is running. It causes some errors inside “handleDraw()”, however.

I felt that it may be possible if I call noLoop(), wait for draw() to finish (by using a delay() or Thread.sleep()), and THEN make changes to the JFrame / GLWindow.

Apparently that doesn’t work either!

There doesn’t seem to be a way to know when EXACTLY draw() / handleDraw() finishes, either.

I tried to registerMethod("post", this);, but the post() method doesn’t seem to help either.

Is it just not possible…? Should I return to simply setting the surface object’s size and location…?

(Please be aware that my knowledge of the Processing source code happens to be quite limited!)

YAYY! I found a solution , ":D!
(Another problem though :upside_down_face:, How to type an ASCII emoji on Discourse? :grin:)

void post() {
  if (fs) {
    if (!window.isUndecorated()) {
      try {
        window.setVisible(false);
        window.setUndecorated(true);
        window.setVisible(true);
      }
      catch(Exception e) {
      }
      while (!window.isUndecorated()) {
      }
    } else {
      surface.setLocation(0, 0);
    }
  } else { 
    if (window.isUndecorated()) {
      try {
        window.setUndecorated(false);
      }
      catch(Exception e) {
      }
    }
  }
}

What is fs? It’s a boolean, for which you also need to do this:

  • Make two ints called xbf and ybf, and:
void keyPressed() { 
  try {
    if (keyCode == 107) {
      fs = !fs;
      if (fs) {
        xbf = window.getWidth();
        ybf = window.getHeight();
        surface.setSize(displayWidth, displayHeight);
        surface.setLocation(-8, 0);
      } else {
        surface.setSize(xbf, ybf);
        surface.setLocation(displayWidth/2 - xbf/2, displayHeight/2 - ybf/2 );
      }
    }
  }
  catch(Exception e) {
  }
}

Of course you need this inside setup():
registerMethod("post", this);

I will post an explanation as to how this works soon~!
Also, an optimized version - totally not for myself to copy and paste :joy:.

  • What even IS this/?/1!?1?
    So you know how video games (like Minecraft!!1!) resize the window to ‘fullscreen’ when you press F11? This does that. Setting the window to “undecorated” means to remove the title bar, the window maximize, window minimize, and window close buttons.

  • How did you do it, ("o_O))?
    Right: here is an explanation as to how this works:
    Basically, the window.setUndecorated() method runs a new thread. If you continue to use draw(), you are rendering graphics to the screen, which disrupts, or interrupts that thread.
    To avoid that, I simply put a while loop to wait till the thread is done.
    The if exists to check if the window was already set to undecorated, and if it needs to be done again, since it takes a bit of time to set the window as undecorated!

  • Why do I set the window to invisible and then visible?
    Because for some reason, it resizes it perfectly to fit the screen, and also makes the taskbar disappear. It is needed even though there is a surface.setLocation(0, 0);

  • Why am I using a method called ‘post()’ and what is it?!
    Processing “handles” the draw() function inside a function called handleDraw(), which runs every frame. However, to ensure that the draw() function is ‘safe’ to use (so you didn’t change things and cause random errors all the time), the Processing development team made sure that you couldn’t access them. Problem: libraries need this functionality, and so do we! So they allowed it to be extended and made two more methods, called pre() and post(), which are called before and after draw() is executed, respectively.

  • Why so many ifs?!
    If you’ve ran this code, you know how long it takes to do the task! The ifs ensure that the task isn’t done every single frame - it’s an optimization. But the solution as a whole isn’t as optimized.
    I’ll post the ‘optimized’ version of the code soon!
    Check the solution answer here
    , ":D!

  • What’s this “window” object?!
    This is a way to dig into Processing’s workings and do some of this stuff. I myself came to know about it from here, months ago: Get location of a PApplet window - #10 by micycle
    To know what it contains, check the solution answer here

1 Like

Here it is as promised, ":D!

void post() {
  if (fs)
    if (!window.isUndecorated()) {
      window.setVisible(false);
      try {
        window.setUndecorated(true);
      }
      catch(Exception e) {
      }
      window.setVisible(true);
      while (!window.isUndecorated());
    } else surface.setLocation(0, 0);
  else if (window.isUndecorated())
  try {
    window.setVisible(false);
    window.setUndecorated(false);
    window.setVisible(true);
  } 
  catch(Exception e) {
  }
}

Again, I’m setting the window as invisible and visible again. This is to remove the ‘blur’ that I forgot to notice before-

1 Like

Moment of truth.
Here is the last revision, I hope! :joy:

I’m sad to say this, but this only supports the P2D and P3D renderers!
Pretty sure most people will be using GPU-based rendering anyway, amIrite?

Also, there is bad luck involved…
In short, this could change with newer versions of Processing, …and:

The code, yay!:

import com.jogamp.newt.opengl.GLWindow;
// [https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/opengl/GLWindow.html];

GLWindow window;

void setup() {
  size(640, 480, P3D);
  registerMethod("post", this); // Never forget the "this dot", and also this!!11!

  sphereDetail(8);
  surface.setResizable(true);
  surface.setTitle("haha press f11");
  window = (GLWindow) surface.getNative();
}


void draw() {
  background(0);

  stroke(255);
  strokeWeight(8);
  line(
    map(window.getX(), 0, displayWidth, 0, width*1.75), 0, 
    map(window.getX(), 0, displayWidth, 0, width*1.75), 
    map(window.getY(), 0, displayHeight, 0, height*2.5)
    );


  if (mousePressed) stroke(255f, 0, 0);
  strokeWeight(1f);
  noFill();
  lights();
  translate(mouseX, mouseY);
  rotateX(millis()*.001f);
  rotateZ(millis()*.001f);
  sphere(50f);
}





boolean fs;
void keyPressed() {
  if (keyCode == 107) {
    fs = !fs;
    surface.setSize(displayWidth, displayHeight);
    if (!fs) surface.setSize(640, 480);
  }
}

void post() {
  if (fs)
    if (!window.isUndecorated()) {
      window.setVisible(false);
      try {
        window.setUndecorated(true);
      }
      catch(Exception e) {
      }
      window.setVisible(true);
      while (!window.isUndecorated());
    } else surface.setLocation(0, 0);
  else if (window.isUndecorated())
  try {
    window.setVisible(false);
    window.setUndecorated(false);
    window.setVisible(true);
  } 
  catch(Exception e) {
  }
}

1 Like

I, a few days ago, discovered some new things I wish I had mentioned sooner…

As I was writing this, I was only editing the older code with newer functionality, but,
…apparently it’s possible to ditch that all and do this with literally two lines of code!:

  // The two golden lines!:
  window.setFullscreen(fullscreen);
  while (fullscreen? !window.isFullscreen() : window.isFullscreen());

  // Note: I had this code before the two lines as well:
  //pfullscreen = fullscreen;

  // I was also wrapping the two lines in an `if`:
  //if (pfullscreen != fullscreen)

  // I removed this check since it was adding a little bit of delay and
  // also took away benefit number `3`, as I will now list in the post.

Again, make sure to place all of that in post()!

Write registerMethod("post", this); in setup()!



There are benefits to using this method which I also just tested:

  • MUCH faster!
  • Welcome to true fullscreen. the older method allowed you to use the Meta + Up Arrow and Meta + Down Arrow keys to adjust the window a little bit here and there. PS the ‘Meta’ key is the same as ‘Windows key’.
  • You can spam press F11, and your sketch won’t freeze!
  • The window now retains its previous position and dimensions when exiting fullscreen.

Edit made 5 hours later, the same day or whatever! :joy::

It’s been another hour!

Here’s some test code!:

…for the copy-paste speedrunners in us :joy:

import com.jogamp.newt.opengl.GLWindow;
// [https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/opengl/GLWindow.html];

GLWindow window;
boolean fullscreen;

// You NEED this!:
void settings() {
  size(640, 480, P3D);
}

void setup() {
  registerMethod("post", this);

  surface.setResizable(true);
  window = (GLWindow)surface.getNative();
}

void draw() {
  background(abs(sin(millis() * 0.001f)) * 255); // Delete this!
}

void keyPressed() {
  if (keyCode == 107)
    fullscreen = !fullscreen;
}

void post() {
  window.setFullscreen(fullscreen);
  while (fullscreen? !window.isFullscreen() : window.isFullscreen());
}

Here’s how I want to explain it:

I deeply, deeply, very deeply encourage you to read this!:

import com.jogamp.newt.opengl.GLWindow;
// [https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/opengl/GLWindow.html];

GLWindow window;
boolean fullscreen, pfullscreen;
// ^^^ `pfullscreen` is used to track when an update occurs.
// Please take a look at the very end of this code snippet!

void setup() {
  size(640, 480, P3D);
  registerMethod("post", this);

  sphereDetail(8);
  surface.setResizable(true);
  surface.setTitle("`F11` - A Story:");
  window = (GLWindow)surface.getNative();
}


void draw() {
  // "Why not give some more code away?" - Brahvim ";D!:
  hint(DISABLE_DEPTH_TEST);
  fill(0, 40);
  rect(0, 0, width, height);
  hint(ENABLE_DEPTH_TEST);

  // "Why not give those without knowledge of 3D graphics what they would like?":

  // I want my graphics to scale with the screen's size.
  // My eyes are `60` (`PI / 3`: `2 * PI` AKA `TAU` is a full `360`) degrees wide,
  // ...and I'm giving you the aspect ratio of this screen.
  // If something has a depth less than `0.01`, don't draw it.
  // If it goes above ten thousand, don't draw it then either!
  perspective(PI / 3, (float)width / (float)height, 0.01f, 10000);


  // Here, I tell your eyes how my world is:
  camera(
    // This is where the eyes stand:
    width / 2, height / 2, -400, 

    // They look at this beautiful point my universe:
    width / 2, height / 2, 0, 

    // This is the direction of our sky, my guy!:
    0, 1, 0);

  stroke(255);
  strokeWeight(8);
  line(window.getX(), 0, window.getX(), 400);

  // Oh! And I use actual conventions now, by the way. 
  // `p_` for parameters... `f` after all `float`s... livin'-my-life!
  line(map(window.getX(), 0, displayWidth, 0, width * 1.75f), 0, 
    map(window.getX(), 0, displayWidth, 0, width * 1.75f), 
    map(window.getY(), 0, displayHeight, 0, height * 2.5f));


  if (mousePressed) 
    stroke(255, 0, 0);
  strokeWeight(1);

  noFill();
  lights();

  // Thanks to OpenGL Mathematics, our graphics now fit
  // every screen!

  // However, this separates our world and the screen's 
  // coordinate systems. `mouseX` and `mouseY` don't
  // put objects in the right places!

  // After having spent months on trying to solve
  // this problem, I have found that...

  // The truly working fix is this:
  // [http://andrewmarsh.com/blog/2011/12/04/gluunproject-p3d-and-opengl-sketches/]

  // Modify it a bit:

  //perspective(FOV, ASPECT, I_CALL_THIS_THE_MOUSE_Z_VALUE, FAR_PLANE_DEPTH);
  //camera(); // You put what you want here!

  //classFromSirAndrewMarsh.captureViewMatrix((PGraphics3D)g);

  // `0.9f`: at the near clipping plane.
  // `0.9999f`: at the far clipping plane.

  // If you the second comment on that post, the `Y` needs to
  // be inverted:
  //classFromSirAndrewMarsh.gluUnProject(mouseX, height - mouseY, 
  ////0.9f + map(mouseY, height, 0, 0, 0.1f),
  //0, PVector_that_stores_the_result);

  // Now, you call the function with the arguments you want!:
  //perspective(FOV, ASPECT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH);

  // Here's one of the tricks I discovered super early into trying 
  // to solve the problem.

  // Note: it only tries to approximate where the mouse is. 
  // It COMPELTELY *breaks* in fullscreen:
  translate(
    map(mouseX, 0, width, width, 0), 
    map(mouseY, 0, height, 0, height));

  // Alright! Time for our beautiful graphics!~
  rotateX(millis() * 0.001f);
  rotateZ(millis() * 0.001f);
  sphere(50);
  // ...that was just an example, what did you expect? :joy:
}

void keyPressed() {
  // Minecraft uses `F11`:
  if (keyCode == 107)
    fullscreen = !fullscreen;

  // Most games will use `Alt + Enter`.
  // Here's how to implement it!:

  // `PApplet` has a list (`ArrayList`..?)
  // called `pressedKeys` inside, but it is `private`,
  // ..and I don't want to play around with reflection, so...

  // Declare this in the global scope (AKA before `setup()`):
  //ArrayList<Integer> keysHeld = new ArrayList<Integer>(3);

  // In `keyPressed()`:
  //keysHeld.add(keyCode);

  //In `keyReleased()`:
  //try {
  //  keysHeld.remove(keysHeld.indexOf(keyCode));
  //}
  //catch(IndexOutOfBoundsException ioobe) {
  //}

  // Make this cool new function!:
  //boolean keyIsPressed(int p_keyCode) {
  //return keysHeld.contains(p_keyCode);
  //}

  // ...sorry if you don't like my conventions (such as `p_`).


  // ..and here's the code that remains inside this function!:
  //if (keyIsPressed(ALT) && keyCode == ENTER)
  //fullscreen = !fullscreen;

  // Remember: the right-side `Alt` key, `AltGr`,
  // AKA "Alt-Graph", is different. You might want
  // to import in `java.awt.event.KeyEvent`, and use:
  // `KeyEvent.VK_ALT_GRAPH`, ...or both
  // `KeyEvent.VK_ALT` and.... 'alt-graph'.
}


// The gold we all were looking for. Right here:
// :)
void post() {
  window.setFullscreen(fullscreen);
  while (fullscreen? !window.isFullscreen() : window.isFullscreen());

  //pfullscreen = fullscreen;
  //if (pfullscreen != fullscreen)
  //windowResizedFunction();

  // PS The JogAmp documentation probably states ways to get a callback, 
  // but we should stick to doing this. No idea what things, 
  // the Processing developers might do.
}

The Alt + Enter speedrun version:

Copy-pasting at it’s finest, eh? :sunglasses:

import com.jogamp.newt.opengl.GLWindow;
// [https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/opengl/GLWindow.html];

GLWindow window;
boolean fullscreen;
ArrayList<Integer> keysHeld = new ArrayList<Integer>(3);

void settings() {
  size(640, 480, P3D);
}

void setup() {
  registerMethod("post", this);
  surface.setTitle("`Alt` + `Enter` for the win!");

  surface.setResizable(true);
  window = (GLWindow)surface.getNative();
}

void draw() {
  background(abs(sin(millis() * 0.001f)) * 255);
}

void keyPressed() {
  keysHeld.add(keyCode);
  if (keyCode == 107)
    fullscreen = !fullscreen;
  if (keyIsPressed(ALT) && keyCode == ENTER)
    fullscreen = !fullscreen;
}

void keyReleased() {
  try {
    keysHeld.remove(keysHeld.indexOf(keyCode));
  } 
  catch(IndexOutOfBoundsException ioobe) {
  }
}

boolean keyIsPressed(int p_keyCode) {
  return keysHeld.contains(p_keyCode);
}

void post() {
  window.setFullscreen(fullscreen);
  while (fullscreen? !window.isFullscreen() : window.isFullscreen());
}
2 Likes