processingでウィンドウサイズ(size())で変数を使いたい.

void setup()で自由にウィンドウサイズを設定したい

void setup(){
  int w,h;
  w=600;
  h=800;
  size(w,h);
}

void draw(){
  rect(0,100,100,10);
}

でプログラムを動かしてみると、

Please fix the size() line to continue.

とエラーが出る。

特に、processingで画像処理をするとき、読み込む画像サイズをウィンドウサイズに設定したい。

この時どうすればよいか。

setResizable()

PImage img;
void setup() { 
  img=loadImage("img.jpg");
  surface.setResizable(true);
  surface.setSize(img.width, img.height);
  background(255);
}

を用いる。 以下コピペ用

  int windowWidth=1600;
  int windowHeight=900;


void setup() { 
  surface.setResizable(true);
  surface.setSize(windowWidth, windowHeight);
  background(255);
}

void draw(){
 
}

img.width; img.heightは画像の幅と高さを返す関数である。PImageクラスのメソッド(関数)である。

リファレンスは以下である。 processing.org

By default, Processing sketches can't be resized. When surface.setResizable(true) is used within a sketch, the window can be resized while it's running.

とのことです。

もし、自分の画面のサイズを使いたいときはdisplayWidthdisplayHeightを使うとフルスクリーンにしたりするときに便利