Java Swing Sprite:如何模拟滚动骰子

时间:2015-02-04 12:20:27

标签: java swing sprite

在我们使用Java开发的游戏板中,我们希望在按下按钮时显示一种滚动骰子。我们试图将以下图像用作精灵:

enter image description here

所以当按下按钮时,会发生什么:

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

其中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;
}

问题是在动画过程中没有显示任何内容:我希望看到许多骰子面孔一个接一个地改变,直到for循环结束但没有显示任何内容......我唯一看到的是发布结果根据已推出骰子的彩色典当但同时没有任何东西......你能帮助我吗?

1 个答案:

答案 0 :(得分:2)

如果我正确理解您的代码,您会尝试在同一个for循环中多次更新UI。这不是动画的工作方式。您需要一个定时通知您移动到下一帧才能查看的计时器。

在Java教程中,了解如何使用计时器的好地方是here