如何在Processing中创建单个草图的多个窗口? [英] How to create more than one window of a single sketch in Processing?

查看:40
本文介绍了如何在Processing中创建单个草图的多个窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Processing 中仅使用一个草图来创建两个窗口.

我想要做的是,如果我在一个窗口中单击一个按钮,那么某些图像会出现在另一个窗口中.

我在 Google 上搜索过并找到了一些示例.实际上,我在这个堆栈溢出网络"中发现了同样的问题.这是链接.

创建多个窗口处理中的单个草图http://forum.processing.org/one/主题/multiple-windows-2-4-2011.html

这是第二个链接的代码.

import java.awt.Frame;P帧f;第二个Applet;//f = new PFrame();无效设置(){大小(320、240);f = 新 PFrame();}无效画(){背景(255,0,0);填充(255);rect(10,10,frameCount%0,10);s.background(0, 0, 255);s.fill(100);s.rect(10,20,frameCount%0,10);s.redraw();}公共类 PFrame 扩展框架{公共 PFrame() {setBounds(100,100,400,300);s = 新的第二个小程序();添加(s);s.init();展示();}}公共类 secondApplet 扩展 PApplet {公共无效设置(){大小(400, 300);无环();}公共无效画(){}}

但是当我运行这些代码时,我在 add(s); 处收到以下错误消息.

Container 类型中的 add(Component) 方法不适用于参数 (multi_window_test.secondApplet)

第一个链接的第一个注释的代码类似,但是当我运行此代码时,我收到相同的错误消息.

我发现的其他示例代码都相似.它们都创建了 PFrame 类和扩展 PApplet 的 secondApplet.他们说这些代码运行良好,但我无法运行这些代码.

我找不到错误消息的原因.除了我之外,其他人在运行此示例代码时似乎没有问题.如果有人知道解决方案,请帮助我.

另外,如果有其他简单的方法可以在一个草图中创建多窗口,请告诉我.

解决方案

错误消息的原因不言而喻:add() 函数需要一个 Componentcode>,而 PApplet 不是 Component.这是因为 PApplet 从处理 3 开始不再扩展 Applet,因此将其用作 Component 的旧代码将不再有效.>

相反,请考虑我对这个问题的回答.基本上,只需为您的第二个窗口创建一个扩展 PApplet 的类,然后使用第二个 PApplet 作为参数调用 PApplet.runSketch():

void setup() {大小(100, 100);String[] args = {"TwoFrameTest"};SecondApplet sa = new SecondApplet();PApplet.runSketch(args, sa);}无效画(){背景(0);椭圆(50, 50, 10, 10);}公共类 SecondApplet 扩展 PApplet {公共无效设置(){大小(200, 100);}公共无效画(){背景(255);填充(0);椭圆(100, 50, 10, 10);}}

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.

解决方案

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

这篇关于如何在Processing中创建单个草图的多个窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆