“loadImage() inside settings()” rule is unclear

The documentation on settings() says (emphasis added):

The settings() method runs before the sketch has been set up, so other Processing functions cannot be used at that point. For instance, do not use loadImage() inside settings(). The settings() method runs “passively” to set a few variables, compared to the setup() command that call commands in the Processing API.

Why? I have a sketch whose dimensions should be the dimensions of the image it interacts with. Instead of changing the hardcoded size() values in setup() every time I test a new image, I instead:

void settings() {
  my_image = loadImage("image.png");
  size(my_image.width, my_image.height);
}

This works well, as I expected, and seems to be the only to accomplish the task. So why does the documentation tells us not to do it?

Interesting that the right way of doing it…

PImage my_image;
void settings() {
  size(100, 100, P2D);
}
void setup() {
  surface.setResizable(true);
  my_image = loadImage("image.png");
  surface.setSize(my_image.width, my_image.height);
}
void draw() {
  image(my_image, 0, 0);
}

crashes for me when using P2D:

2018-05-21-164031_492x23_scrot

Using your approach @HGDoSNmPmz6fyxC works both for default and P2D modes (I’m on Linux)

If I delay resizing the sketch then it works fine:

PImage my_image;
void settings() {
  size(100, 100, P2D);
}
void setup() {
  surface.setResizable(true);
}
void keyPressed() {
  my_image = loadImage("image.png");
  surface.setSize(my_image.width, my_image.height);
}
void draw() {
  if (my_image != null) {
    image(my_image, 0, 0);
  }
}

Have you checked if there is already an open issue? This sounds like perhaps it should be reported. The code may have evolved out from under the docs, for example.

  1. https://Forum.Processing.org/two/discussion/18033/how-to-set-processing-to-automatically-detect-an-image-size-and-set-it-as-a-background#Item_1

  2. https://Forum.Processing.org/two/discussion/16705/null-pointer-exception-when-loading-image-in-settings-p3-1-1#Item_1

1 Like