Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions

:
#)DefaultAnimator.java 1.0.1 2008-08-19 Copyright (c) 2008 Werner Randelshofer Hausmatt 10, CH-6405 Goldau, Switzerland All rights reserved. The copyright of this software is owned by Werner Randelshofer. You may not use, copy or modify this software, except in accordance with the license agreement you entered into with Werner Randelshofer. For details see accompanying license terms.
 
 
 package org.monte.media;
 
 import java.util.*;
DefaultAnimator.

Author(s):
Werner Randelshofer  @version 1.0.1 2008-08-19 finish method of interpolators was not always called.
1.0 Apr 28, 2008 Created.
 
 public class DefaultAnimator implements Animator {
     protected EventListenerList listenerList = new EventListenerList();
    
List of active interpolators. Implementation note: This vector is only accessed by the animationThread.
 
     private Vector activeInterpolators;
    
List of new interpolators. Implementation note: The dispatcher thread adds items to this list, the animationThread removes items. This queue is used to synchronize the dispatcher thread with the animation thread. Note: the dispatcher thread is not necesseraly the Event Dispatcher thread. The dispatcher thread is any thread which dispatches interpolators.
 
     private Vector newInterpolators;
    
    
We keep a reference to the animationThread to be able to stop it and to be able to wait until it is finished.
 
     private Thread animationThread;
     private boolean isAnimating;
     private Object lock = new Object();
     protected ChangeEvent changeEvent;
    
    
The sleep time controls the framerate of the animator. A value of 33 is approximately 30 frames per second.
 
     private int sleep = 33;
    
    
Creates a new instance.
 
     public DefaultAnimator() {
          = new Vector();
          = new Vector();
     }
    
    
Set the lock object, on which the animator synchronizes while animating the interpolators.
 
     public void setLock(Object lock) {
         this. = lock;
     }
     
     public boolean isActive() {
         return  != null;
     }
     
     public void start() {
         stop();
          = new Thread(this);
         .start();
     }
     
     public void stop() {
         if ( != null) {
             Thread t = ;
              = null;
             t.interrupt();
             try {
                 t.join();
             } catch (InterruptedException e) {
             }
         }
     }
    
    
Dispatches an interpolator for the animation thread. This will launch the animation thread if it is not already active.
 
     public void dispatch(Interpolator interpolator) {
         synchronized () {
            .addElement(interpolator);
            if (! isActive()) start();
        }
    }
    
    public void animateStep() {
        long now = System.currentTimeMillis();
        
        // Enqueue new interpolators into the activeInterpolators list
        // Avoid enqueuing new interpolators which must be run sequentally
        // with active interpolators.
        OuterLoop: for (int i=0; i < .size(); i++) {
            Interpolator candidate = (Interpolator.elementAt(i);
            boolean isEnqueueable = true;
            for (int j=0; j < ij++) {
                Interpolator before = (Interpolator.elementAt(j);
                if (candidate.isSequential(before)) {
                    isEnqueueable = false;
                    break;
                }
            }
            if (isEnqueueable) {
                for (int j=0; j < .size(); j++) {
                    Interpolator before = (Interpolator.elementAt(j);
                    if (candidate.replaces(before)) {
                        before.finish(now);
                    }
                    if (candidate.isSequential(before)) {
                        isEnqueueable = false;
                        break;
                    }
                }
            }
            if (isEnqueueable) {
                candidate.initialize(now);
                .addElement(candidate);
                .removeElementAt(i--);
            }
        }
        
        // Animate the active interpolators
        // Remove finished interpolators.
        for (int i=0; i < .size(); i++) {
            Interpolator active = (Interpolator.elementAt(i);
            if (active.isFinished()) {
                .removeElementAt(i--);
            } else if (active.isElapsed(now)) {
                active.finish(now);
                .removeElementAt(i--);
            } else {
                active.interpolate(now);
            }
        }
    }
    
    public void run() {
        //fireStateChanged();
        
        // Animation loop.
        // We loop here as long as we have something to do.
        while (Thread.currentThread() == ) {
            synchronized () {
                animateStep();
            }
            
            boolean hasFinished = false;
            synchronized () {
                if (.size() == 0 && .size() == 0) {
                     = null;
                    hasFinished = true;
                }
            }
            if (hasFinished) {
                fireStateChanged();
                return;
            }
            
            try {
                Thread.sleep(); // 30 frames per second
            } catch (InterruptedException e) {
            }
        }
        
        // If we have been stopped, we finish all interpolators.
        // Note: We get here only if method stop() has been called.
        //       If we finish by ourselves, we return from within the while loop
        //       above.
        synchronized () {
            synchronized () {
                long now = System.currentTimeMillis();
                for (int i=0; i < .size(); i++) {
                    Interpolator active = (Interpolator.elementAt(i);
                        active.finish(now);
                }
                for (int i=0; i < .size(); i++) {
                    Interpolator candidate = (Interpolator.elementAt(i);
                    candidate.initialize(now);
                    candidate.finish(now);
                }
                .removeAllElements();
                .removeAllElements();
            }
        }
        
        fireStateChanged();
    }
    public void addChangeListener(ChangeListener listener) {
        .add(ChangeListener.classlistener);
    }
    
    public void removeChangeListener(ChangeListener listener) {
        .remove(ChangeListener.classlistener);
    }
    
Notify all listeners that have registered interest for notification on this event type.
    protected void fireStateChanged() {
        // Guaranteed to return a non-null array
        Object[] listeners = .getListenerList();
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==ChangeListener.class) {
                // Lazily create the event:
                if ( == null) {
                     = new ChangeEvent(this);
                }
                ((ChangeListener)listeners[i+1]).stateChanged();
            }
        }
    }
    public boolean isSynchronous() {
        return false;
    }
New to GrepCode? Check out our FAQ X