Problem with background(PImage)

edited June 2014 in JavaScript Mode

This code throws an error: "Uncaught Error using image in background(): PImage not loaded. "

PImage bgImage;      // display window image

void setup() {
  size(800, 800);  
  bgImage = createImage(width, height, RGB);
}
void draw() {
  background(bgImage);
}

I cant understand why or how to fix it... Anyone plz?

Answers

  • Answer ✓

    insert this at end ofg setup()

    bgImage.loadPixels();
    for (int i = 0; i < bgImage.pixels.length; i++) {
      bgImage.pixels[i] = color(0, 90, 102); 
    }
    bgImage.updatePixels();
    
  • edited June 2014

    Hi Chrisir, thanks for your answer. I have also fixed it by loading a transparent image. Your solution is as good. But I still dont understand why an empty image doesnt work, nor can I find any bug report of this one...

  • The original code doesn't throw any exceptions for me in both Processing versions: 2.0.2 & 1.5.1! ;;)

  • Answer ✓

    There was a thread, not so long ago, about this problem. The image isn't "empty", it is uninitialized. It is like trying to use an array of objects without filling it, etc.

  • Ok thanks PhiLho.

Sign In or Register to comment.