微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

调用方法时,处理第二个 PApplet 窗口不会更新

如何解决调用方法时,处理第二个 PApplet 窗口不会更新

我有以下代码允许有两个窗口,在主窗口中按下“k”键时,必须调用 Graph 的函数 disegna()添加文本。

但似乎无法正常工作,没有发生绘图更新。

你能帮我一把吗?

enter image description here

int N = 500;
boolean flag = false;
Grafico graph = new Grafico(N,N);

void settings() {
  size(N,N);
}

void setup() {
  surface.setTitle("Grafico1");
  surface.setLocation(0,0);

  String[] args = {
    "--location=" + (N*2) + ",0","Grafico2"
  };
  papplet.runSketch(args,graph);
  grafico();
}

void grafico() {
  background(0);
  text("Main",10,20);
}

void keypressed() {
  switch (key) {
  case 'k':
    graph.disegna();
    break;
  }
}

void draw() {
  if (!flag) {
    frame.setLocation(0,0);
    flag = true;
  }
}

public class Grafico extends papplet {
  private final int w,h;

  public Grafico(int w,int h) {
    this.w = w;
    this.h = h;
  }
  
  void settings () {
    size(w,h);
  }
  
  void setup() {
    ini();
  }

  void ini() {
    background(0);
    text("Second",20);
  }
  
  void disegna() {
     text("Hello!",50);
     println("Hello!");
  }
  
  void draw() {
  }
}

解决方法

在 Grafico delay(10)draw() 循环中添加一个小的 PApplet 使其工作!

public class Grafico extends PApplet {
  private final int w,h;

  public Grafico(int w,int h) {
    this.w = w;
    this.h = h;
  }
  
  void settings () {
    size(w,h);
  }
  
  void setup() {
    ini();
  }

  void ini() {
    background(0);
    text("Second",10,20);
  }
  
  void disegna() {
     text("Hello!",50);
     println("Hello!");
  }
  
  void draw() {
    delay(10);
  }
}

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。