Home > Net >  Processing function does nothing but freezes the program
Processing function does nothing but freezes the program

Time:09-21

I'm working on a pathfinding algorithm in processing. If I don't draw a line that crosses the path between the green and the red dot, it works, but if I do, I've got a problem, that if I call a function, the program does absolutely nothing but freezes, and I have no idea why. The pixelss stores what you draw, and there are all kinds of stuff, that don't matter at this problem. When you paste it to processing, press ctrl t to auto-format it so you can understand it better, but I'd bet it's a newbie issue.

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);}}}}}

CodePudding user response:

You should at least print something when the ArrayIndexOutOfBoundsException is caught.

It looks like you are working with some kind of gui library, make sure you are doing any processing in separate thread from the gui or the gui will become unresponsive and appear to 'freeze' like you describe.

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

CodePudding user response:

The program is getting caught in your while loop. You can see this if you print out the values of i and j inside the loop. They never meet the condition to escape the loop, so that chunk of code runs repeatedly with no change.

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

This hangs the app because the while loop needs to complete before the the draw function gets called again to update the screen.

It's not clear to me what you're actually doing in the while loop, but that's where you should look. Either alter your logic inside the loop, or change the condition to ensure that the code doesn't get stuck in an infinite loop.

  • Related