Do Tog
Do Tog

Reputation: 35

New window in Processing

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:

Upvotes: 2

Views: 10949

Answers (2)

Kevin Workman
Kevin Workman

Reputation: 42176

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.

Upvotes: 7

colouredmirrorball
colouredmirrorball

Reputation: 331

Processing 3 was changed so it is no longer dependent on AWT allowing for more flexibility, but breaking code that depends on it (like JFrames and such). The new way to do it is by using PSurfaces but documentation and examples are lacking at the present moment. This part of Processing 3 is under active development so you'll have to wait it out a bit.

Upvotes: 1

Related Questions