We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › PGraphics2D and resizing
Page Index Toggle Pages: 1
PGraphics2D and resizing (Read 610 times)
PGraphics2D and resizing
Nov 10th, 2009, 8:12am
 
so, I posted a while ago, but got no responses. I'm hoping if I simplify my question, someone with some know how will be able to point out where I'm going wrong.

basic Idea, I have a PApplet in a Jframe. the PApplet resizes as the JFrame does. how this happens is that componentResized within the JFrame calls a method in the PApplet which sets size().

this method also re-instantiates several PGraphics2D objects that I used hold different images, text, lines, etc...

resizing generally works totally fine, but there's the occasion that I get a null pointer to one of my PGraphics2D objects when I try and draw them in the draw() method. also, and maybe more confusingly, the background color is sometimes changed to some random color when resizing...

is there anything inherently wrong with the way I'm using PGraphics2D or the way I'm calling size() when the Jframe is resized?

please help and thanks,
Seth
Re: PGraphics2D and resizing
Reply #1 - Nov 10th, 2009, 1:14pm
 
I found that in the source code for PApplet, in the size() comments it says to use setSize if you're not calling in setup or draw, but I still am having the same problem.
Re: PGraphics2D and resizing
Reply #2 - Nov 11th, 2009, 7:34am
 
So i'll post this, even though it doesn't seem anyone else runs into this problem because I know I'd appreciate someone else posting what they've found.

As I said before, I am using several PGraphics2D objects to hold visual collections of stuff. The problem was a NullPointerExceptions that was happening in draw() when I tried to use the image() method to draw these PGraphics2D objects directly. normally this handles fine, but when the PApplet's containing component is being resized frequently the exception seemed to be thrown eventually.

here's my guess and what is so far my successful solution. the call to the method to re-instantiate all my PGraphics2D objects was happening in the componentResized() method in the containing component (in my case a JPanel). I'm guessing that since that resizing method is called off the EventDispatchThread and the draw() method (as I understand from looking at the PApplet source code) is running on its own "Animation Thread", the draw method, every once in a while, was trying to draw the PGraphics2D Object during a time when another call to the resizing method (called on the EDT) was happening. so the PGraphics2D object wasn't quite ready yet.

Again this is all my best guest (and I'm not a concurrency expert). Here is my simple solution:

The issue is trying to make sure draw is never accessing anything that could be even instantaneously null. rather than directly drawing the PGraphics2D object in draw(), I copied each PGraphics2D object to a PImage directly after every call to the resizing method. Now draw is always referencing something that at least exists, whereas when it was drawing directly from PGraphics2D object, there may be a point during one of these function calls that draw could try to draw pg:

Code:
pg = new PGraphics2D();
pg.setParent(this);
pg.setPrimary(false);
pg.setSize(width, OFFSET);
pg.beginDraw();
//all the other drawing stuff
pg.endDraw();


While if you make sure that each time you get past endDraw() you copy the whole buffer to a PImage and then reference that PImage in draw, you should not ever be referencing something that is being set up still.

I know no one else showed any interest in this issue, and maybe it's because I'm not extremely articulate, but I do hope this helps someone or at least probes a "hey he's wrong" from someone who has more experience.
Page Index Toggle Pages: 1