티스토리 뷰

Processing

[Processing] Multiple windows on Processing

생각많은 소심남 2015. 6. 6. 02:52

한번 5개의 processing 창을 동시에 출력하는 예제를 해봤다. 내용은 각 창마다 마우스 포인터의 위치를 따라가는 예제이다.

(참고 : http://stackoverflow.com/questions/20730901/create-more-than-one-window-of-a-single-sketch-in-processing)


import javax.swing.*; 

SecondApplet s;

ThirdApplet t;

FourthApplet f;

FifthApplet ff;


void setup() {

  size(640, 480);

  PFrame f = new PFrame(width, height);

  PFrame s = new PFrame(width, height);

  PFrame t = new PFrame(width, height);

  PFrame ff = new PFrame(width, height);

  frame.setTitle("first window");

  f.setTitle("second window");

  s.setTitle("third window");

  t.setTitle("fourth window");

  ff.setTitle("fifth window");

  fill(0);

}

void draw() {

  background(255);

  ellipse(mouseX, mouseY, 10, 10);

  s.setGhostCursor(mouseX, mouseY);

}

public class PFrame extends JFrame {

  public PFrame(int width, int height) {

    setBounds(100, 100, width, height);

    s = new SecondApplet();

    t = new ThirdApplet();

    f = new FourthApplet();

    ff = new FifthApplet();

    add(s);

    add(t);

    add(f);

    add(ff);

    s.init();

    t.init();

    f.init();

    ff.init();

    show();

  }

}

public class SecondApplet extends PApplet {

  int ghostX, ghostY;

  public void setup() {

    background(0);

    noStroke();

  }


  public void draw() {

    background(50);

    fill(255);

    ellipse(mouseX, mouseY, 10, 10);

    fill(0);

    ellipse(ghostX, ghostY, 10, 10);

  }

  public void setGhostCursor(int ghostX, int ghostY) {

    this.ghostX = ghostX;

    this.ghostY = ghostY;

  }

}


public class ThirdApplet extends PApplet {

  int ghostX, ghostY;

  public void setup() {

    background(0);

    noStroke();

  }


  public void draw() {

    background(50);

    fill(255);

    ellipse(mouseX, mouseY, 10, 10);

    fill(0);

    ellipse(ghostX, ghostY, 10, 10);

  }

  public void setGhostCursor(int ghostX, int ghostY) {

    this.ghostX = ghostX;

    this.ghostY = ghostY;

  }

}


public class FourthApplet extends PApplet {

  int ghostX, ghostY;

  public void setup() {

    background(0);

    noStroke();

  }


  public void draw() {

    background(50);

    fill(255);

    ellipse(mouseX, mouseY, 10, 10);

    fill(0);

    ellipse(ghostX, ghostY, 10, 10);

  }

  public void setGhostCursor(int ghostX, int ghostY) {

    this.ghostX = ghostX;

    this.ghostY = ghostY;

  }

}


public class FifthApplet extends PApplet {

  int ghostX, ghostY;

  public void setup() {

    background(0);

    noStroke();

  }


  public void draw() {

    background(50);

    fill(255);

    ellipse(mouseX, mouseY, 10, 10);

    fill(0);

    ellipse(ghostX, ghostY, 10, 10);

  }

  public void setGhostCursor(int ghostX, int ghostY) {

    this.ghostX = ghostX;

    this.ghostY = ghostY;

  }

}


실행 결과는 다음과 같다.



아 참 JFrame의 extend한 PFrame을 여러 형태로 만들어 놓으면 각창의 시작 위치도 조절할 수 있을 법하다. 이건 피곤해서 다음에 해봐야 겠다. 

댓글