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.VerticalBarsPanel;
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 a VerticalBarsPanel with a BasicTextLabel.
054: *
055: * @author Karsten Lentzsch
056: * @version $Revision: 1.8 $
057: */
058:
059: public final class VerticalBarsIntro extends Model {
060:
061: private static final Color JAVAONE_BLUE = new Color(46, 49, 146);
062: // private static final String ZOINK_FILENAME =
063: // "com/jgoodies/animation/tutorial/sound/mp3/zoink.mp3";
064:
065: private static final String PROPERTYNAME_DURATION = "duration";
066: private static final String PROPERTYNAME_PAUSE = "pause";
067:
068: private VerticalBarsPanel verticalBarsPanel;
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 :: Vertical Bars Intro");
089: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
090: JComponent panel = new VerticalBarsIntro().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 VerticalBarsIntro() {
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 blue, bold Tahoma 20 pt.
136: textLabel = new BasicTextLabel(" ");
137: textLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
138: textLabel.setColor(JAVAONE_BLUE);
139:
140: verticalBarsPanel = new VerticalBarsPanel(textLabel);
141: verticalBarsPanel.setBackground(Color.WHITE);
142: verticalBarsPanel.setFraction(0.2);
143:
144: durationField = BasicComponentFactory
145: .createIntegerField(new PropertyAdapter(this ,
146: PROPERTYNAME_DURATION));
147: pauseField = BasicComponentFactory
148: .createIntegerField(new PropertyAdapter(this ,
149: PROPERTYNAME_PAUSE));
150: // Create an Action to animate the bars
151: animateAction = new AnimateAction();
152: }
153:
154: // Building *************************************************************
155:
156: /**
157: * Builds and returns a panel with the preview in the top
158: * and the tool panel in the bottom.
159: *
160: * @return the built panel
161: */
162: private JComponent buildPanel() {
163: FormLayout layout = new FormLayout("fill:pref:grow",
164: "fill:pref:grow, p, p");
165:
166: PanelBuilder builder = new PanelBuilder(layout);
167: CellConstraints cc = new CellConstraints();
168: builder.add(buildPreviewPanel(), cc.xy(1, 1));
169: builder.addSeparator("", cc.xy(1, 2));
170: builder.add(buildToolsPanel(), cc.xy(1, 3));
171: return builder.getPanel();
172: }
173:
174: public JComponent buildPreviewPanel() {
175: FormLayout layout = new FormLayout("fill:275dlu:grow",
176: "50dlu:grow, fill:50dlu, 50dlu:grow");
177: JPanel panel = new JPanel(layout);
178: panel.setBackground(Color.WHITE);
179: panel.add(verticalBarsPanel, new CellConstraints(1, 2));
180: return panel;
181: }
182:
183: public JComponent buildToolsPanel() {
184: FormLayout layout = new FormLayout(
185: "pref, 3dlu, 25dlu, 12dlu, pref, 3dlu, 25dlu, 12dlu:grow, 50dlu",
186: "pref");
187:
188: PanelBuilder builder = new PanelBuilder(layout);
189: builder.setDefaultDialogBorder();
190: CellConstraints cc = new CellConstraints();
191: builder.addLabel("Duration", cc.xy(1, 1));
192: builder.add(durationField, cc.xy(3, 1));
193: builder.addLabel("Pause", cc.xy(5, 1));
194: builder.add(pauseField, cc.xy(7, 1));
195: builder.add(new JButton(animateAction), cc.xy(9, 1));
196: return builder.getPanel();
197: }
198:
199: // Animation **************************************************************
200:
201: private Animation createAnimation() {
202: // Animation soundAnimation = createSoundAnimation();
203:
204: String[] texts = new String[] { "With Swing", "And Java2D",
205: "YOU can build", "Cool Java Apps", "Today!" };
206: textLabel.setText(texts[0]);
207: List sequence = new LinkedList();
208: sequence.add(Animations.pause(pause));
209: for (int i = 1; i < texts.length; i++) {
210: Animation moveBarsAnimation = new MoveBarsAnimation(
211: texts[i - 1], texts[i], duration);
212: sequence.add(moveBarsAnimation);
213: // sequence.add(soundAnimation == null
214: // ? moveBarsAnimation
215: // : Animations.parallel(soundAnimation, moveBarsAnimation));
216: sequence.add(Animations.pause(pause));
217: }
218: return Animations.sequential(sequence);
219: }
220:
221: // private Animation createSoundAnimation() {
222: // try {
223: // return new SoundAnimation(ZOINK_FILENAME, duration);
224: // } catch (Exception e) {
225: // System.out.println("Failed to create the sound animation.");
226: // return null;
227: // }
228: // }
229:
230: private final class MoveBarsAnimation extends AbstractAnimation {
231:
232: private final AnimationFunction fractionFunction;
233: private final AnimationFunction colorFunction;
234: private final AnimationFunction stringFunction;
235:
236: private MoveBarsAnimation(String text1, String text2,
237: long duration) {
238: super (duration, true);
239: fractionFunction = createFractionFunction(duration);
240: colorFunction = AnimationFunctions.alphaColor(
241: createAlphaFunction(duration), JAVAONE_BLUE);
242: stringFunction = createStringFunction(text1, text2,
243: duration);
244: }
245:
246: private AnimationFunction createFractionFunction(long duration) {
247: return AnimationFunctions.linear(duration,
248: new Float[] { new Float(0.4f), new Float(0.75d),
249: new Float(0.4f) }, new float[] { 0.0f,
250: 0.5f, 1.0f });
251: }
252:
253: private AnimationFunction createAlphaFunction(long duration) {
254: return AnimationFunctions.linear(duration,
255: new Integer[] { new Integer(255), new Integer(0),
256: new Integer(255) }, new float[] { 0.0f,
257: 0.5f, 1.0f });
258: }
259:
260: private AnimationFunction createStringFunction(String text1,
261: String text2, long duration) {
262: return AnimationFunctions.discrete(duration, new String[] {
263: text1, text2 });
264: }
265:
266: public void applyEffect(long time) {
267: Float fraction = (Float) fractionFunction.valueAt(time);
268: verticalBarsPanel.setFraction(fraction.doubleValue());
269: textLabel.setColor((Color) colorFunction.valueAt(time));
270: textLabel.setText((String) stringFunction.valueAt(time));
271: }
272:
273: }
274:
275: // Animation Action *******************************************************
276:
277: private final class AnimateAction extends AbstractAction {
278:
279: private AnimateAction() {
280: super ("Animate");
281: }
282:
283: public void actionPerformed(ActionEvent e) {
284: Animation animation = createAnimation();
285: int fps = 30;
286: animation.addAnimationListener(new StartStopHandler());
287: new Animator(animation, fps).start();
288: }
289:
290: }
291:
292: /**
293: * Disables the animate action at animation start and
294: * enables it when the animation stopped.
295: */
296: private final class StartStopHandler extends AnimationAdapter {
297:
298: public void animationStarted(AnimationEvent e) {
299: animateAction.setEnabled(false);
300: }
301:
302: public void animationStopped(AnimationEvent e) {
303: animateAction.setEnabled(true);
304: }
305: }
306:
307: }
|