Processing 3 and using image size in setup()?

Hi,

Could someone please tell me how I can get this sketch to work in Processing 3? I've tried using surface.setResizable() and surface.setSize() etc which changes the display window to the right dimensions but the image still does not get displayed in it ... [argh: how do I get code to display properly in here?]

PImage tmp;
PImage[] img = new PImage[2];
int x, y, w, h, r=100, r2=100;
int num = int(map(r2,0,100,10000,2000));
void setup() {
  img[0] = loadImage("3.jpg");
  img[1] = loadImage("4.jpg");
  size(img[0].width, img[0].height);
  initStuff();
}
void draw() {
}
void mouseReleased() {
  background(255);
  initStuff();
}
void keyPressed() {
  save(num+".jpg");
}
void doStuff(int v, float v1, float v2) {
  w = (int) random(r2, 3*r2);
  h = (int) random(r2, 3*r2);
  x = (int) random(v1, v2-w);
  y = (int) random(height-h);
  tmp = createImage(w, h, RGB);
  tmp.loadPixels();
  int c=0;
  for (int j=0; j<h; j++) {
    for (int i=0; i<w; i++) {
      tmp.pixels[c] = img[v].get(x+i, y+j);
      c++;
    }
  }
  tint(255, 100);
  image(tmp, x+random(-r, r), y+random(-r, r));
}
void initStuff() {
  for (int i=0; i<num; i++) {
    doStuff(0, 0, width/2);
    doStuff(1, width/2, width);
  }
}

Answers

  • edited October 2015

    Ok, thanks.

  • It made it work like this now, I guess that's the right way to go then?

    void setup() {
      size(100,100);
      img[0] = loadImage("image1.jpg");
      img[1] = loadImage("image2.jpg");
      surface.setResizable(true);
      surface.setSize(img[0].width, img[0].height);
    
    }
    
    void draw() {
        initStuff();
        noLoop();
    }
    
  • Another option: use PGraphics object as a buffer. Work with buffer only and display it in draw() function rescaling to the window size. There are two adventages in this way: you can work with huge image sizes and when surface is changed by user you don't loose window content.

  • tsuleg: but I sometimes want the display window to take its size from the referenced image/photo. And that size might change, depending on the image/photo. So I guess I have to go with the solution above. Even if it's more code to write than in Processing 2.

  • To explain where the change came from, please read this: https://github.com/processing/processing/wiki/Changes-in-3.0#things-that-may-break-your-2x-sketches

    Specifically this:

    In the past, the size() function was implemented by doing backflips behind the scenes. Those backflips made things very fragile, introduced cross-platform quirks that have consumed too many of my weekends, and prevented us from making wholesale performance improvements to the rendering system (higher performance, better full screen support, etc).

  • Thanks, and stuff like the enhanced fullscreen support are great indeed. I guess it's just somewhat annoying if you suddenly have to do stuff differently or use more code to achieve the same ;) But overall the decision for this makes sense, of course. Maybe I'm also just a bit grumpy today ;P

  • Answer ✓

    We considered it carefully, over many years. It was a situation of making things better for the 90% case of wanting fast, full-screen programs and fewer edge-case bugs.

Sign In or Register to comment.