开发者

How to use Thread.sleep to show simple dropping animation?

I am making a game board and i would like to show the game board animation, The animation is a object dropping down like this,


time=0

○○○

○○○

○○○

○○○

time=1

●○○

○○○

○○○

○○○

time=2

●○○

●○○

○○○

○○○

time=3

●○○

●○○

●○○

○○○

time=4

●○○

●○○

●○○

●○○


 for (int row = 0; row < ROW_MAX; row++) {
      for (int col = 0; col < COLUMN_MAX; col++) {
          TempBoard[row][col]= hollow;
       }
 }
drawBoard(TempBoard);
Thread.sleep(1000);
TempBoard[0][column] = solid;
drawBoard(TempBoard);
Thread.sleep(1000);
TempBoard[1][column] = solid;
drawBoard(TempBoard);
Thread.sleep(1000);
TempBoard[2][column] = solid;
drawBoard(TempBoard);
Thread.sleep(100);
TempBoard[3][column] = solid;
drawBoard(TempBoard);
Thread.sleep(1000);

But i cannot get what i want,it shows time=4 directly and missed the steps in time=0-3 what is the problem? How can i fix it? Or any other simple way to do it? Thankyou.

The drawBoard method:

public void drawBoard(Disc[][] updateBoard) {
    Disc[][] tempBoard = updateBoard;
    for (int row = 0; row < ROW_MAX; row++) {
        for (int col = 0; col < COLUMN_MAX; col++) {
            if (tempBoard[row][col] == hollow) {
                //jbtBoard[row][col].setIcon(hollowImg);
                System.out.println(hollow);
            } else if (tempDisc[row][col] == solid) {
                //jbtBoard[row][col].setIcon(solidImg);
                System.out.println(solid);
        }
    }
}

However, When i run the code, the output is like this:


time=0,1,2开发者_运维百科,3

○○○

○○○

○○○

○○○

time=4

●○○

●○○

●○○

●○○



If you are doing this (within one callback) on the UI thread, the procedure will run like you said, but the UI thread will only be able to display the draw commands you give after your code has finished - which effectively means showing version 4.


You should not be using Thread.sleep().

You should be use a Swing Timer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜