Imaginary Code

from kougaku-navi.net

Processingでフルスクリーンと通常のウィンドウを切り替える

この話は前にも書いているが、前のコードではフルスクリーン化した際にウィンドウ本体へのフォーカスが失われるという症状があることがわかったので、そのあたりを修正した。具体的には this.requestFocus(); という1行を加えればよい。ついでに通常のウィンドウに戻す方法も追記する。ちなみにProcessingのフルスクリーン制御ライブラリはあるにはあるのだが、更新が止まっているのでこうやって自前で対応している。

フルスクリーン化する関数
void setFullScreen() {
  size(displayWidth, displayHeight); 
  frame.removeNotify();
  frame.setUndecorated(true);
  frame.addNotify();
  frame.setSize(width, height);
  frame.setLocation(0, 0);
  frame.setAlwaysOnTop(true);
  this.requestFocus();
}
通常のウィンドウにする関数(描画領域のサイズを指定)
void setNormalWindow(int w, int h) {
  frame.removeNotify();
  frame.setUndecorated(false);
  frame.addNotify();
  frame.setAlwaysOnTop(false);
  frame.setLocation(0, 0);
  frame.setSize( w + frame.getInsets().left + frame.getInsets().right, h + frame.getInsets().top + frame.getInsets().bottom );
  size(w, h);
  this.requestFocus();
}
使用例

キーボードの「1」を押すとフルスクリーン化し、「2」を押すと通常のウィンドウに戻る。

void setup() {  
  size(400, 300);
}  

void draw() {  
  background(100);
  text( width + ", " + height, 20, 20 );
}

void keyPressed() {
  if ( key=='1' ) {
    setFullScreen();
  }
  if ( key=='2') {
    setNormalWindow(400, 300);
  }
}

void setNormalWindow(int w, int h) {
  frame.removeNotify();
  frame.setUndecorated(false);
  frame.addNotify();
  frame.setAlwaysOnTop(false);
  frame.setLocation(0, 0);
  frame.setSize( w + frame.getInsets().left + frame.getInsets().right, h + frame.getInsets().top + frame.getInsets().bottom );
  size(w, h);
  this.requestFocus();
}

void setFullScreen() {
  size(displayWidth, displayHeight); 
  frame.removeNotify();
  frame.setUndecorated(true);
  frame.addNotify();
  frame.setSize(width, height);
  frame.setLocation(0, 0);
  frame.setAlwaysOnTop(true);
  this.requestFocus();
}