Java Swing Sprite:如何模拟滚动骰子
在我们用Java开发的游戏板中,我们希望在按下按钮时显示一种滚动骰子。我们正在尝试将以下图像用作精灵: 因此,当按下按钮时,会发生以下情况:Java Swing Sprite:如何模拟滚动骰子,java,swing,sprite,Java,Swing,Sprite,在我们用Java开发的游戏板中,我们希望在按下按钮时显示一种滚动骰子。我们正在尝试将以下图像用作精灵: 因此,当按下按钮时,会发生以下情况: private void startAnimationDie(final JPanel panel) { int launchResult = /* getting a launch dice result from the core game (from 1 to 6)*/ new Thread() { public
private void startAnimationDie(final JPanel panel) {
int launchResult = /* getting a launch dice result from the core game (from 1 to 6)*/
new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
BufferedImage[] animationBuffer = initAnimationBuffer();
BufferedImage[][] exactDieFaces = initExactDieFaces();
int launchResult = coreGame.launchDie();
coreGame.getMyPartecipant().setLastLaunch(launchResult);
AnimationSprite animation = new AnimationSprite(animationBuffer, Constants.DIE_ANIMATION_SPEED);
animation.start();
JLabel resultDie = new JLabel();
resultDie.setBounds(60, 265, Constants.DIE_SIZE, Constants.DIE_SIZE);
for (int counter = 0; counter < Constants.DIE_ANIMATION_SPEED * 100; counter++) {
animation.update();
//panel.removeAll();
panel.updateUI();
//System.out.println("infor");
resultDie.setIcon(new ImageIcon(animationBuffer[counter % Constants.ROTATIONS]));
panel.add(resultDie);
panel.updateUI();
updateUI();
}
panel.removeAll();
panel.updateUI();
AnimationSprite resultAnimation = new AnimationSprite(exactDieFaces[launchResult - 1], 6);
resultAnimation.start();
resultAnimation.update();
System.out.println("0");
resultDie.setIcon(new ImageIcon(exactDieFaces[launchResult - 1][0]));
System.out.println("1");
resultDie.setBounds(60, 265, Constants.DIE_SIZE, Constants.DIE_SIZE);
System.out.println("2");
panel.add(resultDie);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
userPlayer.getGamePanel().makePossibleMoveFlash();
}
});
} catch (Exception ex) {
}
}
}.start();
private void startAnimationDie(最终JPanel面板){
int launchResult=/*从核心游戏中获得启动骰子结果(从1到6)*/
新线程(){
公开募捐{
试一试{
SwingUtilities.invokeAndWait(新的Runnable(){
公开募捐{
BuffereImage[]animationBuffer=initAnimationBuffer();
BuffereImage[]exactDieFaces=initExactDieFaces();
int launchResult=coreGame.launchDie();
coreGame.getMyPartecipant().setLastLaunch(launchResult);
AnimationSprite animation=新的AnimationSprite(animationBuffer,常数.DIE\u animation\u速度);
animation.start();
JLabel resultDie=新的JLabel();
结果:设置边界(60,265,常数。模具尺寸,常数。模具尺寸);
用于(int计数器=0;计数器<常数.DIE\u动画\u速度*100;计数器++){
animation.update();
//panel.removeAll();
panel.updateUI();
//System.out.println(“infor”);
resultDie.setIcon(新的图像图标(animationBuffer[计数器%常量.旋转]);
面板。添加(结果显示);
panel.updateUI();
updateUI();
}
panel.removeAll();
panel.updateUI();
AnimationSprite resultAnimation=新的AnimationSprite(exactDieFaces[launchResult-1],6);
resultAnimation.start();
resultAnimation.update();
系统输出打印项次(“0”);
resultDie.setIcon(新的图像图标(exactDieFaces[launchResult-1][0]);
系统输出打印项次(“1”);
结果:设置边界(60,265,常数。模具尺寸,常数。模具尺寸);
系统输出打印项次(“2”);
面板。添加(结果显示);
试一试{
睡眠(200);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
userPlayer.getGamePanel().makePossibleMoveFlash();
}
});
}捕获(例外情况除外){
}
}
}.start();
其中,exactDieFaces包含根据正在启动的棋子颜色的骰子面,该面将在模拟滚动完成后显示;另一方面,animationBuffer包含40或50个从精灵图像中提取的所有颜色的随机面,如下所示:
private BufferedImage[] initAnimationBuffer() {
BufferedImage[] result = new BufferedImage[Constants.ROTATIONS];
int rowSprite, colSprite;
Random random = new Random();
for (int i = 0; i < Constants.ROTATIONS; i++) {
rowSprite = 1 + random.nextInt(5);
colSprite = 1 + random.nextInt(5);
result[i] = DieSprite.getSpriteExact(0 /* offset */,colSprite, rowSprite);
}
return result;
}
private buffereImage[]initAnimationBuffer(){
BuffereImage[]结果=新的BuffereImage[常数.旋转];
冰晶,冰晶;
随机=新随机();
对于(int i=0;i
问题是在动画过程中没有显示任何内容:我希望看到许多骰子面一个接一个地改变,直到for循环结束,但没有显示任何内容…我唯一看到的是根据启动骰子的颜色棋子的启动结果,但同时没有显示任何内容…您能帮我吗?如果我理解您的代码正确地说,您正试图在同一for循环中多次更新UI。动画不是这样工作的。您需要一个计时器,它会定期通知您移动到下一帧进行查看
Java教程是了解如何使用计时器的一个很好的起点。1)要更快地获得更好的帮助,请发布(最简单的完整可验证示例)或(简短、自包含、正确的示例)。2)图像的热链接。
resultDie.setBounds(…)
Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。因此,它们不利于像素完美的布局。相反,使用布局管理器,或者与布局填充和边框一起使用。我恐怕不能发布MCVE,因为该类是大型RMI游戏的一部分,不能单独使用……我投票支持close因为..“寻求调试帮助的问题”(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建最小、完整且可验证的示例。”如果你不能将问题归结为MCVE,我认为这个问题不适合这样做。我已经使用了Swing Timer,但我不知道如何在这里使用它…请你写一个示例代码好吗?