Loading...
Logo
Processing Forum
I have some code that draws stuff in a PGraphics.
When i do:

Copy code
  1.       println(preProcessed.pixels == null);
It always prints true. And when i try to acces the pixels i get a nullpointerException...
But displaying the pgraphic goes without a problem.
(processing 2.0.1).

I tried to isolate the problem and did found another strange bug.
When i use updatePixels things shift down:




do more people have this?
      

Copy code
  1. void setup() {
  2.   size(400, 400, OPENGL);
  3.   
  4.   PImage img = loadImage("blob_testImage.png");
  5.   
  6.   PGraphics pg = createGraphics(200, 200, P2D);
  7.   
  8.   println(pg.pixels == null);

  9.   pg.beginDraw();
  10.   pg.ellipse(100, 100, 100, 100);
  11.   pg.image(img.get(0, 0, 100, 100), 0, 0);
  12.   pg.endDraw();
  13.   
  14.   //pg.updatePixels();
  15.   
  16.   println(pg.pixels == null);

  17.   image(pg, 0, 0);

  18.   println(pg.pixels[100]);
  19. }

Replies(5)

to give a bit more detail about how strange it is.

I have this in my large script:
( tableBounds width and height is 255 and 243 for example, i double checked this) 
Copy code

  1.  preProcessed = createGraphics(tableBounds.width(), tableBounds.height(), P2D);
  2.       preProcessed.beginDraw();
  3.       preProcessed.endDraw();
  4.       println("preProcessed pixels null? :"+(preProcessed.pixels == null));

which prints true.
Then i have this:

  1. PGraphics preProcessed;


  2. void setup() {
  3.   size(400, 400, OPENGL);

  4.   preProcessed = createGraphics(255, 243, P2D);
  5.   preProcessed.beginDraw();
  6.   preProcessed.endDraw();
  7.   println("preProcessed pixels null? :"+(preProcessed.pixels == null));
  8. }
which prints false!
Sorry for spamming. This is so important for me. It can be the difference between graduating this or next year.

Anyway i found it. In setup it's ok. In draw it's broken:

Copy code
  1. PGraphics preProcessed;


  2. void setup() {
  3.   size(400, 400, OPENGL);

  4.   preProcessed = createGraphics(255, 243, P2D);
  5.   preProcessed.beginDraw();
  6.   preProcessed.endDraw();
  7.   println("preProcessed pixels null? :"+(preProcessed.pixels == null));
  8. }

  9. void draw() {
  10.   preProcessed = createGraphics(255, 243, P2D);
  11.   preProcessed.beginDraw();
  12.   preProcessed.endDraw();
  13.   println("preProcessed pixels null? :"+(preProcessed.pixels == null));
  14. }

Something I've learned very early in Processing when directly manipulating 
a PImage/ PGraphics  is to use loadPixels() 1st.
And also an  updatePixels() just to be sure! Even  though they're not always necessary!
Indeed. loadPixels() is here for a reason: in JAVA2D drawing, and in OpenGL too, graphics are hidden by the graphics library (Sun's Java2D stuff or OpenGL stuff), so if you want to access the pixels, you need to load them explicitly (a slow operation!)
updatePixels() allows to push back the modified pixels to the underlying graphics system.

In old P2D / P3D drawings (pre-2.0), they were perhaps not necessary (at least in Java mode), because they worked directly on a pixels array, but as GoToLoop said, it is safer to always use them when you need to access pixels[].
yeah i'm to used to pre-2.0 i guess since i hardly used it there.