I made this Towers of Hanoi a few years ago and ran into the same issue. My solution was to put it on a separate Thread.
import java.util.Stack;
import java.lang.Runnable;
boolean start = true;
final int DELAY = 500;
final int OFFSET = 150;
final int RING_CURVE = 90;
final int RING_HEIGHT = 30;
final int RING_LENGTH_STEP = 10;
final int RING_START_HEIGHT = 370;
final int RING_MAX_LENGTH = 250;
final int PEG_LEFT = 175;
final int PEG_MIDDLE = 475;
final int PEG_RIGHT = 800-25;
final int NUMBER_OF_DISK = 9;
Stack<Ring>ringsA = new Stack<Ring>();
Stack<Ring>ringsB = new Stack<Ring>();
Stack<Ring>ringsC = new Stack<Ring>();
Stack[] tower = {ringsA, ringsB, ringsC};
void setup() {
size(1000, 500);
for (int i = 0; i < NUMBER_OF_DISK; i++) {
ringsA.push(new Ring(RING_MAX_LENGTH - i*20, color(random(255), random(255), random(255)), 20));
}
new Thread(new Runnable() {
@Override
public void run() {
towersOfHanoi(n, from, aux, to);
}
}
).start();
}
void drawBackground() {
background(100);
fill(200);
for (int i = 0; i < 3; i++) {
rect(OFFSET + i*300, 100, 50, 300, 10);
}
rect(0, 400, width, 50);
}
void towersOfHanoi(int n, int from, int aux, int to){
if(n==1){
move(from,to);
delay(DELAY);
}
else{
towersOfHanoi(n-1,from,to,aux);
move(from, to);
delay(DELAY);
towersOfHanoi(n-1,aux,from,to);
}
}
int n= NUMBER_OF_DISK;
int from = 0;
int aux = 1;
int to = 2;
void draw() {
refresh();
println("test");
}
void refresh() {
drawBackground();
int currentRingHeight = RING_START_HEIGHT;
for (int i = 0; i < ringsA.size(); i++) {
Ring r=ringsA.get(i);
fill(r.getColor());
rect(PEG_LEFT - (r.getSize()/2), currentRingHeight, r.getSize(), RING_HEIGHT, RING_CURVE);
currentRingHeight -= RING_HEIGHT;
}
currentRingHeight = RING_START_HEIGHT;
for (int i = 0; i < ringsB.size(); i++) {
Ring r=ringsB.get(i);
fill(r.getColor());
rect(PEG_MIDDLE - (r.getSize()/2), currentRingHeight, r.getSize(), RING_HEIGHT, RING_CURVE);
currentRingHeight -= RING_HEIGHT;
}
currentRingHeight = RING_START_HEIGHT;
for (int i = 0; i < ringsC.size(); i++) {
Ring r=ringsC.get(i);
fill(r.getColor());
rect(PEG_RIGHT - (r.getSize()/2), currentRingHeight, r.getSize(), RING_HEIGHT, RING_CURVE);
currentRingHeight -= RING_HEIGHT;
}
}
/* --=-- TOWER OF HANOI --=--
GOAL:
Move all of the rings from the leftmost peg to the rightmost peg.
RULES:
1. You can only move 1 ring at a time.
2. Rings cannot be placed on rings that are smaller than they are(ex. the bottem ring could not be placed on top ring)
*/
void move(int orgin, int dest) {
if (orgin != dest && orgin < 3 && dest < 3) {
if (!tower[orgin].isEmpty()) {
if (tower[dest].isEmpty()) {
tower[dest].push(tower[orgin].pop());
} else {
if (((Ring)tower[orgin].peek()).getSize() < ((Ring) tower[dest].peek()).getSize()) {
tower[dest].push(tower[orgin].pop());
} else {
System.err.println("RING TOO BIG");
return;
}
}
} else {
System.err.println("PEG DOES NOT HAVE RINGS");
}
}
//System.out.println("NUMBER ERROR");
}
class Ring {
int size;
color c;
int position;
Ring(int size, color c, int position) {
this.size = size;
this.c = c;
this. position = position;
}
color getColor() {
return c;
}
int getSize() {
return size;
}
int getPosition() {
return position;
}
void setPostition(int size) {
this.size = size;
}
}