New window in Processing

Do Tog picture Do Tog · Aug 27, 2015 · Viewed 8.8k times · Source

Yesterday I found the following code for creating a second window in Processing

import javax.swing.JFrame;

PFrame f;
secondApplet s;

void setup() {
size(600, 340);

 }

 void draw() {
 background(255, 0, 0);
 fill(255);
 }     

 void mousePressed(){

 PFrame f = new PFrame();
 }

 public class secondApplet extends PApplet {

 public void setup() {
   size(600, 900);
    noLoop();
 }
 public void draw() {
   fill(0);
   ellipse(400, 60, 20, 20);
 }
 }
 public class PFrame extends JFrame {
   public PFrame() {
    setBounds(0, 0, 600, 340);
   s = new secondApplet();
   add(s);
    s.init();
    println("birh");
    show();
  }
}

And edited...

 void mousePressed(){

 PFrame f = new PFrame();
 }

Into:

 if(mousePressed && mouseX > 1050 && mouseX < 1350 && mouseY > 700 && mouseY < > 750) {
   f = new PFrame();

    }    
  }

It worked lovely, but since I downloaded and installed Processing III, I've got the following errors:

  • The function add() expects parameters like add(component).
  • The function init() does not exist.
  • The method show() from the type window is deprecated.

Answer

Kevin Workman picture Kevin Workman · Aug 27, 2015

First of all, that code is not very good. I'm surprised it worked in Processing 2, let alone Processing 3. Be very wary of code you just find randomly on the internet.

That being said, here's some code:

void setup() {
  size(100, 100);

  String[] args = {"TwoFrameTest"};
  SecondApplet sa = new SecondApplet();
  PApplet.runSketch(args, sa);
}

void draw() {
  background(0);
  ellipse(50, 50, 10, 10);
}     

public class SecondApplet extends PApplet {

  public void settings() {
    size(200, 100);
  }
  public void draw() {
    background(255);
    fill(0);
    ellipse(100, 50, 10, 10);
  }
}

The above works for me, although the approach seems pretty hackish. If you really want to have two windows in your sketch, you might be better off creating a Java application that spawns two sketches.