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.component;
032:
033: import java.awt.Color;
034: import java.awt.event.ActionEvent;
035: import java.beans.PropertyChangeEvent;
036: import java.beans.PropertyChangeListener;
037: import java.net.URL;
038:
039: import javax.swing.*;
040:
041: import com.jgoodies.animation.*;
042: import com.jgoodies.animation.tutorial.TutorialUtils;
043: import com.jgoodies.binding.adapter.BoundedRangeAdapter;
044: import com.jgoodies.binding.beans.Model;
045: import com.jgoodies.binding.beans.PropertyAdapter;
046: import com.jgoodies.binding.value.ConverterFactory;
047: import com.jgoodies.binding.value.ValueModel;
048: import com.jgoodies.forms.builder.PanelBuilder;
049: import com.jgoodies.forms.layout.CellConstraints;
050: import com.jgoodies.forms.layout.FormLayout;
051:
052: /**
053: * Demonstrates the BlurredIcon.
054: *
055: * @author Karsten Lentzsch
056: * @version $Revision: 1.6 $
057: */
058: public final class BlurredIconExample extends Model {
059:
060: private static final String ICON_FILENAME = "com/jgoodies/animation/tutorial/component/JavaOne.jpg";
061:
062: private static final long DURATION = 2000;
063:
064: private BlurredIcon blurredIcon;
065: private JLabel iconLabel;
066: private JSlider timeSlider;
067: private Action animateAction;
068:
069: // Self Starter ***********************************************************
070:
071: public static void main(String[] args) {
072: try {
073: UIManager
074: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
075: } catch (Exception e) {
076: // Likely PlasticXP is not in the class path; ignore.
077: }
078: JFrame frame = new JFrame();
079: frame.setTitle("Animation Tutorial :: BlurredIcon");
080: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
081: JComponent panel = new BlurredIconExample().buildPanel();
082: frame.getContentPane().add(panel);
083: frame.pack();
084: TutorialUtils.locateOnOpticalScreenCenter(frame);
085: frame.setVisible(true);
086: }
087:
088: // Instance Creation ******************************************************
089:
090: public BlurredIconExample() {
091: initComponents();
092: }
093:
094: // Component Creation and Initialization **********************************
095:
096: /**
097: * Creates the text label, sliders and animation Actions.
098: * Binds sliders to bound properties of the text label.
099: */
100: private void initComponents() {
101: // Load the JavaOne logo.
102: URL url = getClass().getClassLoader()
103: .getResource(ICON_FILENAME);
104: ImageIcon javaOneLogo = new ImageIcon(url);
105: // Setup a BlurredIcon with a JavaOne logo.
106: blurredIcon = new BlurredIcon(javaOneLogo, DURATION, 100);
107:
108: // Have a label that uses the blurred Icon
109: iconLabel = new JLabel(blurredIcon);
110:
111: // Create a slider for the label's time in interval [0, 5000]
112: timeSlider = new JSlider();
113: ValueModel timeAdapter = new PropertyAdapter(blurredIcon,
114: "time", true);
115: timeSlider.setModel(new BoundedRangeAdapter(ConverterFactory
116: .createLongToIntegerConverter(timeAdapter), 0, 0,
117: (int) DURATION));
118: timeAdapter
119: .addValueChangeListener(new PropertyChangeListener() {
120:
121: public void propertyChange(PropertyChangeEvent evt) {
122: iconLabel.repaint();
123: }
124: });
125:
126: // Create an Action to animate the blur effect
127: animateAction = new AnimateAction();
128: }
129:
130: // Building *************************************************************
131:
132: /**
133: * Builds and returns a panel with the preview in the top
134: * and the tool panel in the bottom.
135: *
136: * @return the built panel
137: */
138: private JComponent buildPanel() {
139: FormLayout layout = new FormLayout("fill:pref:grow",
140: "fill:pref:grow, p, p");
141:
142: PanelBuilder builder = new PanelBuilder(layout);
143: CellConstraints cc = new CellConstraints();
144: builder.add(buildPreviewPanel(), cc.xy(1, 1));
145: builder.addSeparator("", cc.xy(1, 2));
146: builder.add(buildToolsPanel(), cc.xy(1, 3));
147: return builder.getPanel();
148: }
149:
150: public JComponent buildPreviewPanel() {
151: FormLayout layout = new FormLayout("fill:200dlu:grow",
152: "fill:150dlu:grow");
153: JPanel panel = new JPanel(layout);
154: panel.setBackground(Color.WHITE);
155: panel.add(iconLabel, new CellConstraints());
156: return panel;
157: }
158:
159: public JComponent buildToolsPanel() {
160: FormLayout layout = new FormLayout(
161: "pref, 3dlu, 50dlu, 12dlu, 50dlu", "pref");
162:
163: PanelBuilder builder = new PanelBuilder(layout);
164: builder.setDefaultDialogBorder();
165: CellConstraints cc = new CellConstraints();
166: builder.addLabel("Time", cc.xy(1, 1));
167: builder.add(timeSlider, cc.xy(3, 1));
168: builder.add(new JButton(animateAction), cc.xy(5, 1));
169: return builder.getPanel();
170: }
171:
172: // Animation **************************************************************
173:
174: private Animation createAnimation() {
175: Animation blurAnimation = new BlurAnimation(DURATION / 2);
176: return Animations.sequential(Animations.reverse(blurAnimation),
177: blurAnimation);
178: }
179:
180: private final class BlurAnimation extends AbstractAnimation {
181:
182: private BlurAnimation(long duration) {
183: super (duration, true);
184: }
185:
186: public void applyEffect(long time) {
187: blurredIcon.setTime(time * 2);
188: iconLabel.repaint();
189: }
190:
191: }
192:
193: // Animation Action *******************************************************
194:
195: private final class AnimateAction extends AbstractAction {
196:
197: private AnimateAction() {
198: super ("Animate");
199: }
200:
201: public void actionPerformed(ActionEvent e) {
202: Animation animation = createAnimation();
203: int fps = 30;
204: animation.addAnimationListener(new StartStopHandler());
205: new Animator(animation, fps).start();
206: }
207:
208: }
209:
210: /**
211: * Disables the animate action at animation start and
212: * enables it when the animation stopped.
213: */
214: private final class StartStopHandler extends AnimationAdapter {
215:
216: public void animationStarted(AnimationEvent e) {
217: animateAction.setEnabled(false);
218: }
219:
220: public void animationStopped(AnimationEvent e) {
221: animateAction.setEnabled(true);
222: }
223: }
224:
225: }
|