Threads, draw(), asynctask with big drawings?

edited November 2014 in Android Mode

Hi, The question is a bit hard to formulate, but here is my problem: I am doing a cell-based simulation and want to display the states of the so-called cells on a grid using color codes. In Java mode, it's quite fast and I have used the architecture below:

  • in setup(): create the cells (an Arraylist of cells), start a thread("computation"), and call noLoop();

  • in the method computation(): update the state values of all cells, and, at the end, call redraw();

  • in draw(): a "for" loop in which I call the draw() for each cell.

It did not bother me and basically I was happy that the simulation time was faster than the real time.

I tried to port it to Android (I am testing it on a Galaxy S5), and now it is much slower. I guess it is because of the for loop in the draw(), so I tried:

  • putting this "for" loop in another thread(), but then everything bugs

  • putting this "for" loop in a class inheriting AsyncTask: it works but the display is messed up.

I guess i need to put some "synchronize" here and there, but I am always confused on how to do that.

Any help?

Thanks.

Answers

  • edited October 2014 Answer ✓

    You can't draw to the canvas in another thread. I don't know anything about Android mode but I guess this is similar to java mode. There are two options:

    • Do your calculations in a separate thread and use the computed values to calculate the drawings in the main thread

    or

    • In the other threads, render to a PGraphics instead of the default PApplet PGraphics then at the end put everything together. Pseudocode:

      PGraphics p = createGraphics(width, height, renderer);
      p.beginDraw();
      p.stroke(255);
      p.line(x1, y1, x2, y2);
      p.endDraw();
      

    Then in the main draw loop use image:

    image(p, 0, 0);

Sign In or Register to comment.