0

I am not so sure how exactly does people create animation in java such as translating a square across the screen. When I try to change the position of a rectangle using a for loop everything gets executed instantly and I end up with the end coordinate of the rectangle. So what I want to know is how do people achieve these smooth animation overtime and if I want to create my custom animation class for whatever reason, how would I add a delay to make it appear as animation instead of instant position change in java?

  • In a nutshell: running something like `rectangle.x = (System.currentTimeInMillis() - startTimeInMillis) * 0.5 + 10;` every few milliseconds. Or just running `rectangle.x += 2;` every few milliseconds. – user253751 May 13 '16 at 04:02
  • Hey Thanks. how would I do rectangle.x every millisecond without causing a thread to sleep or infringe upon other event from executing? – AnotherRandomUser May 13 '16 at 04:03
  • You need to have an understand of concurrency. Essentially, you have a thread/timer which ticks in the background, which sends notifications to observers which update their states based on their requirements and then which are rendered to the screen (in a separate thread, often known as the Event Dispatching Thread). The method by which you ultimatly achieve this, will be depend on the framework you are using – MadProgrammer May 13 '16 at 04:10
  • For [example](http://stackoverflow.com/questions/28619150/move-image-in-a-spiral-fashion-in-java/28619554#28619554), [example](http://stackoverflow.com/questions/30433074/jpanel-image-flies-from-the-screen/30433207#30433207), [example](http://stackoverflow.com/questions/34119221/java-fade-in-and-out-two-jpanels-at-the-same-time/34123681#34123681). Animation is a complex example ([for example](http://stackoverflow.com/questions/28335787/how-can-i-implement-easing-functions-with-a-thread/28338188#28338188)) and from my experience you're better of using dedictated libraries for these tasks – MadProgrammer May 13 '16 at 04:12
  • Wow I think I kind of of get it now. So in order to do some animation like that and still listens to the user, I need to have different thread right? But isn't that kind of in convenient? What if I have multiple animation that I need to run but a thread can't run all of them because the animation timing is different? Does this mean I have to use even more thread for those multiple animations? – AnotherRandomUser May 13 '16 at 04:13
  • @AnotherRandomUser *"What if I have multiple animation that I need to run but a thread can't run all of them because the animation timing is different?"* - This is when you would use a single thread that generates "ticks" at a constant rate, all your animations would observe this and make appropriate changes based on their needs. This is how something like the TimingFramework works – MadProgrammer May 13 '16 at 07:53
  • @AnotherRandomUser *"I need to have different thread right? But isn't that kind of in convenient?"* - You'll find that almost all GUI frameworks work on this simple principle, one thread to manage all the events going on in the UI, it's actually a area of academic research (to create a thread safe framework which is actually usable). There are a number of frameworks and approaches which can help, depending on which framework your using – MadProgrammer May 13 '16 at 07:57
  • Look at JavaFX and transitions. – ManoDestra May 13 '16 at 13:36

2 Answers2

0

You can simply store the x and y position of the rectangle. Then run a while loop where you check if a given amount of time has passed. If it has, increment the position variables.

Code (will require minor adjustments to meet your needs):

int tick = 500; //set this to whatever you want
long lastTime = System.currentTimeInMillis();
while(true) {
 long currTime = System.currentTimeInMillis();
 if (currTime > lastTime + tick) {
  lastTime = currTime;
  x = x + 1; //replace this with your object's position
 }
}
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • Hey that work thanks but I have a quick question. This thing is run on a separate thread correct? What if I want to do 10 different animation do I need 10 thread for it to work and how would I do that when my computer only has 8? I just want to test. – AnotherRandomUser May 13 '16 at 05:16
  • First, you can still create 10 threads on a machine with 8 cores. The computer will abstract it out and run the threads on the same core in parallel. Second, you don't HAVE to to run this on a separate thread, but it's a good idea. – nhouser9 May 13 '16 at 05:45
  • Just beware that depending on the framework, you could risk race conditions between the "ticker" and the "renderer" (or if you're running in the "event" thread, prevent further interactions with the UI) – MadProgrammer May 13 '16 at 07:51
  • @AnotherRandomUser Use a single thread which generates ticks at a constant state, allow all you other animations to observe this and make changes accordingly, the TimingFramework works this way. – MadProgrammer May 13 '16 at 07:55
-1

You could add a delay by using the method Thread.sleep(long millis) which will halt the execution of the current thread by the specified number of milliseconds. There is probably a better way however.

Pyrrhic
  • 65
  • 10
  • Hey thanks for the answer but I am not so sure of thread.sleep is the best option because lets said you are creating a menu right? What if you tells the thread to sleep for some animation but then how will it respond to the user click if it sleep? – AnotherRandomUser May 13 '16 at 03:55
  • I don't believe it would be able to register the user's click while it is sleeping. – Pyrrhic May 13 '16 at 03:57
  • Yah I guess so. I been trying to google search on how this would be done but I have found nothing so far and I have been searching for over an hour now before coming here and asking the question. – AnotherRandomUser May 13 '16 at 04:00
  • @DogeOverlord That would assume a lot, if you're calling `Thread.sleep` in the frameworks "main" or "event" thread, then yes, it will cause the UI to stop responding – MadProgrammer May 13 '16 at 04:13