KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > examples > basic > Basic


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.examples.basic;
32
33 import java.awt.*;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37
38 import javax.swing.BorderFactory JavaDoc;
39 import javax.swing.JFrame JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42
43 import com.jgoodies.animation.*;
44 import com.jgoodies.animation.animations.BasicTextAnimation;
45 import com.jgoodies.animation.components.BasicTextLabel;
46
47 /**
48  * A test class for a bunch of animations.
49  *
50  * @author Karsten Lentzsch
51  * @version $Revision: 1.4 $
52  *
53  * @see Animation
54  * @see BasicTextLabel
55  */

56 public final class Basic {
57     
58     private static final int DEFAULT_FRAME_RATE = 30;
59     
60     private JLabel JavaDoc reportLabel;
61     private BasicTextLabel label1;
62     private BasicTextLabel label2;
63     private BasicTextLabel label3;
64
65     
66     public static void main(String JavaDoc[] args) {
67         // Try to get the framerate from the command-line.
68
int fps = DEFAULT_FRAME_RATE;
69         if (args.length > 0) {
70             try {
71                 fps = Integer.parseInt(args[0]);
72             } catch (NumberFormatException JavaDoc e) {
73                 System.out.println("Could not parse the custom frame rate: " + args[0]);
74             }
75         }
76         System.out.println("The desired framerate is " + fps + '.');
77         new Basic(fps);
78     }
79     
80     
81     /**
82      * Constructs an animation demo using the specified frame rate.
83      *
84      * @param fps the frames per second
85      */

86     private Basic(int fps) {
87         createComponents();
88         buildFrame();
89         
90         Animator animator = new Animator(createComposedAnimation(), fps);
91         animator.start();
92     }
93
94
95     // Building ***************************************************************
96

97     /**
98      * Creates and configures the components used by the animations.
99      */

100     private void createComponents() {
101         Font font = new Font("dialog", Font.BOLD, 16);
102
103         reportLabel = new JLabel JavaDoc();
104
105         label1 = new BasicTextLabel(" ");
106         label1.setFont(font);
107
108         label2 = new BasicTextLabel(" ");
109         label2.setFont(font);
110         
111         label3 = new BasicTextLabel(" ");
112         label3.setFont(font);
113     }
114     
115
116     /**
117      * Builds and opens the demo frame.
118      */

119     private void buildFrame() {
120         JFrame JavaDoc frame = new JFrame JavaDoc();
121         frame.setContentPane(buildContent());
122         frame.setSize(400, 300);
123         frame.setTitle("JGoodies Animation :: Intro Example");
124         frame.setDefaultCloseOperation(3); // EXIT_ON_CLOSE
125
locateOnScreenCenter(frame);
126         frame.setVisible(true);
127     }
128     
129
130     /**
131      * Builds and answers the frame's content pane.
132      *
133      * @return the built content
134      */

135     private Container buildContent() {
136         JPanel JavaDoc panel = new JPanel JavaDoc(new GridLayout(4, 0));
137         panel.add(reportLabel);
138         panel.add(label1);
139         panel.add(label2);
140         panel.add(label3);
141         panel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
142         return panel;
143     }
144
145
146     // Animation Creation *****************************************************
147

148     /**
149      * Creates and answers a parallel animation that is composed of
150      * four animations: report, fade, scaling, and spacing.
151      *
152      * @return the composed animation
153      */

154     private Animation createComposedAnimation() {
155         List JavaDoc animations = new LinkedList JavaDoc();
156         
157         animations.add(Animations.repeat(5f, new SimpleReportAnimation(reportLabel, 2000)));
158         animations.add(BasicTextAnimation.defaultFade(label1,
159                                                       5000,
160                                                       "Default Fade",
161                                                       Color.darkGray));
162         animations.add(BasicTextAnimation.defaultScale(label2,
163                                                        4000,
164                                                        "Default Scale",
165                                                        Color.darkGray));
166         animations.add(BasicTextAnimation.defaultSpace(label3, 3000,
167                                                        "Default Space",
168                                                        Color.darkGray));
169         
170         int count = 1;
171         for (Iterator JavaDoc i = animations.iterator(); i.hasNext();) {
172             Animation element = (Animation) i.next();
173             element.addAnimationListener(new AnimationLogger("Animation " + count++));
174         }
175         
176         Animation composedAnimation = Animations.parallel(animations);
177         composedAnimation.addAnimationListener(new AnimationLogger("Composed animation"));
178         return composedAnimation;
179     }
180
181
182     // Helper Code ***********************************************************
183

184     /**
185      * Locates the given component on the screen's center.
186      *
187      * @param component the component to be located on the screen's center
188      */

189     private void locateOnScreenCenter(Component component) {
190         Dimension paneSize = component.getSize();
191         Dimension screenSize = component.getToolkit().getScreenSize();
192         component.setLocation(
193             (screenSize.width - paneSize.width) / 2,
194             (screenSize.height - paneSize.height) / 2);
195     }
196
197     private static class SimpleReportAnimation extends AbstractAnimation {
198     
199         private final JLabel JavaDoc target;
200         
201         private long frameNumber = 0;
202         private long startTime = -1;
203     
204         /**
205          * Constructs a simple report animation.
206          *
207          * @param target the JLabel used to display
208          * @param duration the animation's duration
209          */

210         private SimpleReportAnimation(JLabel JavaDoc target, long duration) {
211             super(duration);
212             this.target = target;
213         }
214     
215         /**
216          * Applies the animation effect: sets a text for the time, total time,
217          * iteration, frame number, and - after a second - the total frame rate.
218          *
219          * @param time the current time
220          */

221         protected void applyEffect(long time) {
222             frameNumber++;
223             long totalTime = totalTime();
224             float fps = totalTime < 1000 ? 0 : frameNumber * 1000 / totalTime;
225             int iteration = (int) (totalTime / duration());
226
227             target.setText(
228                 "time=" + time / 1000 + "s"
229                 + "; total=" + totalTime / 1000 + "s"
230                 + "; iteration=" + iteration
231                 + "; frame=" + frameNumber
232                 + "; fps= " + fps);
233         }
234         
235         private long totalTime() {
236             if (startTime == -1) {
237                 startTime = System.currentTimeMillis();
238             }
239             return System.currentTimeMillis() - startTime;
240         }
241         
242     }
243     
244     
245     // An AnimationListener that logs animation events to the console.
246
private static class AnimationLogger extends AnimationAdapter {
247         private final String JavaDoc name;
248         
249         AnimationLogger(String JavaDoc name) { this.name = name; }
250         
251         public void animationStarted(AnimationEvent e) {
252             System.out.println(name + " started.");
253         }
254         public void animationStopped(AnimationEvent e) {
255             System.out.println(name + " stopped.");
256         }
257     }
258     
259
260 }
Popular Tags