error when I call loadPixels() after CreateGraphics(w,h) version 3.0

In Processing 3.0 I get an error when I call loadPixels() after CreateGraphics(w,h).

Some code like this works fine in v2.2.1.

PGraphics pg;

public void setup() {
  size(800, 600);
  pg = createGraphics(width, height); 

  pg.loadPixels();
  for (int i=0; i < pg.width*pg.height; i++) 
    pg.pixels[i] = i % 255;
  pg.updatePixels();

  background(pg);
}

But in version 3.0 returns me that:

Exception in thread "Animation Thread" java.lang.NullPointerException
    at processing.awt.PGraphicsJava2D.getRaster(PGraphicsJava2D.java:2710)
    at processing.awt.PGraphicsJava2D.loadPixels(PGraphicsJava2D.java:2734)
    at myTemplate.setup(myTemplate.java:23)
    at processing.core.PApplet.handleDraw(PApplet.java:2373)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1523)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

I don't understand why my P3 version is calling any AWT code: processing.awt.PGraphicsJava2D.loadPixels

Any idea?

Answers

  • edited October 2015 Answer ✓
    // forum.processing.org/two/discussion/12915/
    // error-when-i-call-loadpixels-after-creategraphics-w-h-version-3-0
    
    // 2015-Oct-09
    
    PGraphics pg;
    
    public void setup() {
      size(800, 600);
      noLoop();
    
      pg = createGraphics(width, height, JAVA2D);
    
      //Before P3, loadPixels() could replace beginDraw() entirely:
      pg.beginDraw();
    
      pg.loadPixels();
      for (int i = 0, dim = pg.width * pg.height; i != dim
        ; pg.pixels[i] = i++ % 0400 | #000000);
      pg.updatePixels();
    
      background(pg);
    }
    
  • Great !!! Thank you very much!!!

    The expressionpg.pixels[i] = i++ % 0400 | #000000is quite cryptic for me @-) . What does it mean?

  • edited October 2015 Answer ✓

    i++ % 0400 | #000000:

  • Thanks!!!! ^:)^

  • I do hate it when he does this. All he needed to do was add pg.beginDraw() but instead he had to rewrite the entire thing and ends up raising more questions than he answers.

    i think he's also introduced a bug....

  • Really thinks that, koogs? I do like this kind of response, it opens new ways for resolve a problem. I enjoy analyzing GoToLoop answers!!!

Sign In or Register to comment.