How do I change the size of the display window?

So, I was tampering around with the “setResizable” feature in Processing. I was pretty satisfied with the results, when combined with the “scale” function and the “PMatrix” class, I can basically resize the entire sketch while it’s running without changing anything.

The problem is, at least within the code, I can’t seem to change the window size. I can still move my mouse to the edge of the window, and once my cursor becomes an arrow, click and drag to change the dimensions of the window. But, I need to be able to change it within the code!

I tried using “setSize(”, but it didn’t really seem to do anything. It changed the size of the PGraphics that gets projected onto the window, but didn’t change the size of the window itself. The minimize, maximize, and close buttons were still in the same place, the thin gray rectangle surrounding the sketch was still the same size. Other than cutting off the sketch display at a certain point and having everything after that point be the same color as the background, I don’t really see what was accomplished.

If anyone could give me a hand on this one, I’d greatly appreciate it. Thanks!

int w, h;
boolean change;

void setup() {
  size(500, 500);
  w = width;
  h = height;
  background(255);
  surface.setResizable(true);
}

void draw() {
  surface.setSize(w, h);
  if (w > 500) change = true;
  if (!change) w++; 
  else w--;
  if (w < 400) change = false;
}
1 Like

But that doesn’t change the size of the window itself. Just the canvas it draws on :confused:

Then you didn’t run the code. It changes the window with borders;

1 Like

That doesn’t really change anything, though. It changes what it says the dimensions are, but not the actual dimensions themselves. :confused:

So, I set the canvas’s dimensions to 545,675 in the settings function, and setResizable(true) in the setup function. I also made it so it prints the width & the height every frame, just for demonstration.

Here’s what happens if I call setSize(600,700) within the void draw():

And here’s what happens if I don’t:

I apologize for the text not being easily readable on the upload quality, but the first one says “width=500, height=700” and the second says “width=545, height=675”. The system thinks that the canvas has changed size, but as we can see, the window is the exact same size.

Okay, I feel really, really, really dumb. For some reason, I didn’t notice that your code says “surface.setSize(”, as opposed to just “setSize(”. I apologize for not listening, and thank you for the help so far.

With that said, this problem isn’t completely fixed just yet. There is a bug in the setSize function that seems to, on occasion, set dimensions that are completely different than what I asked for in the code. It’s hard to explain what’s going on, and I actually don’t have time at the moment to perform tests to make sure I didn’t just set the numbers wrong. However, when I get the time, if I determine that it’s just something on my end, I’ll be sure to mark your answer as the solution. If not, I’ll be sure to clarify what happened in a followup comment and see if we (or someone else, you don’t necessarily have to help if you don’t want) can figure out what’s going on.

Thanks!

Well, I made a video and now I will upload it anyway.

ezgif.com-gif-maker

1 Like

Yup, that’s the same results I got when I copied over your code. I was so confused why your code worked and mine didn’t. :blush: Also, out of curiosity, what object was the setSize acting on when I didn’t put the “surface .” before it? Just for curiosity’s sake.

Not sure, maybe on the canvas frame. see line 489 here.

The difference between setSize() and surface.setSize()

The following discussion applies to Processing sketches using Java mode only.

There are two parts to the sketch

  1. The window - this is a java.awt.JFrame which as the name suggest is a simple window frame with a title bar and close icon which can hold content. This is referenced by the variable surface
  2. The canvas - this is the drawing surface and its starting size is fixed with the size(w, h) statement found in the setup() or settings() method. It is an object of type java.awt.Canvas

When a processing sketch is executed it creates the canvas using the values from size(w, h) and a JFrame whose size is just big enough to surround the canvas.

Now in Java awt/Swing you can change the size of either the window or the canvas without changing the size of the other component. In Processing this is not the case, if you modify the frame size with surface.setSize() then the canvas is resized but not the the other way round,

Now back to the final problem - although setSize() does not change the window size it does change the variables width and height

So in this sketch you would expect the green rectangle to fill the display it doesn’t. I suspect that this is a side-effect caused by software updates and although not desirable behaviour is easily avoided.

void setup() {
  size(600, 600);
  surface.setResizable(true);
  setSize(500, 400); // Does not resize window
}

void draw() {
  background(255, 200, 0);
  fill(0, 192, 0);
  // expect this next line to fill display with green rectangle
  rect(0, 0, width, height);
  // but it doesn't
}
4 Likes

Okay, so I’ve done some further experimentation, and yeah…it’s not just my code that’s flopping up here.

void setup() {
  size(300,400);
  surface.setResizable(true);
}

void draw() {
  background(0);
  surface.setSize(300,400);
}

If I run this sketch, the code will make a perpetual effort to keep the size at 300,400, no matter how much I resize things. Unfortunately, this effort appears to be in vain. Observe:

Keep in mind, I did nothing special here. I simply clicked and dragged to expand the window. Every time I do this, the moment I release the mouse button, it fails to rescale back to 300,400.

I should also note that, when I tried having it println the dimensions, it was still convinced the width and height was 300,400. So that’s great…

(It’s also worth noting that my end goal is to create a program that I can resize, but who’s width to height ratio is always constant.)

I tried your code and when I used the mouse to resize the window it always went back to 300x400 whenever I stopped dragging or released the mouse button. (I was running it on a iMac)

I do suggest you modify the program to avoid changing the window size while its trying to render the frame. Try this

int w, h;

void setup() {
  size(300, 400);
  w = width;
  h = height;
  surface.setResizable(true);
  registerMethod("pre", this);
}

void pre() {
  if (w != width || h != height) {
    surface.setSize(w, h);
  }
}

void draw() {
  background(255, 200, 0);
  fill(0, 200, 0);
  rect(0.25 * width, 0.25 * height, 0.5* width, 0.5 * height);
}

No luck :disappointed:

I should note, I’m doing this on Windows 10.

Also worth noting, when I change the window size with my mouse, the window bugs out graphically until I stop resizing it. This happens regardless of if I call the setSize() function.

Perhaps someone else can try in o Windows 10 and see if they get the same result.

I could send a video if you want?

Okay, so as it turns out, what was tripping me up was the “snap windows” feature in Windows 10.

Once I disabled this feature, the program worked exactly as you said it would. Granted, it still had those graphical glitches I described, but still.

I assume you mean that the windows ‘flickers’ If that is the case then try this code - on my machine gave good results unless you resized the window quickly.

int w, h;

void setup() {
  size(300, 400);
  w = width;
  h = height;
  surface.setResizable(true);
}

void mouseEntered() {
  if (w != width || h != height) {
    surface.setSize(w, h);
  }
}

void mouseExited() {
  if (w != width || h != height) {
    surface.setSize(w, h);
  }
}

void draw() {
  background(255, 200, 0);
  fill(0, 200, 0);
  rect(0.25 * width, 0.25 * height, 0.5* width, 0.5 * height);
}

The above code is not perfect - sometimes the window does not resize after releasing the mouse but that is fixable but requires more sophisticated code.

Maintaining the aspect ratio is significantly harder to do well. I might have a go at producing a more complete example this afternoon. Watch this space.

1 Like

Cool! I didn’t know there was a mouseEntered or mouseExited function.

By any chance, is there any way the console can figure out if the mouse is being held down when it’s outside the PApplet console?

Neither I. I wonder why the request on taking it into the reference was denied.
It seems really useful.

1 Like

Plain Java seems not capable of doing this. You can find some tricks using overlays etc, but the correct way I think to do this is using JNA Here is also a Java lib.
Personally, I use AHK for Windows objects and dll access by launching it, as an in the background running app.