为什么不能用处理重画SecondApplet?

Why can't redraw SecondApplet with processing?

我正在处理 3,实际上我正在尝试处理 2 windows。

我想重绘第二个window但重绘什么都不做(在最终项目中,从串行端口传入的数据将重绘)。不知道为什么。

这是我的代码:

/*
*  17.09.16 : Logan Gallois
*  future imporvement :
*  Windows print graph of PWMs values from RGB LED
*  Add autoDetect position to move windows at the good location
*/

import processing.serial.*;
Serial myPort;
SecondApplet sa;

int[] numbers = new int[600];
boolean flag = false;
float var1;
float var2;
float var3;
float var4;

void setup() {
  size(1024, 576); //Affiche en plein écran
  surface.setResizable(false);
  noStroke();
  println(Serial.list()); /* Verifier dans la liste ou se trouve l'arduino */
  /* Puis changer en dessous la valeur en fonction */
  /* En general 0 pour windows et 1 pour un mac */
  String os=System.getProperty("os.name"); /* Detection automatique de l'OS */
  if(os != "Mac OS X") {
    if(Serial.list().length > 0) {
      myPort = new Serial(this, Serial.list()[0], 115200);
    }
  } else {
    if(Serial.list().length > 1) { /* Module BLE en position 0 */
      myPort = new Serial(this, Serial.list()[1], 115200);
    }
  }
  //Important : se câler sur la valeur en baud du prog Arduino
  myPort.bufferUntil('\n');
  String[] args = {"TwoFrameTest"};
  sa = new SecondApplet();
  PApplet.runSketch(args, sa);
  delay(100);
}

void draw() {
  if(!flag) {
    surface.setLocation(displayWidth/2-width/2-400,displayHeight/2-height/2);
    sa.frame.setTitle("Monitoring RGB Values");
    flag = true;
  }
  background(color(int(var1), int(var2), int(var3)));
  //sa.background(0, 255, 0);
    /*for(int i = 0 ;i<=width/10;i++){
     sa.stroke(200);
     sa.line((-frameCount%10)+i*10,0,(-frameCount%10)+i*10,height);

     sa.line(0,i*10,width,i*10);
    }

    sa.noFill();
    sa.stroke(0);
    sa.beginShape();
    for(int i = 0; i<numbers.length;i++){
      sa.vertex(i,350-numbers[i]);
    }
    sa.endShape();
    for(int i = 1; i<numbers.length;i++){
      numbers[i-1] = numbers[i];
    }
    numbers[numbers.length-1]=mouseY;*/
}

void mouseClicked() {
  print("clicked");
  sa.background(0, 0, 255);
  sa.fill(100);
  sa.redraw(); //This line do not redraw my 'sa' window
  background(0, 0, 255);
  redraw();
}

void serialEvent (Serial myPort) {

  String inString = myPort.readStringUntil('\n');
  //println(inString);
  if (inString != null) {

    inString = trim(inString); 
    int inputs[] = int(split(inString,',')); // on élude les virgules

    // on affecte nos 4 valeurs
    if(inputs.length == 4) {
     var1 = inputs[0];
     var2 = inputs[1];
     var3 = inputs[2];
     var4 = inputs[3];

    // On ré-échelonne la valeur analogique en valeur RGB
    var1 = map(var1, 0, 1023, 0, 255);
    var2 = map(var2, 0, 1023, 0, 255);
    var3 = map(var3, 0, 1023, 0, 255);
    var4 = map(var3, 0, 1023, 0, 255);
    print(int(var1));
    print(" ,");
    print(int(var2));
    print(" ,");
    print(int(var3));
    print(" ,");
    println(var4);
    }
  }
}

public class SecondApplet extends PApplet {

  public void settings() {
    size(600, 600);
    noLoop(); //I tried with and without that
  }
  public void draw() {

  }

  public void mouseClicked() {
    print("clicked2");
    background(0, 255, 0);
    redraw(); //This line works great
  }
}

有什么想法吗?

当 posting 时,请尝试将您的问题缩小到 MCVE。这意味着去掉所有串行的东西,只使用基本的鼠标事件。

您的第二个草图的 draw() 函数是空的。您希望调用 redraw() 实际做什么?

这是一个 MCVE 示例:

SecondApplet sa;

void setup() {
  size(500, 500); //Affiche en plein écran

  String[] args = {"TwoFrameTest"};
  sa = new SecondApplet();
  PApplet.runSketch(args, sa);
  delay(100);
}

void draw() {
  background(255, 0, 0);
}

void mouseClicked() {
  sa.redraw();
}

public class SecondApplet extends PApplet {

  public void settings() {
    size(100, 100);
    noLoop();
  }
  public void draw() {
    background(0);
    ellipse(random(width), random(height), 25, 25);
  }

  public void mouseClicked() {
    redraw();
  }
}

此 MCVE 完美运行,并从第一个和第二个草图 windows.

中正确触发第二个草图的 draw() 函数

我建议使用此 MCVE。如果你仍然无法让它工作,请随意 post 一个后续问题,最好没有连续的东西(这使得你的草图无法 运行 因为我们无法访问你的硬件)。祝你好运。