Imaginary Code

from kougaku-navi.net

Processingで複数のウィンドウを作る

PAppletの派生クラスでもう1つのウィンドウ上で動くアプレットの内容を記述し、PFrameを使ってそのアプレットを動かすウィンドウを作る感じ。なんか毎回こういうの書いてる気がするなー。

PFrame.pde
import java.awt.Frame;
import java.awt.Insets;

public class PFrame extends Frame {  
  public PFrame(PApplet app) {
    app.init();
    while ( app.width<=PApplet.DEFAULT_WIDTH || app.height<=PApplet.DEFAULT_HEIGHT );
    Insets insets = frame.getInsets();
    setSize( app.width + insets.left + insets.right, app.height + insets.top + insets.bottom );
    setResizable(false);    
    add(app);
    show();
  }
}
メインのコード
PFrame second_frame;
SecondApplet second_app;

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

  second_app = new SecondApplet();
  second_frame = new PFrame(second_app);
  second_frame.setTitle("2nd frame");
  second_frame.setLocation(200, 200);
}

void draw() {
  background(255);
  fill(0, 255, 0);
  ellipse( mouseX, mouseY, 100, 100 );
}

class SecondApplet extends PApplet {
  void setup() {
    size( 200, 200 );
  }
  
  void draw() {
    background(255);
    fill(255, 0, 0);
    ellipse( mouseX, mouseY, 50, 50 );
  }
}