Processing 处理:draw()中的代码没有输出

Processing 处理:draw()中的代码没有输出,processing,Processing,在下面的代码中,它运行时没有错误,但没有输出。但是,当我从draw()循环中删除代码时,它会成功输出。 如果我将代码放在for循环中,那么输出只发生在for循环的末尾,而不是在for循环期间。 我不明白为什么在这两种情况下都没有输出 void setup(){ size(800,800); } int rows = height; int cols = width; int [][] myArray = new int[rows][cols]; void draw(){ backgr

在下面的代码中,它运行时没有错误,但没有输出。但是,当我从draw()循环中删除代码时,它会成功输出。 如果我将代码放在for循环中,那么输出只发生在for循环的末尾,而不是在for循环期间。 我不明白为什么在这两种情况下都没有输出

void setup(){
  size(800,800);
}

int rows = height;
int cols = width;
int [][] myArray = new int[rows][cols];

void draw(){
  background(255);
  for (int i=0;i<cols;i=i+10){
    for (int j=0;j<rows;j=j+10){
      myArray[i][j]=int(random(255));
    }
  }
  for (int i=0;i<cols;i=i+10){
    for (int j=0;j<rows;j=j+10){
      fill(myArray[i][j]);
      rect(i,j,i+10,j+10);
    } 
  }
}
void setup(){
尺寸(800800);
}
int行=高度;
int cols=宽度;
int[][]myArray=新的int[行][cols];
作废提款(){
背景(255);

对于(int i=0;i
width
height
在调用
size()
之后才有实际值。仅仅因为您将变量放在函数之后,并不意味着它们在
setup()之后被赋值
runs:在运行任何函数之前,都会分配所有全局变量。因此,在本例中,宽度和高度都为0,这将完全不绘制任何内容,因为没有要着色的像素=)

您希望这样做:

// let's put our global vars up top, to prevent confusion.
// Any global variable that isn't just declared but also
// initialised, gets initialised before *anything* else happens.
int rows, cols;
int[][] myArray;

// Step 2 is that setup() runs
void setup(){
  size(800,800);
  // now we can initialise values:
  rows = width;
  cols = height;
  myArray = new int[rows][cols];
}

// Setup auto-calls draw() once, and
// then draw() runs at a specific framerate
// unless you've issued noLoop() at some point.
void draw(){
  background(255);
  for (int i=0;i<cols;i=i+10){
    for (int j=0;j<rows;j=j+10){
      myArray[i][j]=int(random(255));
    }
  }
  for (int i=0;i<cols;i=i+10){
    for (int j=0;j<rows;j=j+10){
      fill(myArray[i][j]);
      rect(i,j,i+10,j+10);
    }  
  }
}
int[][] myArray;

// Step 2 is that setup() runs
void setup() {
  size(800,800);
  myArray = new int[width][height];
}

void draw() {
  background(255);
  int i, j, step = 10;
  for (i=0; i<height; i=i+step) {
    for (j=0; j<width; j=j+step) {
      myArray[i][j]=int(random(255));
      fill(myArray[i][j]);
      rect(i,j,i+step,j+step);
    }  
  }
}