So I’m writing a program that plays Reversi/Othello against a player. I wrote a method to make a short animation of the pieces flipping-
public void flip(int row, int col, Graphics window)
{
Color a;
if (pieces[row][col]==1)
a = Color.black;
else
a = Color.white;
for ( int size = 90; size>0; size-=2)
{
try { Thread.sleep(11,1111); } catch (InterruptedException exc){}
window.setColor(new Color( 0, 100, 0 ));
window.fillRect(row*100+3, col*100+3, 94, 94);
window.setColor(a);
window.fillOval(row*100 + 5, col*100+5+(90-size)/2, 90, size);
}
if (a==Color.black)
a=Color.white;
else
a=Color.black;
for ( int size = 0; size<90; size+=2)
{
try { Thread.sleep(11,1111); } catch (InterruptedException exc){}
window.setColor(new Color( 0, 100, 0 ));
window.fillRect(row*100+3, col*100+3, 94, 94);
window.setColor(a);
window.fillOval(row*100 + 5, col*100+5+(90-size)/2, 90, size);
}
}
It works well and looks great, but the problem is that since thread.sleep pauses the entire program, it can only flip one piece at a time. Is there something I can do to pause just that method without interrupting the rest of the program?
Thanks everyone. The new thread worked, but now I have a different problem. The three setcolor methods in the flip method are getting mixed up. I think it’s because some threads are setting the color to green, some to black and some to white. How do I fix this?
You should run
flipin separateThreadin that case. The simplest example: