2

I want to create two windows by using just one single sketch in Processing.

What I'm trying to do is that if I click a button in one window, then some image appear in another window.

I've searched Google and found some examples. Actually, I found the same question in this 'stack overflow web'. Here are the links.

Create more than one window of a single sketch in Processing http://forum.processing.org/one/topic/multiple-windows-2-4-2011.html

Here is the codes of second links.

import java.awt.Frame;
PFrame f;
secondApplet s;
//f = new PFrame();
void setup() {
 size(320, 240);
 f = new PFrame();
}

void draw() {
  background(255,0,0);
   fill(255);
   rect(10,10,frameCount%0,10);
   s.background(0, 0, 255);
   s.fill(100);
   s.rect(10,20,frameCount%0,10);
   s.redraw();
}

public class PFrame extends Frame{
    public PFrame() {
        setBounds(100,100,400,300);
        s = new secondApplet();
        add(s);
        s.init();
        show();
    }
}

public class secondApplet extends PApplet {
    public void setup() {
        size(400, 300);
        noLoop();
    }

    public void draw() {
    }
} 

But when I run this codes, I get the following error message at add(s);.

The method add(Component) in the type Container is not applicable for the arguments (multi_window_test.secondApplet)

Code of first comment of first link is similar, but when I run this code, I get the same error message.

Other example codes that I found are all similar. They all create PFrame class and secondApplet which extends PApplet. They said these codes works well but I can't run these codes.

I couldn't find the reason of my error message. Other people seems to have no problem when running this example code except me. If someone knows the solution, please help me.

Also, if there is a other simple way to create multi-windows in one sketch, please let me know.

Community
  • 1
  • 1
Jinha.Kim
  • 145
  • 1
  • 3
  • 10

1 Answers1

4

The reason for the error message is pretty self-explanatory: 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, consider my answer to this question. Basically, just 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);
  }
}
Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Can I ask one more question? Actually, I have two monitors, and I want to show two windows at each monitors. I want to show 2nd window at 2nd monitor as fullScreen. Could you let me know how I can do this? – Jinha.Kim Sep 26 '15 at 17:08
  • @Jinha.Kim That's a little more involved, but basically you need to use Java's `GraphicsEnvironment` class. Google is your friend, and I talked about it [here](http://forum.processing.org/two/discussion/comment/49654/#Comment_49654). Try something out and if you get stuck, I suggest posting another separate question. – Kevin Workman Sep 26 '15 at 20:48
  • I just found a solution. There is a simple command in Processing 3.0. If I wirte fullScreen(2) instead of size(400,400), then window show as fullScreen in 2nd monitor. Here is the link. https://www.processing.org/reference/fullScreen_.html – Jinha.Kim Sep 30 '15 at 01:48
  • Also, I want to close the 2nd window if I click the mousebutton in 1st window. Now I can open the two windows due to your help, but I also close the 2nd window when I want. Could you let me know how I can do this? – Jinha.Kim Sep 30 '15 at 01:53
  • @Jinha.Kim Post what you've tried (in the form of an example [MCVE](http://stackoverflow.com/help/mcve) like I posted in my answer) in a new question, and we'll go from there. – Kevin Workman Sep 30 '15 at 12:13