Java Swing Dice Rolling Animation

I am coding a graphic craps game. There is a JButton called “roll,” which when clicked on it rolls the dice for the game. The GUI then displays what you downloaded using jpeg of die faces.

Everything works fine, except that I have to add animation to the GUI. My idea was to somehow quickly display different face values ​​in a short period of time (simulating a “roll”) using the same jpeg display method. However, as I am sure you all know, this does not work.

I am familiar with the idea of ​​EDT and the Timer class, but I'm not sure how to use them. Basically, I want this animation to happen when I clicked the Key button, and when the animation is finished, I want it to display what was actually rolling as before.

Any help would be greatly appreciated. Here is the code that I still have:

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

CHANGE WITH UPDATED CODE !!!

Here, where the timer and array are declared:

 public class NCrapsGUI extends JFrame { private Timer timer; private int numPlayers; private int totalIcons = 6; private ImageIcon imageArray[];` /* CODE */ 

And this is where the array is populated inside the NCrapsGUI constructor:

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

This is the whole rollActionPerformed method. I assume that the timer will start correctly at the beginning, but whenever I try to start it, I get a lot of errors. However, when I made the new JPanel separately and made it the executor of the action, I did not receive an error. I tried adding to the ActionAdistEventListner, but NetBeans literally did not let me type anything.

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

`

+6
source share
1 answer

Your main idea of ​​animation is a good one, I think, but whether it works or not, everything is in the implementation details, of course. I suggest

  • What do you read on your images and do ImageIcons once, perhaps at the beginning of the program.
  • So that you put the icons in an ImageIcon array of length 7, but you put the icon in 1-6 slots, leaving the 0th element zero.
  • To use the Swing Timer to randomly exchange these icons with some appropriate delay, say 200 or 300 ms.
  • That you use a random object to get a random number between 1 and 6, and then with that number as the index of the array, get the icon from the array.
  • So that you display ImageIcons in JLabel (or two JLabels if you show 2 die) and change the icons just by calling the setIcon(...) method setIcon(...) .

Edit
You indicate in your comment that you have tried:

 timer = new Timer(100,this); 

And that is your problem - your use of this . You should not try to use the same ActionListner for everything. Instead, create an ActionListener right where you need it. Sort of,

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

Source: https://habr.com/ru/post/903569/


All Articles