define size with variable

edited March 2016 in How To...

is it possible to define width and height with variable ?
I saw settings() but it doesn't work.

int w = 200;
int h = 200;
void settings() {
  size(w, h);
}

void setup() {
  w = 100 ; 
  println(width); // return 200
}

I try to define img size after generated one from the rendering, but it does not work since the img file is not yet exported...

PImage temp4img ; 

void setup() {
  size(400, 400);
  strokeWeight(50);
  point(width/2, height/2);
  save("me.png");
  temp4img = loadImage("data/me.png"); // null pointer exception
 temp4img.resize(20,20);
 image(temp4img,0,0);
}
Tagged:

Answers

  • edited March 2016

    The OP from this recent thread got it right just by reading about settings():
    https://forum.Processing.org/two/discussion/15545/can-t-get-size-to-adapt-to-an-image

    I don't get how the 1st example is related to the 2nd 1 though?
    Given the 2nd 1 doesn't use settings(). :-@

  • Answer ✓

    what are you expecting from that first block of code?

    int w = 200;
    int h = 200;
    void settings() {
      println("settings");
      size(w, h);
    }
    
    void setup() {
      println("setup");
      w = 100 ; 
      println(width); // return 200
      println(w); // return 100
    }
    

    settings runs before setup. screen is set to 200x200. you change w in setup but are printing width, which is 200 as expected.

  • save("me.png");
    

    saves an image in the sketch directory, not the data directory.

  • edited March 2016

    @GoToLoop whats OP ?
    okay so we can make calcul inside settings() to affect width & height
    edit : calcul without function
    @koogs what are you expecting from that first block of code?
    to determine a new value for width, which result from a calculation :
    another example

    String test="ksjdfhffffffffffffffffffffffffffffffff";
    
    void settings() {
      int w = int(textWidth(test)); // null pointer exception
      size(w, 100);
    }
    
    void setup() {
      println(width);
    }
    
  • that gives a NPE, which is because you are calling textWidth()

    https://processing.org/reference/settings_.html

    "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. "

  • edited March 2016

    Most of what @koogs has stated above is not true anymore since version 3.0.2! #-o
    AFaIK sketchPath, displayWidth, displayHeight & args[] are all available already inside settings().
    But we still need to wait for setup() for both width & height for obvious reasons. ;;)

  • What's OP?

    Not 100% sure but I bet it's Original Poster or something like that. ~:>

Sign In or Register to comment.