# Dynamically Setting the Window as Undecorated

**URL:** https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327
**Category:** Coding Questions
**Created:** [November 5, 2021, 11:24am UTC](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327 "2021-11-05T11:24:42Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Brahvim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brahvim/32/14756_2.png) [@Brahvim](https://discourse.processing.org/u/Brahvim)
#### Post date: [November 5, 2021, 11:24am UTC](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327/1 "2021-11-05T11:24:43Z")

</div>

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!** _)

---

<div class="post-metadata">

### Author: ![Brahvim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brahvim/32/14756_2.png) [@Brahvim](https://discourse.processing.org/u/Brahvim)
#### Post date: [December 19, 2021, 10:20am UTC](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327/2 "2021-12-19T10:20:21Z")

</div>

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

```java
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 `int`s called `xbf` and `ybf`, and:

```java
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 😂~~.

---

<div class="post-metadata">

### Author: ![Brahvim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brahvim/32/14756_2.png) [@Brahvim](https://discourse.processing.org/u/Brahvim)
#### Post date: [December 19, 2021, 10:21am UTC](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327/3 "2021-12-19T10:21:01Z")

</div>

- 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 `if`s?!  
If you’ve ran this code, you know how long it takes to do the task! The `if`s 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](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327/5)**, ":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](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/10)  
**To know what it contains** , check the solution answer [here](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327/5)

---

<div class="post-metadata">

### Author: ![Brahvim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brahvim/32/14756_2.png) [@Brahvim](https://discourse.processing.org/u/Brahvim)
#### Post date: [December 19, 2021, 10:47am UTC](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327/4 "2021-12-19T10:47:43Z")

</div>

Here it is as promised, ":D!

```java
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-_

---

<div class="post-metadata">

### Author: ![Brahvim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brahvim/32/14756_2.png) [@Brahvim](https://discourse.processing.org/u/Brahvim)
#### Post date: [December 19, 2021, 10:57am UTC](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327/5 "2021-12-19T10:57:00Z")

</div>

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

**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:

> [@Getting A Sketch's Window's Position](https://discourse.processing.org/t/getting-a-sketchs-windows-position/31772/7):
>
> Just adding a note for people deciding on whether to go the getNative() route: not only is it a renderer specific Object – a whatever-the-renderer-gives-us – but it could be subject to change. This is whimsically expressed in the Processing API documentation… from [PSurface.getNative](http://processing.github.io/processing-javadocs/core/processing/core/PSurface.html#getNative--) : Get the native window object associated with this drawing surface. For Java2D, this will be an AWT Frame object. For OpenGL, the window. The data returned here is subject to the whims of the renderer, and using …

**The code, yay!:**

```java
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) {
  }
}

```

---

<div class="post-metadata">

### Author: ![Brahvim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brahvim/32/14756_2.png) [@Brahvim](https://discourse.processing.org/u/Brahvim)
#### Post date: [August 26, 2022, 12:02pm UTC](https://discourse.processing.org/t/dynamically-setting-the-window-as-undecorated/33327/6 "2022-08-26T12:02:49Z")

</div>

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!:

```auto
  // 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!_ 😂:

It’s been another hour!

# Here’s some test code!:

…for the copy-paste speedrunners in us 😂

```auto
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!:

```auto
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? 🕶

```auto
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());
}

```
