没有什么可以阻止您在 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 文件中。