AnimationController.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package animation;
  2. public class AnimationController {
  3. private Action next;
  4. private boolean continuous;
  5. private long lastTime;
  6. private long timeBetween;
  7. public AnimationController()
  8. {
  9. next = null;
  10. continuous = false;
  11. lastTime = 0;
  12. timeBetween = 0;
  13. }
  14. public Action getNextAction() throws InterruptedException
  15. {
  16. long old = lastTime;
  17. Action ret = null;
  18. synchronized( this ) {
  19. while( next == null )
  20. wait();
  21. lastTime = System.currentTimeMillis();
  22. ret = next;
  23. }
  24. if( !continuous )
  25. next = null;
  26. else
  27. {
  28. if( lastTime - old < timeBetween )
  29. {
  30. Thread.sleep( timeBetween - ( lastTime - old ) );
  31. System.out.println( "sleep: " + ( timeBetween - ( lastTime - old ) ) );
  32. lastTime = System.currentTimeMillis();
  33. }
  34. }
  35. return ret;
  36. }
  37. public void setContinuous( boolean c )
  38. {
  39. this.continuous = c;
  40. }
  41. public void setTimeBetween( long between )
  42. {
  43. timeBetween = between;
  44. }
  45. public void setNextAction( Action a )
  46. {
  47. next = a;
  48. synchronized( this ) {
  49. notify();
  50. }
  51. }
  52. public boolean isContinuous()
  53. {
  54. return continuous;
  55. }
  56. }