Here are two approaches you can take:
Rewrite your algorithm so that it can be increased using a variable. Then increase each iteration of the loop in the draw. Here is an example of someone trying to apply this approach to processing.
http://www.processing.org/discourse/alpha/board_Programs_action_display_num_1059766998.html
Write to the buffer off-screen and save each frame / iteration of the loop, and then display them later. I left the bubble sorting algorithm from the above example and methods for drawing per frame so you can see how I changed the structure to use the buffer.
int[] myArray; PGraphics pg; ArrayList<PImage> frameBufferList; void setup() { size(400, 400); frameRate(15); frameBufferList = new ArrayList<PImage>(); myArray = randomArray(20); bubbleSortSaveFrame(myArray); } void draw() { background(127); image(frameBufferList.get(frameCount % frameBufferList.size()), 0, 0); } int[] randomArray(int numOfElements) { int[] returnArray = new int[numOfElements]; for (int i = 0; i < returnArray.length; i++) { returnArray[i] = floor(random(0, height)); } return returnArray; } void displayArray(int[] arrToDisplay) { float step = float(width) / float(arrToDisplay.length); for (int i = 0; i < arrToDisplay.length; i++) { rectMode(CORNERS); rect(i*step, height, (i+1)*step, height-arrToDisplay[i]); } } void displayArrayBuffer(int[] arrToDisplay) { pg = createGraphics(width, height, P2D);
Note. Usually you use one buffer for each frame, in this case we create a buffer for each saved image so that you can run out of memory if you create thousands of images. Dose, who has another solution for using PGraphics / buffer without getting into memory?
source share