Java摇摆骰子滚动动画
我正在编写一个骰子的GUI游戏。有一个叫做“roll”的JButton,当点击时掷骰子进行游戏。然后GUI会显示您使用jpeg模具面部滚动的内容。Java摇摆骰子滚动动画
一切工作很好,除了我现在应该添加一个动画到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);
}
}
}
编辑更新的代码!
在此处,定时器和数组声明:
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方法。我猜计时器在开始时就开始于 ,但每当我尝试启动它时,都会出现大量错误。但是,当我做了一个新的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;
}
}
`
动画背后的基本想法是好的,我认为,但它是否有效或者不是全部在实施细节当然。我建议你
- 你读你的图像和一次ImageIcons,可能在程序的开始。
- 将图标放入长度为7的ImageIcon数组中 - 但您会将图标放入1-6个插槽中,并将第0项留空。
- 您使用摆动计时器以某种适当的延迟随机交换这些图标,例如200或300毫秒。
- 您使用Random对象来获取1到6之间的随机数,然后将此数字作为数组索引,从数组中获取Icon。
- 通过简单地调用JLabel的
setIcon(...)
方法,您可以在JLabel中显示ImageIcons(或者如果显示2个die,则显示两个JLabel)并交换图标。
编辑
在你的评论说明你试过:
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
}
});
+1建议。首先,我还会着眼于解决渲染单个芯片的问题。 – trashgod
我是否将所有这些代码放入了我的rollActionPerformed方法中?除了填充数组。 – lessthanjacob
您将在构造函数中创建ImageIcons。 rollActionPerformed可能会由一个按钮启动,并启动定时器,该定时器将拥有自己的ActionListener,并且您可以随意交换图标,直到它重复x次(您将给它一个int计数器变量,检查)。 –
不知道是谁推倒你或者为什么(通过推倒推倒),但我投票决定抵消它。 –
发布http://sscce.org/ +1 – mKorbel