Java Swing Dice滚动动画

时间:2011-12-12 04:29:23

标签: java swing animation

我正在编写一个掷骰子的GUI游戏。有一个名为“roll”的JButton,当点击时掷骰子进行游戏。然后,GUI使用jpeg的模具面显示您滚动的内容。

一切都很好,除了我现在应该向GUI添加动画。我的想法是以某种方式使用显示jpeg的相同方法以某种方式在短时间内快速显示不同的面值(模拟“滚动”)。但是,我相信大家都知道,这不起作用。

我熟悉EDT和Timer类的想法,但我不确定如何使用它们。基本上我希望这个动画在我点击“滚动”按钮时发生,并且当动画完成时,我希望它显示实际上像以前一样滚动的内容。

非常感谢任何帮助。这是我到目前为止的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


/* This is the GUI declaration */
public class NCrapsGUI extends JFrame {
     //code...   
/* Action when "roll" is clicked */
    private void rollActionPerformed(java.awt.event.ActionEvent evt){                                         
        game.rollDice();
            //Rolls both die
            sumOfDice.setText(Integer.toString(game.getSum()));
            //Displays the sum of the die
            numRolls.setText(Integer.toString(game.getNumRolls()));
            //Displays the number of rolls in each game
            // <editor-fold defaultstate="collapsed" desc="Die JPEG's">
            // If statements display the die face based on the number rolled
            if (game.getDie1Value() == 1) {
                 die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg")));
            }
            if (game.getDie1Value() == 2) {
                die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg")));
            }
            if (game.getDie1Value() == 3) {
                die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face3.jpg")));
            }
            if (game.getDie1Value() == 4) {
                die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face4.jpg")));
            }
            if (game.getDie1Value() == 5) {
                die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face5.jpg")));
            }
            if (game.getDie1Value() == 6) {
                die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face6.jpg")));
            }
            if (game.getDie2Value() == 1) {
                die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg")));
            }
            if (game.getDie2Value() == 2) {
                die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg")));
            }
            if (game.getDie2Value() == 3) {
                die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face3.jpg")));
            }
            if (game.getDie2Value() == 4) {
                die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face4.jpg")));
            }
            if (game.getDie2Value() == 5) {
                die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face5.jpg")));
            }
            if (game.getDie2Value() == 6) {
                die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face6.jpg")));
            }
            //</editor-fold>
            /* 
             * If the game is beyond the first roll, it checks to see if the sum of the
             * values rolled equal 7 or the point value for a loss or win respectively.
             * If it is a win, it adds a win. Likewise for a loss.
             */
            if (game.getGameStatus() == 2) {
                if (game.getSum() == game.getPoint()) {
                    game.addWin();
                    numWins.setText(Integer.toString(game.getWins()));
                    game.setGameStatus(1);
                    game.setPoint(0);
                    game.resetRolls();
                    return;
                }
                if (game.getSum() == 7) {
                    game.addLoss();
                    numLosses.setText(Integer.toString(game.getLosses()));
                    game.setGameStatus(1);
                    game.setPoint(0);
                    game.resetRolls();
                    return;
                }
            }

            /*
             * This checks to see if the game is on the first roll. If it is, it checks 
             * if the sum of the die is 7 or 11 for a win, or 2, 3, or 12 for a loss. If
             * not, it passes it on to the next roll and sets the point value to the sum
             */
            if (game.getGameStatus() == 1) {
                game.setPoint(game.getSum());
                dieSum.setText(Integer.toString(game.getPoint()));

                if (((game.getSum() == 7) || ((game.getSum() == 11)))) {
                    game.addWin();
                    numWins.setText(Integer.toString(game.getWins()));
                    game.setPoint(0);
                    dieSum.setText(Integer.toString(game.getPoint()));
                    game.resetRolls();
                    return;
                }

                if (((game.getSum() == 2) || ((game.getSum()) == 3)) || (game.getSum()) == 12) {
                    game.addLoss();
                    numLosses.setText(Integer.toString(game.getLosses()));
                    game.setPoint(0);
                    dieSum.setText(Integer.toString(game.getPoint()));
                    game.resetRolls();
                    return;
                } else {
                    game.setGameStatus(2);
                }
            }
       }                                    

使用更新代码编辑!!!

这里是声明Timer和数组的地方:

public class NCrapsGUI extends JFrame
{
private Timer timer; 
private int numPlayers;
private int totalIcons = 6;

private ImageIcon imageArray[];`

/* CODE */

这里是NCrapsGUI构造函数中填充数组的地方:

imageArray = new ImageIcon[totalIcons];
   for (int i = 0; i < 6 ;i++)
    {
        int temp = i + 1;
        imageArray[i] = new ImageIcon("face" + temp + ".jpg");
    }

    initComponents();`

这是整个rollActionPerformed方法。我猜测Timer正确启动 在开始时,但每当我尝试启动它时,我都会遇到大量错误。但是,当我 我单独创建了一个新的JPanel,并使它实现了动作监听器,我没有 错误。我尝试将实现ActionListner添加到此声明,但字面上是NetBeans 不会让我输入任何东西。

private void rollActionPerformed(java.awt.event.ActionEvent evt) {                                     



game.rollDice();
//Rolls both die

sumOfDice.setText(Integer.toString(game.getSum()));
//Displays the sum of the die

numRolls.setText(Integer.toString(game.getNumRolls()));
//Displays the number of rolls in each game

// <editor-fold defaultstate="collapsed" desc="Die JPEG's">
// If statements display the die face based on the number rolled
if (game.getDie1Value() == 1) 
{
    die1Disp.setIcon(imageArray[0]);
}

if (game.getDie1Value() == 2) 
{
    die1Disp.setIcon(imageArray[1]);
}

if (game.getDie1Value() == 3)
{
    die1Disp.setIcon(imageArray[2]);
}

if (game.getDie1Value() == 4) {
    die1Disp.setIcon(imageArray[3]);
}

if (game.getDie1Value() == 5) {
    die1Disp.setIcon(imageArray[4]);
}

if (game.getDie1Value() == 6) 
{
    die1Disp.setIcon(imageArray[5]);
}

if (game.getDie2Value() == 1) 
{
    die2Disp.setIcon(imageArray[0]);
}

if (game.getDie2Value() == 2) 
{
    die2Disp.setIcon(imageArray[1]);
}


if (game.getDie2Value() == 3) 
{
    die2Disp.setIcon(imageArray[2]);
}

if (game.getDie2Value() == 4)
{
    die2Disp.setIcon(imageArray[3]);
}

if (game.getDie2Value() == 5) 
{
    die2Disp.setIcon(imageArray[4]);
}

if (game.getDie2Value() == 6) 
{
    die2Disp.setIcon(imageArray[5]);
}

//</editor-fold>

/* 
 * If the game is beyond the first roll, it checks to see if the sum of the
 * values rolled equal 7 or the point value for a loss or win respectively.
 * If it is a win, it adds a win. Likewise for a loss.
 */

if (game.getGameStatus() == 2) {
    if (game.getSum() == game.getPoint()) {
        game.addWin();
        numWins.setText(Integer.toString(game.getWins()));
        game.setGameStatus(1);
        game.setPoint(0);
        game.resetRolls();
        return;
    }

    if (game.getSum() == 7) {
        game.addLoss();
        numLosses.setText(Integer.toString(game.getLosses()));
        game.setGameStatus(1);
        game.setPoint(0);
        game.resetRolls();
        return;
    }
}

`

1 个答案:

答案 0 :(得分:6)

我认为你的动画背后的基本理念是一个很好的理念,但是它是否有效当然都在实现细节中。我建议

  • 您在图片中读到并创建了ImageIcons一次,可能是在程序开始时。
  • 您将图标放入长度为7的ImageIcon数组中 - 但是您将在1-6个插槽中放置一个图标,将第0个项目保留为空。
  • 您使用Swing Timer以适当的延迟(例如200或300毫秒)随机交换这些图标。
  • 您使用Random对象获取1到6之间的随机数,然后使用此数字作为数组索引,从阵列中取出Icon。
  • 您在JLabel中显示ImageIcons(如果您正在显示2个模具,则显示两个JLabel)并通过简单地调用JLabel的setIcon(...)方法来交换图标。

修改
您在评论中说明了您尝试过:

timer = new Timer(100,this);

这就是你的问题 - 你使用this。您不应该尝试为所有内容使用相同的ActionListner。而是在那里创建一个ActionListener,您需要它。像,

  timer = new Timer(100, new ActionListener() {
     public void actionPerformed(ActionEvent actionEvt) {
        // ... put your ActionListener's code here 
     }
  });