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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:01:21+00:00 2026-06-03T08:01:21+00:00

This is simple version of my problem. I have 3 classes: public class TopographyFrame

  • 0

This is simple version of my problem.
I have 3 classes:
public class TopographyFrame extends JFrame – simple JFrame with JPAnel and button
public class TopograpyPanel extends JPanel – JPanel to fill Rectangles
public class Siec – class to perform calculations and call repaint on JPAnale

in JPanel i overided paintComponent() method

public void paintComponent (Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        System.out.println(rectangles.length);
        for(int i = 0 ; i < rectangles.length ; i++){
            g2.setPaint(neurony[i].winner);
            g2.fillRect((int)rectangles[i].x,(int)rectangles[i].y,(int)rectangles[i].width, (int)rectangles[i].height);
        }
    }

neurony – array of objects with field public Color winner

in class Siec i have reference to JPanel to repaint it
in class JFrame i have a button with private action listener:

class MyListener implements ActionListener{
        Siec s;
        public MyListener(Siec s){
            this.s = s;
        }
        public void actionPerformed(ActionEvent arg0) {
            try {
                s.forPaint();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

method forPaint() in Siec looks like:

public void forPaint(){

        setTopography();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        setTopography();
    }
public void setTopography() {
    for (int i = 0; i < vector.colors.length; i++) {
            neurony[i].winner = vector.colors[(int)(random() * 900 % vector.colors.length)];
    }
    panel.repaint();
}

vector.color is array of Colors

So my problem is: when i click a button i would like to JPanel repaint immediately and then after 3 second repaint one more time. Insted JPanel repaints only one time after 3s delay.
}

  • 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-06-03T08:01:23+00:00Added an answer on June 3, 2026 at 8:01 am

    You can’t sleep, wait, or otherwise pause on the event handling thread, ever. Doing so blocks all events from being processed, including painting events. Your first painting can’t occur because you’re sleeping on the event thread, preventing it from happening.

    The right way to do any kind of animation — even simple stuff like this — is to create your own thread. That second thread can call repaint(), sleep for 3 seconds, then call repaint() again. The SwingWorker class is nominally a simpler way to do this, but in all honesty, beginners always find creating their own thread to be easier.

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

Sidebar

Related Questions

take this simple code: class A{ public: virtual void foo() = 0; void x(){
I am testing this sample on version 13 of android (3.2) and I have
This simple problem is kiling me. I posted something earlier about trying to clean
I was trying to make a tail-recursive version of this very simple SML function:
I have the following C++ code (simplified version): class Shape { bool isCircle =
Current situation: I have the current version of my MVC Framework which uses classes
The simple version is that I expect my __tostring method I have built in
I am struggling to understand this simple regular expression. I have the following attempt:
This simple example fails to compile in VS2K8: io_service io2; shared_ptr<asio::deadline_timer> dt(make_shared<asio::deadline_timer>(io2, posix_time::seconds(20))); As
This simple regex matching returns a string instead of an object on every browser

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.