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

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.