JHotDraw Pattern Language

Creating Animation


JHotDraw provides support for a basic form of animation where figures can be moved around the drawing following simple rules.

Animation is provided in JHotDraw by making a subclass of Drawing that implements the Animatable interface. This interface defines one method animationStep() that is called to render each frame of the animation. This allows animationStep() to perform incremental updates to the state of the drawing which if they occur frequently enough appear as animation.

This form of animation requires that each figure in the drawing has some rules to govern how it is updated on each new frame. This can be preformed directly inside Drawing where animationStep() specifies a transformation for every figure in the drawing. This is acceptable when all figures are to be animated uniformly but precludes different subclasses from exhibiting different behaviour.

An alternative approach is to implement the Animatable interface for each animating figure in an application. These figures may then define the animationStep() as they wish. The Drawing's role in this scenario is to implement an animationStep() method that enumerates through the collection of figures calling each animatable figure’s animationStep() method.

     public void animationStep() {
         Enumeration<Figure> k = figures();
         while (k.hasMoreElements()){
             Figure fig = k.nextElement();
             if(fig instanceof Animatable){
                 Animatable anim = (Animatable)fig;
                 anim.animationStep();
             }
         }
     }

Animation only works if the drawing is changed in very quick intervals JHotDraw does not provide any default mechanism to repeatedly call the animation method so this functionality must be created manually. The example program JavaDraw (JHotDraw5.1/CH/ifa/draw/samples/javadraw) creates a class Animator that spawns a separate thread to call animationStep() at regular intervals, developers wishing to create animation should reuse this class where possible in their systems (insert  import CH.ifa.draw.samples.javadraw.Animator at the top of a class to reference Animator).


Copyright Douglas Kirk