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

  • Home
  • SEARCH
  • 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 3322030
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:08:01+00:00 2026-05-17T23:08:01+00:00

I am writing a game using Java Swing. I want to paint each time

  • 0

I am writing a game using Java Swing. I want to paint each time a loop executes with a small delay in between to create a cascade effect on screen. I believe that the efficiency routines in the system are collapsing the calls to repaint() into a single call. At any rate, the changes all occur at once after the total delay. Is there some way to force the system to repaint immediately and then delay on each iteration of the loop?

My Code:

for(int i=0;i<10;i++){
    JButton[i].setBackground(Color.WHITE);
    JButton[i].repaint();
    Thread.sleep(2000);
    JButton[i].setBackground(Color.GRAY);
    JButton[i].repaint();
}
  • 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-17T23:08:02+00:00Added an answer on May 17, 2026 at 11:08 pm

    You can use JComponent.paintImmediately to force an immediate repaint.

    EDIT: After reading your question again it occurs to me that you might be executing your logic on the event dispatch thread. This would mean that the repaint requests will not be executed until after your method returns. If you put your code into another thread then that will probably fix the problem and it will be a lot nicer than using paintImmediately.

    void uiFunction() {
        new Thread() {
            public void run() {
                for(int i = 0; i < 10; i++) {
                    final JButton b = buttons[i];
                    SwingUtilities.invokeLater(new Runnable() {
                        b.setBackground(Color.WHITE);
                        b.repaint();
                    }
                    Thread.sleep(2000);
                    SwingUtilities.invokeLater(new Runnable() {
                        b.setBackground(Color.GRAY);
                        b.repaint();
                    }
                }
            }
        }.run();
    }
    

    It’s a bit mucky but hopefully it gives you an idea of where to begin.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in the process of writing a Java 2D game. I'm using the built-in
in my spare time I'm engaged with writing a little game using Microsofts XNA
I'm writing a game in Java, and I want the user to be able
I'm writing a Java game and I want to implement a power meter for
I'm writing a game in Java, right now it's Swing + JOGL - a
While writing a game for J2ME we ran into an issue using java.lang.Integer.parseInt() We
I am writing a simple checkers game in Java. When I mouse over the
I'm writing a small online flash game that keeps high scores of players. Obviously
Little background.. I'm in the process of making an OpenGL game using Java and
I am planning to write an simple 3d(isometric view) game in Java using jMonkeyEngine

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.