Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:35:23+00:00 2026-05-27T10:35:23+00:00

I’m coding up a GUI game of craps. There is a JButton called roll

  • 0

I’m coding up a GUI game of craps. There is a JButton called “roll” which when clicked rolls the dice for the game. The GUI then displays what you rolled using jpeg’s of die faces.

Everything works great, except I’m supposed to now add an animation to the GUI. My idea was to somehow rapidly display different face values for a short period of time (simulating a “roll”) using the same method of displaying the jpeg’s. However, as I’m sure you all know, that doesn’t work.

I’m familiar with the idea of EDT and the Timer class, but I’m not sure exactly how to use them. Basically I want this animation to happen when I hit the “roll” button, and when the animation is finished, I want it to display what was actually rolled like it did before.

Any help would be greatly appreciated. Here’s the code I have thus far:

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

EDIT WITH UPDATED CODE!!!

Here’s 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 here 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 entire rollActionPerformed method. I’m guessing the Timer gets started right
at the beginning, but whenever I try to start it I get loads of error. However, when I
I made a new JPanel seperately, and made it implement action listener, I got no
errors. I tried adding implements ActionListner to this declaration, but NetBeans literally
would not let me type anything in.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:35:24+00:00Added an answer on May 27, 2026 at 10:35 am

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

    • That you read in your images and make ImageIcons once, probably at the start of the program.
    • That you put the icons into an ImageIcon array with a length of 7 — but you’ll put an icon into the 1-6 slots, leaving the 0th item null.
    • That you use a Swing Timer to swap these icons randomly with some appropriate delay, say 200 or 300 msec.
    • That you use a a Random object to get a random number between 1 and 6, and then with this number as your array index, get the Icon out of the array.
    • That you display the ImageIcons in a JLabel (or two JLabels if you’re displaying 2 die) and swap Icons by simply calling the JLabel’s setIcon(...) method.

    Edit
    You state in your comment that you tried:

    timer = new Timer(100,this);
    

    And that’s your problem — your use of this. You shouldn’t try to use the same ActionListner for everything. Instead create an ActionListener right there, where you need it. Something like,

      timer = new Timer(100, new ActionListener() {
         public void actionPerformed(ActionEvent actionEvt) {
            // ... put your ActionListener's code here 
         }
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.