如何使用Thread.sleep来显示简单的掉落动画?
我正在制作一个游戏板,我想展示游戏板动画, 动画是一个物体像这样落下,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 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.
您不应该使用 Thread.sleep()。
您应该使用 Swing Timer。
You should not be using Thread.sleep().
You should be use a Swing Timer.