如何使用Thread.sleep来显示简单的掉落动画?

发布于 2024-10-28 21:33:23 字数 1539 浏览 0 评论 0原文

我正在制作一个游戏板,我想展示游戏板动画, 动画是一个物体像这样落下,


time=0
○○○
○○○
○○○
○○○
时间=1
●○○
○○○
○○○
○○○
时间=2
●○○
●○○
○○○
○○○
时间=3
●○○
●○○
●○○
○○○
时间=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);

但我无法得到我想要的,它直接显示time=4,错过了time=0-3的步骤,请问是什么问题?我该如何修复它?或者还有其他简单的方法吗?谢谢。
drawBoard 方法:

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);
        }
    }
}

但是,当我运行代码时,输​​出如下:


time=0,1,2,3
○○○
○○○
○○○
○○○
时间=4
●○○
●○○
●○○
●○○


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
●○○
●○○
●○○
●○○


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-11-04 21:33:23

如果您在 UI 线程上执行此操作(在一个回调内),该过程将像您所说的那样运行,但 UI 线程将只能在代码完成后显示您给出的绘制命令 - 这实际上意味着显示版本 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.

梦在夏天 2024-11-04 21:33:23

您不应该使用 Thread.sleep()。

您应该使用 Swing Timer

You should not be using Thread.sleep().

You should be use a Swing Timer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文