KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > Animator


1 /*
2  * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.animation;
32
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35
36 import javax.swing.Timer JavaDoc;
37
38 /**
39  * Starts and stops an animation and triggers
40  * the animation at a given frame rate.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.3 $
44  */

45 public final class Animator implements ActionListener JavaDoc {
46
47     private final Animation animation;
48     private final Timer JavaDoc timer;
49     private final int framesPerSecond;
50
51     private long startTime;
52     private long elapsedTime = 0;
53
54     
55     // Instance Creation *****************************************************
56

57     /**
58      * Constructs an <code>Animator</code> for the given animation and
59      * frame rate.
60      *
61      * @param animation the animation to animate
62      * @param framesPerSecond the desired frame rate
63      * @throws NullPointerException if the animation is <code>null</code>
64      * @throws IllegalArgumentException if the frame rate is non-positive
65      */

66     public Animator(Animation animation, int framesPerSecond) {
67         if (animation == null)
68             throw new NullPointerException JavaDoc("The animation must not be null.");
69
70         if (framesPerSecond <= 0)
71             throw new IllegalArgumentException JavaDoc("The frame rate must be positive.");
72
73         this.animation = animation;
74         this.framesPerSecond = framesPerSecond;
75         this.timer = createTimer(framesPerSecond);
76     }
77     
78     
79     // ************************************************************************
80

81     /**
82      * Returns the animator's animation.
83      *
84      * @return the animator's animation
85      */

86     public Animation animation() {
87         return animation;
88     }
89     
90
91     /**
92      * Returns the desired frame rate.
93      *
94      * @return the desired frame rate per second
95      */

96     public int framesPerSecond() {
97         return framesPerSecond;
98     }
99     
100
101     /**
102      * Returns the elapsed time since animation start.
103      *
104      * @return time elapsed since the animation start
105      */

106     public long elapsedTime() {
107         return timer.isRunning()
108             ? System.currentTimeMillis() - startTime + elapsedTime
109             : elapsedTime;
110     }
111     
112
113     /**
114      * Starts the animator and in turn the animation.
115      */

116     public void start() {
117         if (!timer.isRunning()) {
118             registerStopListener();
119             startTime = System.currentTimeMillis();
120             timer.start();
121         } /*else {
122                 System.out.println("Animator is already running");
123                 } */

124     }
125     
126
127     /**
128      * Stops the animator.
129      */

130     public void stop() {
131         if (timer.isRunning()) {
132             elapsedTime = elapsedTime();
133             timer.stop();
134         }
135     }
136
137
138     /**
139      * Implements the ActionListener interface for use by
140      * the <code>Timer</code>.
141      *
142      * @param e the action event
143      */

144     public void actionPerformed(ActionEvent JavaDoc e) {
145         animation.animate(elapsedTime());
146     }
147     
148
149     /**
150      * Returns a string representation for the animator.
151      *
152      * @return a string representation for the animator
153      */

154     public String JavaDoc toString() {
155         return "elapsedTime=" + elapsedTime() + "; fps=" + framesPerSecond;
156     }
157     
158     
159     // Helper Code ************************************************************
160

161     /**
162      * Creates and configures a <code>Timer</code> object.
163      *
164      * @param fps the frames per second
165      * @return a <code>Timer</code> with the specified frame rate
166      */

167     private Timer JavaDoc createTimer(int fps) {
168         int delay = 1000 / fps;
169
170         Timer JavaDoc aTimer = new Timer JavaDoc(delay, this);
171         aTimer.setInitialDelay(0);
172         aTimer.setCoalesce(true);
173         return aTimer;
174     }
175
176     
177     /**
178      * Registers a listener that stops the animator if the animation stopped.
179      */

180     private void registerStopListener() {
181         animation.addAnimationListener(new AnimationAdapter() {
182             public void animationStopped(AnimationEvent e) {
183                 //System.out.println("All animations stopped.");
184
stop();
185                 //animation.animate(animation.duration());
186
}
187         });
188     }
189     
190     
191 }
Popular Tags