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 6927519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:03:58+00:00 2026-05-27T11:03:58+00:00

I’m having some trouble with an assignment I’m working on. Part of it is

  • 0

I’m having some trouble with an assignment I’m working on.

Part of it is to “animate” the rolling of a pair of die. I have 6 JPEGS, one of each of the sides. Instead of rolling, I want it to cycle through the images. I’m trying to use setIcon over and over in a loop, and use Thread.sleep() for a delay. This, among other things, all happens when the user hits a button (the event). Instead of displaying the images, it waits the correct amount of time and displays only the last image. Here’s the function I’m calling to perform the loop:

private void aniRoll(){
    for(int i = 0; i < 10; i++){
        die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg")));
        try{
            Thread.sleep(1000); // Sleep for 1 sec
        }
        catch(InterruptedException e){}
        die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg")));
        try{
            Thread.sleep(1000);
        }
        catch(InterruptedException e){}
        die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face3.jpg")));
        try{
            Thread.sleep(1000);
        }
        catch(InterruptedException e){}
        die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face4.jpg")));
        try{
            Thread.sleep(1000);
        }
        catch(InterruptedException e){}
        die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face5.jpg")));
        try{
            Thread.sleep(1000);
        }
        catch(InterruptedException e){}
        die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face6.jpg")));
        try{
            Thread.sleep(1000);
        }
        catch(InterruptedException e){}

    }

}

die1Disp is the name of my JLabel, aniRoll() is the function I’m calling to perform the loop (made private because I don’t want anything outside of my GUI class to call it), and the faceX.jpg are my images.

What I can’t understand is no matter where I put the Thread.sleep() it delays the program first, and then only displays the last image. For example:

die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg")));

die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg")));

//bunch of extra code

try{
    Thread.sleep(10000); // Sleep for 10 sec
}
catch(InterruptedException e){}

This will wait for 10 seconds and then display only the second image. It doesn’t blip the first quickly, display the second, then wait, it waits and only displays the second image. This quirk really has me baffled, and I’m not sure how to correct it. I’m using NetBeans IDE 7.01 as my editor and for the basic layout and code generation I’m using a Component Palette.

–Thanks

  • 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-27T11:03:58+00:00Added an answer on May 27, 2026 at 11:03 am

    First you must make the changes on the gui thread. See

    SwingUtils.invokeLater.

    Also Thread.sleep isn’t going to cut it, you should be using a javax.swing.Timer to specify when to switch icons.

    • 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 have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.