1

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() {

    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
David
  • 693
  • 1
  • 7
  • 20
  • sorry this code came from pre_dinosaurus edges – mKorbel Oct 07 '15 at 09:39
  • Possible duplicate of [How to create more than one window of a single sketch in Processing?](http://stackoverflow.com/questions/32798606/how-to-create-more-than-one-window-of-a-single-sketch-in-processing) – Kevin Workman Oct 07 '15 at 12:44
  • The error you're getting, and the solution to that error, is the exact same as [this question](http://stackoverflow.com/questions/32798606/how-to-create-more-than-one-window-of-a-single-sketch-in-processing/32798761#32798761). In fact I'm just going to copy-paste my answer from there to here. – Kevin Workman Oct 07 '15 at 12:44

1 Answers1

4

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);
  }
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Hi it's work great ! But i am trying to redraw my SecondApplet when I click on the first Window, I got the global : SecondApplet sa; and in my setup I do : sa = new SecondApplet(); Finally I am trying this : void mouseClicked() { print("clicked"); sa.background(0, 0, 255); sa.fill(100); sa.redraw(); } but it's actually changing nothing in my SecondApplet, do you know why ? – Logan Gallois Sep 18 '16 at 08:38
  • @LoganGallois If you have a follow-up question, you'll have better luck if you post it in its own question. – Kevin Workman Sep 18 '16 at 12:15
  • Okay ;) I'll post a question then ;) – Logan Gallois Sep 18 '16 at 12:24