001: /*
002: * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.animation.tutorial.intro;
032:
033: import java.awt.Color;
034: import java.awt.Font;
035: import java.awt.event.ActionEvent;
036: import java.util.LinkedList;
037: import java.util.List;
038:
039: import javax.swing.*;
040:
041: import com.jgoodies.animation.*;
042: import com.jgoodies.animation.components.BasicTextLabel;
043: import com.jgoodies.animation.tutorial.TutorialUtils;
044: import com.jgoodies.animation.tutorial.panel.HorizontalBarsPanel;
045: import com.jgoodies.binding.adapter.BasicComponentFactory;
046: import com.jgoodies.binding.beans.Model;
047: import com.jgoodies.binding.beans.PropertyAdapter;
048: import com.jgoodies.forms.builder.PanelBuilder;
049: import com.jgoodies.forms.layout.CellConstraints;
050: import com.jgoodies.forms.layout.FormLayout;
051:
052: /**
053: * An intro that combines the HorizontalBarsPanel and a BasicTextLabel.
054: *
055: * @author Karsten Lentzsch
056: * @version $Revision: 1.8 $
057: */
058:
059: public final class HorizontalBarsIntro extends Model {
060:
061: private static final Color JAVAONE_RED = new Color(209, 33, 36);
062: // private static final String FLOMP_FILENAME =
063: // "com/jgoodies/animation/tutorial/sound/wav/flomp.wav";
064:
065: private static final String PROPERTYNAME_DURATION = "duration";
066: private static final String PROPERTYNAME_PAUSE = "pause";
067:
068: private HorizontalBarsPanel horizontalBarsPanel;
069: private BasicTextLabel textLabel;
070:
071: private int duration;
072: private long pause;
073:
074: private JComponent durationField;
075: private JComponent pauseField;
076: private Action animateAction;
077:
078: // Self Starter ***********************************************************
079:
080: public static void main(String[] args) {
081: try {
082: UIManager
083: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
084: } catch (Exception e) {
085: // Likely PlasticXP is not in the class path; ignore.
086: }
087: JFrame frame = new JFrame();
088: frame.setTitle("Animation Tutorial :: Horizontal Bars Intro");
089: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
090: JComponent panel = new HorizontalBarsIntro().buildPanel();
091: frame.getContentPane().add(panel);
092: frame.pack();
093: TutorialUtils.locateOnOpticalScreenCenter(frame);
094: frame.setVisible(true);
095: }
096:
097: // Instance Creation ******************************************************
098:
099: public HorizontalBarsIntro() {
100: duration = 350;
101: pause = 2000;
102: initComponents();
103: }
104:
105: // Bound Properties *******************************************************
106:
107: public int getDuration() {
108: return duration;
109: }
110:
111: public void setDuration(int newDuration) {
112: int oldDuration = getDuration();
113: duration = newDuration;
114: firePropertyChange(PROPERTYNAME_DURATION, oldDuration,
115: newDuration);
116: }
117:
118: public long getPause() {
119: return pause;
120: }
121:
122: public void setPause(long newPause) {
123: long oldPause = getPause();
124: pause = newPause;
125: firePropertyChange(PROPERTYNAME_PAUSE, oldPause, newPause);
126: }
127:
128: // Component Creation and Initialization **********************************
129:
130: /**
131: * Creates the text label, sliders and animation Actions.
132: * Binds sliders to bound properties of the text label.
133: */
134: private void initComponents() {
135: // Setup a BasicTextLabel with red, bold Tahoma 24 pt.
136: textLabel = new BasicTextLabel(" ");
137: textLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
138: textLabel.setColor(JAVAONE_RED);
139:
140: horizontalBarsPanel = new HorizontalBarsPanel(textLabel);
141: horizontalBarsPanel.setBackground(Color.WHITE);
142:
143: durationField = BasicComponentFactory
144: .createIntegerField(new PropertyAdapter(this ,
145: PROPERTYNAME_DURATION));
146: pauseField = BasicComponentFactory
147: .createIntegerField(new PropertyAdapter(this ,
148: PROPERTYNAME_PAUSE));
149: // Create an Action to animate the bars
150: animateAction = new AnimateAction();
151: }
152:
153: // Building *************************************************************
154:
155: /**
156: * Builds and returns a panel with the preview in the top
157: * and the tool panel in the bottom.
158: *
159: * @return the built panel
160: */
161: private JComponent buildPanel() {
162: FormLayout layout = new FormLayout("fill:pref:grow",
163: "fill:pref:grow, p, p");
164:
165: PanelBuilder builder = new PanelBuilder(layout);
166: CellConstraints cc = new CellConstraints();
167: builder.add(buildPreviewPanel(), cc.xy(1, 1));
168: builder.addSeparator("", cc.xy(1, 2));
169: builder.add(buildToolsPanel(), cc.xy(1, 3));
170: return builder.getPanel();
171: }
172:
173: public JComponent buildPreviewPanel() {
174: FormLayout layout = new FormLayout("fill:250dlu:grow",
175: "fill:150dlu:grow");
176: JPanel panel = new JPanel(layout);
177: panel.setBackground(Color.WHITE);
178: panel.add(horizontalBarsPanel, new CellConstraints());
179: return panel;
180: }
181:
182: public JComponent buildToolsPanel() {
183: FormLayout layout = new FormLayout(
184: "pref, 3dlu, 25dlu, 12dlu, pref, 3dlu, 25dlu, 12dlu:grow, 50dlu",
185: "pref");
186:
187: PanelBuilder builder = new PanelBuilder(layout);
188: builder.setDefaultDialogBorder();
189: CellConstraints cc = new CellConstraints();
190: builder.addLabel("Duration", cc.xy(1, 1));
191: builder.add(durationField, cc.xy(3, 1));
192: builder.addLabel("Pause", cc.xy(5, 1));
193: builder.add(pauseField, cc.xy(7, 1));
194: builder.add(new JButton(animateAction), cc.xy(9, 1));
195: return builder.getPanel();
196: }
197:
198: // Animation **************************************************************
199:
200: private Animation createAnimation() {
201: // Animation soundAnimation = createSoundAnimation();
202:
203: String WAIT = "wait";
204: String[] texts = new String[] { "Welcome", WAIT,
205: "To the JavaOne 2004!", WAIT, "Java on the Desktop",
206: "Is Back!", WAIT, "This Demo Uses",
207: "The JGoodies Animation", "A Framework",
208: "For Time-Based", "Real-Time Animations", WAIT };
209: List sequence = new LinkedList();
210: sequence.add(Animations.pause(pause));
211: int offset = -1;
212: for (int i = 1; i < texts.length; i++) {
213: if ((texts[i].equals(WAIT))) {
214: offset = -2;
215: } else {
216: Animation moveBarsAnimation = new MoveBarsAnimation(
217: texts[i + offset], texts[i], duration);
218: sequence.add(moveBarsAnimation);
219: // sequence.add(soundAnimation == null
220: // ? moveBarsAnimation
221: // : Animations.parallel(soundAnimation, moveBarsAnimation));
222: offset = -1;
223: }
224: sequence.add(Animations.pause(pause));
225: }
226: textLabel.setText(texts[0]);
227: return Animations.sequential(sequence);
228: }
229:
230: // private Animation createSoundAnimation() {
231: // try {
232: // ClassLoader loader = getClass().getClassLoader();
233: // URL url = loader.getResource(FLOMP_FILENAME);
234: // Clip clip = MP3SoundUtils.getOpenClip(url);
235: // return new SoundAnimation(clip, duration);
236: // } catch (Exception e) {
237: // System.out.println("Failed to create the sound animation.");
238: // return null;
239: // }
240: // }
241:
242: private final class MoveBarsAnimation extends AbstractAnimation {
243:
244: private final AnimationFunction fractionFunction;
245: private final AnimationFunction colorFunction;
246: private final AnimationFunction stringFunction;
247:
248: private MoveBarsAnimation(String text1, String text2,
249: long duration) {
250: super (duration, true);
251: fractionFunction = createFractionFunction(duration);
252: colorFunction = AnimationFunctions.alphaColor(
253: createAlphaFunction(duration), JAVAONE_RED);
254: stringFunction = createStringFunction(text1, text2,
255: duration);
256: }
257:
258: private AnimationFunction createFractionFunction(long duration) {
259: return AnimationFunctions.linear(duration,
260: new Float[] { new Float(0.45f), new Float(1.0d),
261: new Float(0.45f) }, new float[] { 0.0f,
262: 0.5f, 1.0f });
263: }
264:
265: private AnimationFunction createAlphaFunction(long duration) {
266: return AnimationFunctions.linear(duration,
267: new Integer[] { new Integer(255), new Integer(0),
268: new Integer(255) }, new float[] { 0.0f,
269: 0.5f, 1.0f });
270: }
271:
272: private AnimationFunction createStringFunction(String text1,
273: String text2, long duration) {
274: return AnimationFunctions.discrete(duration, new String[] {
275: text1, text2 });
276: }
277:
278: public void applyEffect(long time) {
279: Float fraction = (Float) fractionFunction.valueAt(time);
280: horizontalBarsPanel.setFraction(fraction.doubleValue());
281: textLabel.setColor((Color) colorFunction.valueAt(time));
282: textLabel.setText((String) stringFunction.valueAt(time));
283: }
284:
285: }
286:
287: // Animation Action *******************************************************
288:
289: private final class AnimateAction extends AbstractAction {
290:
291: private AnimateAction() {
292: super ("Animate");
293: }
294:
295: public void actionPerformed(ActionEvent e) {
296: Animation animation = createAnimation();
297: int fps = 30;
298: animation.addAnimationListener(new StartStopHandler());
299: new Animator(animation, fps).start();
300: }
301:
302: }
303:
304: /**
305: * Disables the animate action at animation start and
306: * enables it when the animation stopped.
307: */
308: private final class StartStopHandler extends AnimationAdapter {
309:
310: public void animationStarted(AnimationEvent e) {
311: animateAction.setEnabled(false);
312: }
313:
314: public void animationStopped(AnimationEvent e) {
315: animateAction.setEnabled(true);
316: }
317: }
318:
319: }
|