I'm trying to create two windows with Processing. Before you mark this as a duplicate, as there are other questions similar to this, I have a specific error and I can't find a solution. When I try to add(s)
I get the error The method add(Component) in the type Container is not applicable for the arguments (evolution_simulator.SecondApplet)
I'm not sure how to fix this, and any help would be appreciated. Here is the code:
import javax.swing.*;
PFrame f;
void setup() {
size(320, 240);
f = new PFrame();
}
void draw() {
}
public class PFrame extends JFrame {
SecondApplet s;
public PFrame() {
setBounds(100,100,400,300);
s = new SecondApplet();
add(s); // error occurs here
s.init();
show();
}
}
public class SecondApplet extends PApplet {
public void setup() {
size(400, 300);
noLoop();
}
public void draw() {
}
}
The reason for the error message is because the add()
function is expecting a Component
, and PApplet
is not a Component
. This is because PApplet
no longer extends Applet
as of Processing 3, so old code that uses it as a Component
will no longer work.
Instead, you can create a class that extends PApplet
for your second window, and then call PApplet.runSketch()
using that second PApplet
as a parameter:
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);
}
}