处理函数除了冻结程序什么都不做

Processing function does nothing but freezes the program

我正在研究处理中的寻路算法。如果我不画一条穿过绿点和红点之间路径的线,它会起作用,但如果我这样做,我就会遇到一个问题,如果我调用一个函数,程序除了冻结之外什么都不做,我不知道为什么。 pixelss 存储你画的东西,有各种各样的东西,在这个问题上无关紧要。 当你粘贴到processing时,按ctrl+t自动格式化,这样你会更好地理解它,但我敢打赌这是一个新手问题。

int[][] pixelss = new int[500][500];
void setup() {
  background(255);
  size(500, 500);}
int[][] badcoos = new int[500][500];
void golinego() {
  stroke(200, 200, 255);
  line(30, 30, 470, 470);
  int j = 30;
  int i = 30;
  while (dist(i, j, 470, 470) > 10) {
    stroke(0, 0, 180);
    circle(i, j, 1);
    if (pixelss[i+1][j+1]==0) {
        i++;j++;}
    if (pixelss[i][j]==1) {
      if (pixelss[i][j+1]==1) {
        if (pixelss[i+1][j]==0) {
          i++;}
      } else if (pixelss[i+1][j]==1) {
        if (pixelss[i][j+1]==0) {
          j++;}
      } else {
        i-=1;
        j-=1;}}}}
void draw() {
  stroke(0, 255, 0);
  fill(0, 255, 0);
  circle(30, 30, 10);
  stroke(255, 0, 0);
  fill(255, 0, 0);
  circle(470, 470, 10);
  if (mousePressed == true) {
    try {
      stroke(0);
      fill(0);
      circle(mouseX, mouseY, 2);
      pixelss[mouseX][mouseY] = 1;
      pixelss[mouseX+1][mouseY] = 1;
      pixelss[mouseX-1][mouseY] = 1;
      pixelss[mouseX][mouseY+1] = 1;
      pixelss[mouseX][mouseY-1] = 1;
      pixelss[mouseX+1][mouseY+1] = 1;
      pixelss[mouseX-1][mouseY+1] = 1;
      pixelss[mouseX+1][mouseY-1] = 1;
      pixelss[mouseX-1][mouseY-1] = 1;
    }catch(ArrayIndexOutOfBoundsException e) {}}}
void keyPressed() {
  if (key=='r') {
    pixelss = new int[500][500];
    badcoos = new int[500][500];
    background(255);}
  if (key==' ') {
    golinego();}
  if (key=='d') {
    background(0);
    for (int i = 0; i < 500; i++) {
      for (int j = 0; j < 500; j++) {
        if (pixelss[i][j]==1) {
          stroke(255);
          circle(i, j, 1);}}}}}

您至少应该在捕获到 ArrayIndexOutOfBoundsException 时打印一些内容。

看起来您正在使用某种 gui 库,请确保您在与 gui 不同的线程中进行任何处理,否则 gui 将变得无响应并且看起来 'freeze' 就像您描述的那样。

https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

程序陷入 while 循环。 如果您在循环内打印出 ij 的值,您可以看到这一点。他们从不满足跳出循环的条件,因此代码块重复运行而没有任何变化。

while (dist(i, j, 470, 470) > 10) {
  println(i, j);
  // etc...
}

这会挂起应用程序,因为 while 循环需要在再次调用 draw 函数以更新屏幕之前完成。

我不清楚你在 while 循环中实际做了什么,但那是你应该看的地方。要么更改循环内的逻辑,要么更改条件以确保代码不会陷入无限循环。