i have two animation going on screen.
one animation is a rect going to right. i dont want this animation to stop
2nd animation is message coming on screen for 1 sec and removeing it.

right now 1st animation works fine but doesnt diplay. its be iam seting message="". but how can i display for 1 sec.

public class ...
...
int x = 0;
int y = 0;
int dx = 1;
String message = "hellow world";
...

public void start()
    {
        ...
        if(timer_class == null)
        {
            Timer timer_class = new Timer(10, this); 
        }
        else
        {
            timer_class.stop();
        }
        timer_class.start();                 
    }



public void actionPerformed(ActionEvent e)
    {
       x+=dx; //make 1st animation

            message = "";

        repaint();
    }


    public void paint(Graphics g)
    {
          g.drawRect(x,y,10,10);   //i dont want this animation to stop

              if(!message.equal(""))  //this should only come on screen for 1sec
                {
        g.drawString(message, 600, 200);
        }
    }   
}

Dani AI

Generated

Short answer: do not use Thread.sleep on the Swing EDT. Use a Swing timer (javax.swing.Timer) so the animation keeps running while the message times out. was right to suggest a timer, and correctly warned that sleeping the EDT will stop everything. , your counter approach works — here are a couple of safe, easy patterns and a few common bugs to avoid.

One-shot Swing Timer (simple, thread-safe):

// when showing the message
message = "hello world";
Timer clear = new javax.swing.Timer(1000, ev -> {
    message = "";
    repaint();            // update the UI after clearing
});
clear.setRepeats(false); // fire once
clear.start();
repaint();

Reuse the animation timer (no extra Timer objects):

// when showing the message
message = "hello";
messageExpiresAt = System.nanoTime() + 1_000_000_000L; // 1 second

// inside your animation timer's actionPerformed
if (message != null && System.nanoTime() > messageExpiresAt) {
    message = "";
}
repaint();

Gotchas and fixes:

  • Do not redeclare the timer inside start(): write timer_class = new Timer(...) (no type) so you assign the field instead of creating a local variable.
  • Fix string checks: use message != null && !message.isEmpty() or !message.equals("")equal (no s) is a compile error.
  • Prefer overriding paintComponent(Graphics g) on a JPanel and call super.paintComponent(g) before drawing; paintComponent is the right hook for Swing painting.
  • Use javax.swing.Timer (runs on EDT). If you ever use java.util.Timer, update Swing components with SwingUtilities.invokeLater.

If things still misbehave, add temporary logging to confirm timers start and that repaint() is called, and verify you are not blocking the EDT anywhere.

Recommended Answers

All 5 Replies

use a timer, that runs for one second, after that, setText("")

i am not sure how to run timer for one sec. do i only use one timer? or two timer? one for each animation.

even easier is to let your application "sleep" for one second.
just check the Sleep method from the Thread class.

I don't agree with sleep here. The "application" can't sleep, only a thread can sleep. After the start() method everything in this code runs on the Swing EDT, and sleeping that thread will kill the animation (and any other swing activity).
Three solutions:
1. When you set the text, set a counter to 100. In your timer's actionPerformed dcrement the counter. When it gets to zero reset the text.
2. When you set the text get the system time, and 1 sec to it and save it in a variable. In the actionPeformed check the current system time against that variable to reset the text.
3. When you set the text start a new Timer with its own actionPerformed and a delay of 1 sec. In that actionPeformed reset the text and stop the timer.

Option 2 is probably the easiest/safest for you to code, but 3. is how Swing does stuff like popping up hints when you hover over a control for a second.

commented: agreed +14

When you set the text, set a counter to 100. In your timer's actionPerformed dcrement the counter. When it gets to zero reset the text.

i just tried this solution. its simple and works great for what iam trying to do. thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.