1

In a game board we are developing in Java we want to show a sort of rolling dice when a button is pressed. We are trying to use the following image as a sprite:

enter image description here

so when the button is pressed, here's what happens:

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

where exactDieFaces contains the dice faces according to the pawn color which is launching and that face will be shown after the simulated rolling has finished; on the other hand, animationBuffer contains 40 or 50 random faces of all colors taken from the sprite image like that:

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

The problem is that nothing is shown during the animation: I expected to see many dice faces change one after the other until the for cycle ends but nothing is shown...The only thing I see is the launch result according to the color pawn which has launched the die but meanwhile there is nothing...can you help me?

SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Hot link to the image. – Andrew Thompson Feb 04 '15 at 12:22
  • `resultDie.setBounds(..)` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 04 '15 at 12:23
  • I am afraid I can't post a MCVE because that class is part of big RMI game and it can't be used as stand alone... – SagittariusA Feb 04 '15 at 12:24
  • 1
    I voted to close because.. *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the **shortest code necessary to reproduce it** in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."* If you cannot boil the problem down to an MCVE, I don't see this question as suitable for SO. – Andrew Thompson Feb 04 '15 at 12:29

1 Answers1

2

If I understand your code correctly you are attempting to update the UI many times within the same for loop. That's not how animation works. You need a timer which regularly notifies you to move to the next frame to view.

A good place to start in understanding how to use timers is here in the Java tutorial.

sprinter
  • 27,148
  • 6
  • 47
  • 78
  • I have already used Swing Timer but I am not understanding how to use it here... would you please write a sample code? – SagittariusA Feb 04 '15 at 13:40