[英]Multiple sketch windows in processing
我目前正在进行数据可视化,该可视化从电影中获取信息,并实时播放并同时创建。 为此,我想有两个单独的草图窗口。 一个窗口应显示实时播放的电影,另一个窗口应显示正在进行的可视化。
我似乎无法弄清楚如何轻松添加另一个草图窗口,并尝试了一些在Processing 3中不再可用的示例。 (例如,sojamos库控件p5)
然后我偶然发现了这个例子: https : //gist.github.com/atduskgreg/666e46c8408e2a33b09a
尽管我可以一次显示两个草图窗口,但是显然我不能使用另一个窗口中的数据。
还有另一种方法吗?
谢谢!
没有什么可以阻止您在PWindow
类中声明一个函数(它将创建第二个窗口),该函数接受可以在内部使用的参数并从另一个草图调用它。
因此,您可以将数据作为函数参数传递给第二个窗口。
这个小例子通过一个函数(这里称为evokedFromPrimary
)将相对的mousePressed
位置从第一个窗口传递到第二个窗口,并将其存储在ArrayList
,该ArrayList
将它们绘制在第二个窗口中:
PWindow win;
public void settings() {
size(320, 240);
}
void setup() {
win = new PWindow();
}
void draw() {
background(255, 0, 0);
text("Click in this window to draw at a relative position in the other window", 10, 10, this.width - 20, 100);
}
void mousePressed() {
println("mousePressed in primary window");
float relPosX = map(mouseX, 0, this.width, 0, 100);
float relPosY = map(mouseY, 0, this.height, 0, 100);
win.evokedFromPrimary(relPosX, relPosY);
}
class PWindow extends PApplet {
ArrayList<PVector> vectors = new ArrayList<PVector>();
PWindow() {
super();
PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
}
void evokedFromPrimary(float relPosX, float relPosY) {
println("evoked from primary");
float xPos = map(relPosX, 0, 100, 0, this.width);
float yPos = map(relPosY, 0, 100, 0, this.height);
vectors.add(new PVector(xPos, yPos));
}
void settings() {
size(500, 200);
}
void setup() {
background(150);
}
void draw() {
background(150);
//store the vector size before using to avoid ConcurrentModificationException
int listLength = vectors.size();
for(int i = 0; i < listLength; i++) {
PVector v = vectors.get(i);
ellipse(v.x, v.y, random(50), random(50));
}
}
void mousePressed() {
println("mousePressed in secondary window");
}
}
此处的Pwindow代码位于同一.pda文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.