"size cannot be used here" / loading images in settings()

edited December 2017 in Questions about Code

Hi, Im executing this code and find this error: "size cannot be used here"

PImage img;
PImage picture;
Integer[] palette;

void setup(){
  img = loadImage("palette.jpg");
  palette = getPalette(img);
  picture = loadImage("picture.jpg");
  size(picture.width,picture.height);
  background(255);
}

void draw(){
  colour(picture, palette);
  noLoop();
} 
Tagged:

Answers

  • size is first in setup and can't contain calculations (ie picture.width or height).

    look into settings() in the reference.

  • Ive done this, and the message is the same

     PImage img;
    PImage picture;
    Integer[] palette;
    
    void setup(){
      size(200,200);
      img = loadImage("palette.jpg");
      palette = getPalette(img);
      picture = loadImage("picture.jpg");
      //size(picture.width,picture.height);
      background(255);
    }
    void draw(){
      colour(picture, palette);
      noLoop();
    } 
    
  • You read up on using settings(), but still aren't using settings()? Are you sure you read about it?

  • Ive tried this, but it displays a message saying that the size cannot be calculated in the code

     PImage img;
    PImage picture;
    Integer[] palette;
    int a;
    int b;
    void settings(){
      img = loadImage("palette.jpg");
      palette = getPalette(img);
      picture = loadImage("picture.jpg");
       a = picture.width; 
       b = picture.height;
    }
    void setup(){  
      size(a,b);
      background(255);
    }
    void draw(){
      int a = picture.width;
      int b = picture.height;
      println (a);
      println (b);
      colour(picture, palette);
      noLoop();
    } 
    
  • Answer ✓

    According to the PS3 documentation, the parameters for the size() method must only contain integer literals or integer constants. The parameters MUST not be variables or expressions (calculations).

    The settings() method is provided when using other IDEs e.g. Eclipse and is not used in the Processing IDE.

    You can use the surface.setSize() to do what you want like this

    PImage img;
    
    void setup() {
      size(100, 100);
      img = loadImage("shoot1.png");
      if (img != null) {
        surface.setSize(img.width, img.height);
      }
    }
    
    void draw() {
      if (img != null) {
        image(img, 0, 0);
      } else {
        background(200, 200, 255);
      }
    }
    
  • Thanks, Ill try

  • The settings() method is provided when using other IDEs e.g. Eclipse and is not used in the Processing IDE.

    @quark -- that's not quite right. settings() is normally not needed in Processing 3 PDE, but it works just fine in PDE, and it allows variables in size() (as setup() does not.)

    The example sketch from the settings documentation runs just fine in PDE with a dynamic size:

    int w = 200;
    int h = 200;
    int x = 0;
    
    void settings() {
      size(w, h);
    }
    
    void setup() {
      background(0);
      noStroke();
      fill(102);
    }
    
    void draw() {
      rect(x, 10, 1, 180); 
      x = x + 2;
    }
    
  • I am only quoting the PS3 documentation, so don't shoot the messenger :)

Sign In or Register to comment.