【问题标题】:Processing second PApplet window does not update when method is invoked调用方法时处理第二个 PApplet 窗口不更新
【发布时间】:2021-05-31 12:29:42
【问题描述】:

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

但是好像不能正常工作,没有出现draw update。

你能帮帮我吗?

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", 10, 20);
  }
  
  void disegna() {
     text("Hello!", 10, 50);
     println("Hello!");
  }
  
  void draw() {
  }
}

【问题讨论】:

    标签: java processing draw papplet


    【解决方案1】:

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

    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!", 10, 50);
         println("Hello!");
      }
      
      void draw() {
        delay(10);
      }
    }
    

    【讨论】:

    • 但是为什么会这样呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2013-05-06
    • 2018-11-11
    • 2019-05-07
    • 1970-01-01
    相关资源
    最近更新 更多