Mmm, I looked in PApplet code, and I see various ways to do this. Theoretically, it should work but for some reason, we get this exception.
A possible solution is to set g to null before calling size(). A bit extreme, not sure if it has bad size effects.
Another way is to do the steps done in the right size() path:
setRendererSize(i, 400);
setSize(i, 400);
But the frame isn't resized. You have to call frame.setSize() but taking in account decorations.
If I paste code from internals of Processing:
frame.pack(); // get insets. get more.
Insets insets = frame.getInsets();
int windowW = Math.max(width, MIN_WINDOW_WIDTH) +
insets.left + insets.right;
int windowH = Math.max(height, MIN_WINDOW_HEIGHT) +
insets.top + insets.bottom;
frame.setSize(windowW, windowH);
the frame is correctly resized (along with the above code, of course).
My final test code looks like:
Code:int i = 200;
Insets insets;
void setup() {
size(200, 400);
background(100);
frame.pack(); // get insets. get more.
insets = frame.getInsets();
}
void draw() {
background(100);
line(0, 0, width, height);
line(0, height, width, 0);
}
void mousePressed() {
println("press");
i=i+50;
setRendererSize(i, 400);
setSize(i, 400);
int windowW = Math.max(width, MIN_WINDOW_WIDTH) +
insets.left + insets.right;
int windowH = Math.max(height, MIN_WINDOW_HEIGHT) +
insets.top + insets.bottom;
frame.setSize(windowW, windowH);
}