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 & HelpSyntax Questions › PGraphics...am I doing something wrong here
Page Index Toggle Pages: 1
PGraphics...am I doing something wrong here? (Read 544 times)
PGraphics...am I doing something wrong here?
Oct 22nd, 2006, 12:17am
 
Using 0118 Beta.
I'm trying out the PGraphics class for the first time.  I'm trying something very simple here.  I'm drawing to PGraphics every frame and then drawing PGraphics to the sketch window using image().


Quote:


PGraphics pg;
int x=0;

void setup() {
 size(200, 200);
 
 pg = createGraphics(width, height, P2D);
 frameRate(1);
}

void draw() {
 image(pg, 0, 0);
 
 pg.beginDraw();
 pg.stroke(255);
 pg.rect(x++, 25, 10, 10);
 pg.endDraw();
 
}





I get the following error, and the line with the 'rect' command is highlighted by Processing:

java.lang.NullPointerException

at processing.core.PGraphics2D.thin_pointAtIndex(PGraphics2D.java:1173)

at processing.core.PGraphics2D.thin_flat_line(PGraphics2D.java:1284)

at processing.core.PGraphics2D.rectImpl(PGraphics2D.java:716)

at processing.core.PGraphics.rect(PGraphics.java:1266)

at Temporary_7691_9576.draw(Temporary_7691_9576.java:16)

at processing.core.PApplet.handleDisplay(PApplet.java:1311)

at processing.core.PGraphics.requestDisplay(PGraphics.java:568)

at processing.core.PApplet.run(PApplet.java:1406)

at java.lang.Thread.run(Thread.java:552)

java.lang.NullPointerException

at processing.core.PGraphics2D.thin_pointAtIndex(PGraphics2D.java:1173)

at processing.core.PGraphics2D.thin_flat_line(PGraphics2D.java:1284)

at processing.core.PGraphics2D.rectImpl(PGraphics2D.java:716)

at processing.core.PGraphics.rect(PGraphics.java:1266)

at Temporary_7691_9576.draw(Temporary_7691_9576.java:16)

at processing.core.PApplet.handleDisplay(PApplet.java:1311)

at processing.core.PGraphics.requestDisplay(PGraphics.java:568)

at processing.core.PApplet.run(PApplet.java:1406)

at java.lang.Thread.run(Thread.java:552)
Re: PGraphics...am I doing something wrong here?
Reply #1 - Oct 22nd, 2006, 6:55am
 
I think there are still some bugs to work out with createGraphics()/2D renderer. However, P3D will get the job done. You also needed a background call.

Code:

PGraphics pg;
int x=0;

void setup() {
size(200, 200);
pg = createGraphics(width, height, P3D);
frameRate(1);
}

void draw() {
pg.background(0);
pg.beginDraw();
pg.stroke(255);
pg.noFill();
pg.rect(x++, 90, 10, 10);
pg.endDraw();
image(pg, 0, 0);
}


-ira
Re: PGraphics...am I doing something wrong here?
Reply #2 - Oct 22nd, 2006, 8:04am
 
Cool Thanks!  How come I need to use background()?  I tried it without using background() and it seems like nothing draws on the PGraphics object.  What if I wanted to draw trails?  
ex:
pg.line(pmouseX, pmouseY, mouseX, mouseY);


(the workaround is that I guess I can save all the points and render all the lines for the trail on each frame)
Re: PGraphics...am I doing something wrong here?
Reply #3 - Oct 22nd, 2006, 7:56pm
 
I believe (alright I'm guessing–no time to go source diving today) the reason you need the background() call is to make the (PGraphics) graphics context opaque. Once you call
Code:
 
pg = createGraphics(width, height, P3D);

the PGraphics object has dimension, but it may still be transparent, prior to calling background(). Maybe Ben or someone else can confirm/correct this.

Regarding your question about trails, this should get the job done:
Code:

PGraphics pg;
float x=0;

void setup() {
size(200, 200);
pg = createGraphics(width, height, P3D);
// Any color arg in background()
// will actually get the job done here
pg.background(0);
}

void draw() {
background(0);
pg.beginDraw();
pg.stroke(255);
if (mousePressed){
pg.line(pmouseX, pmouseY, mouseX, mouseY);
}
pg.endDraw();
image(pg, 0, 0);
}


-ira
Re: PGraphics...am I doing something wrong here?
Reply #4 - Oct 23rd, 2006, 1:03am
 
Thanks Ira, that totally worked!  I was missing the magical pg.background() call.
Re: PGraphics...am I doing something wrong here?
Reply #5 - Oct 23rd, 2006, 3:45pm
 
the background() call should go *after* beginDraw().

the black background is because createGraphics with P3D is defaulting to an all black background (this should actually be transparent, i'll fix that in a future release).

we also need to fix that example in the reference because P2D doesn't exist yet, but we already have a bug filed for that. sorry for the confusion.
Page Index Toggle Pages: 1