代码之家  ›  专栏  ›  技术社区  ›  Paul

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

  •  0
  • Paul  · 技术社区  · 5 年前

    我有下面的代码,它允许有两个窗口,在主窗口中按下“k”键时,必须调用该函数 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", 10, 20);
      }
      
      void disegna() {
         text("Hello!", 10, 50);
         println("Hello!");
      }
      
      void draw() {
      }
    }
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Om Kumar    5 年前

    添加一个小的 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!", 10, 50);
         println("Hello!");
      }
      
      void draw() {
        delay(10);
      }
    }
    

    enter image description here

    推荐文章