0

I want to animate the movement of a label that contains a truck image, i've seen some tutorial and everything works alright, but in my case the whole app gets i mean i want to move this truck from a starting point e.g. Truck.setBounds(0,0,50,50) then to a next point, then to another next point, until it reaches its final destination e.g. Truck.setBounds(1210,0,50,50)

Here is my code

     try
                {
                    int x  = 1;
                    while(true)
                    {
                        if(x==1)
                truck.setBounds(35,35,50,50);
                if(x==2)
                truck.setBounds(245,35,50,50);
                        if(x==3)
                truck.setBounds(445,35,50,50);
                        if(x==4)
                truck.setBounds(645,35,50,50);
                        if(x==5)
                truck.setBounds(845,35,50,50);
                        if(x==6)
                truck.setBounds(1045,35,50,50);
                        if(x==7)
                truck.setBounds(1245,35,50,50);
                Thread.sleep(1000);
                        x++;
                        if(x==8)
                            break;
                    }
                }
                catch(Exception ee)
                {
                    return;
                }

Again as i already said, the whole application gets sleep, then when it reaches it final destination e.g. x==7 it appears on the screen, it never appears on starting destination & neither on the next destination etc.

Last thing i'm not very familiar with java nor java swing animation, if you could edit my code to make it works i would be very thankful.

Mohamed Horani
  • 544
  • 3
  • 9
  • 23
  • 1
    use swing timer.you are blocking EDT – Madhawa Priyashantha Mar 12 '16 at 13:16
  • Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Mar 12 '16 at 13:24
  • To clarify the above comments. Don't use Thread.sleep(). That statement is what is blocking the EDT. – camickr Mar 12 '16 at 15:43
  • @Mohamed Horani You can take a look here: [Animating label](http://stackoverflow.com/questions/35225268/how-to-animate-jlabel-from-one-side-to-another-side-of-jframe-using-netbeans/35226214#35226214). It is not on Android, but you can take a look and see if it helps. – user3437460 Mar 12 '16 at 17:05
  • I'm sorry guys, i'm really thankful that all of you, are trying to help me, but i have no prior experience in java, if you could write some java code i would be more thankful to all of you – Mohamed Horani Mar 12 '16 at 21:51

1 Answers1

0

You cannot program event based GUIs this way.

You must never sleep in the main thread of your application. This will freeze everything and - on Android devices, at least - prompt to a "application unresponsive, kill it?" message.

Instead, create a worker/background thread. This thread now unfortunately cannot call your GUI methods (like setBound) directly. What it can do, is sleep. So your pseudo code looks like this:

# main thread

void onTruckMessageReceived(...) {
    ...
    truck.setBound(...);
}

# worker thread

void run() {
    while (notDoneYet) {
      Thread.sleep(1000);
      sendTruckMessageToMainThread();
      ...
    }
}

This should get you going in the general direction. Your API reference will have a topic on threads, background workers etc.; in the best case scenario, you can use an existing Timer class of your library.

If your are working on an Android app then google for AsyncTask, https://developer.android.com/reference/android/os/AsyncTask.html has a good example for you right at the top.

AnoE
  • 8,048
  • 1
  • 21
  • 36