Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. /**
  9. * This class, designed to be used in conjunction with Paint, makes it possible
  10. * to create animated drawings.
  11. *
  12. */
  13.  
  14. public class PaintAnimator extends TimerTask {
  15.  
  16. /**
  17. * The objects to be animated at each time step.
  18. */
  19.  
  20. private List<Object> toanimate_ = new ArrayList<Object>();
  21.  
  22. /**
  23. * The timer that invokes the animation.
  24. */
  25.  
  26. private Timer timer_;
  27.  
  28. /**
  29. * Create a new animator which will perform one animation step every 'delay'
  30. * milliseconds.
  31. *
  32. * @param delay
  33. * number of ms between animation steps
  34. */
  35.  
  36. public PaintAnimator ( long delay ) {
  37. timer_ = new Timer();
  38. timer_.schedule(this,delay,delay);
  39. }
  40.  
  41. /**
  42. * Start animating an object. The object must have a method of the form
  43. *
  44. * <pre>
  45. * public void doAnimateStep () { ... }
  46. * </pre>
  47. *
  48. * doAnimateStep will be called for each step of the animation, and should
  49. * handle updating whatever needs to be updated (e.g. changing the position)
  50. * and drawing the new thing.
  51. *
  52. * @param obj
  53. * object to animate
  54. */
  55.  
  56. public void animate ( Object obj ) {
  57. toanimate_.add(obj);
  58. }
  59.  
  60. /**
  61. * Stop animating an object. doAnimateStep() will no longer be called for the
  62. * specified object.
  63. *
  64. * @param obj
  65. * object to stop animating
  66. */
  67.  
  68. public void unanimate ( Object obj ) {
  69. toanimate_.remove(obj);
  70. }
  71.  
  72. /**
  73. * Carry out one animation step for each thing being animated. This method is
  74. * used by the Java system, and should not be called directly.
  75. */
  76. @Override
  77. public void run () {
  78. // clear the window
  79. Paint.clear();
  80.  
  81. // animate the objects
  82. for ( Object obj : toanimate_ ) {
  83. try {
  84. Method method =
  85. obj.getClass().getMethod("doAnimateStep",(Class[]) null);
  86. method.invoke(obj,(Object[]) null);
  87.  
  88. } catch ( SecurityException e ) {
  89. System.out.println("something went wrong with animating object " + obj);
  90. e.printStackTrace();
  91. } catch ( NoSuchMethodException e ) {
  92. System.out.println("cannot animate object " + obj + " because it "+
  93. "does not have a doAnimateStep() method");
  94. e.printStackTrace();
  95. } catch ( IllegalArgumentException e ) {
  96. System.out.println("something went wrong with animating object " + obj);
  97. e.printStackTrace();
  98. } catch ( IllegalAccessException e ) {
  99. System.out.println("something went wrong with animating object " + obj);
  100. e.printStackTrace();
  101. } catch ( InvocationTargetException e ) {
  102. System.out.println("something went wrong with animating object " + obj);
  103. e.printStackTrace();
  104. }
  105.  
  106. }
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement