Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

java - How to create more than one window of a single sketch in Processing?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...