PageRenderTime 112ms CodeModel.GetById 93ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/ui/misc/FadeOnChangeLabel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 286 lines | 132 code | 35 blank | 119 comment | 26 complexity | 1b0239ee242576b2bb347f56cebe4175 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.ui.misc;
 18
 19import java.awt.AlphaComposite;
 20import java.awt.Color;
 21import java.awt.Container;
 22import java.awt.Graphics;
 23
 24import java.awt.Graphics2D;
 25import java.awt.Insets;
 26
 27
 28
 29import javax.swing.JLabel;
 30import mpv5.utils.ui.PanelUtils;
 31
 32/**
 33 * <p>Title: FadeOnChangeLabel </p>
 34 *
 35 * <p>Description: A label that animates when it's text value change.</p>
 36 *
 37 * <p>Copyright: Copyright (c) 2006 - 2007</p>
 38 *
 39 * @author Chris Fragoulides
 40 * @version 1.0
 41 */
 42public class FadeOnChangeLabel
 43        extends JLabel implements Runnable {
 44
 45    private static final long serialVersionUID = 1L;
 46    /**
 47     * The last value of the label.
 48     */
 49    private String lastValue = "";
 50    /**
 51     * True when an animation is in progress.
 52     */
 53    private boolean animating = false;
 54    /**
 55     * Fade value.
 56     */
 57    private static float fade = 0;
 58    /**
 59     * Initial fade value.
 60     */
 61    private float initFade = .2f;
 62    /**
 63     * This variable controls how match we
 64     * will fade in each animation loop.
 65     */
 66    private float fadeStep = -0.1f;
 67    /**
 68     * Red component of the fade color.
 69     */
 70    private static float red = 0.4f;
 71    /**
 72     * Green component of the fade color.
 73     */
 74    private static float green = 0.8f;
 75    /**
 76     * Blue component of the fade color.
 77     */
 78    private static float blue = 0f;
 79    /**
 80     * When a fade is initiated, the label will have this color.
 81     */
 82    private static Color fadeColor = new Color(red, green, blue, fade);
 83    /**
 84     * Flag to control the fade process.
 85     */
 86    private boolean paintCalled = false;
 87    /**
 88     * If this label doesn't control it's painting, we must set
 89     * the Container responsible for it.
 90     */
 91    private Container repaintCont = null;
 92    private volatile float alpha = 1.0f;
 93
 94    /**
 95     * Constructor. It contains only a call to setOpaque(),
 96     * because we want the color changes to be visible.
 97     */
 98    public FadeOnChangeLabel() {
 99        setOpaque(true);
100    }
101
102
103//
104//    class Fader implements Runnable {
105//
106//        public void run() {
107//            while (!Thread.interrupted() && alpha > 0.0f) {
108//                repaint();
109//                alpha = Math.max(0.0f, alpha - 0.01f);
110//                try {
111//                    Thread.sleep(100);
112//                } catch (InterruptedException ignored) {
113//                }
114//            }
115//        }
116//    }
117    /**
118     * Sets animation parameters. Will only accept values in the
119     * range [0,1] for color components and initial fade,
120     * and in (-1,0) for fade step.
121     * @param r - float value of the red color component.
122     * @param g - float value of the green color component.
123     * @param b - float value of the blue color component.
124     * @param fStep     - float value of the fade step.
125     * @param iFade     - float value of the initial fade.
126     */
127    public void setParams(float r, float g, float b,
128            float fStep, float iFade) {
129        if (r >= 0 && r <= 1) {
130            red = r;
131        }
132
133        if (g >= 0 && g <= 1) {
134            green = g;
135        }
136
137        if (b >= 0 && b <= 1) {
138            blue = b;
139        }
140
141        if (fStep > 0 && fStep < 1) {
142            fadeStep = fStep;
143        }
144
145        if (iFade >= 0 && iFade <= 1) {
146            initFade = iFade;
147        }
148
149    }
150
151    /**
152     * Sets the Container that controls this label's painting.
153     * Call this method if the label is a child component of a container
154     * that does not control the painting process.
155     * @param c - Container.
156     */
157    public void setRepaintContainer(Container c) {
158        repaintCont = c;
159    }
160
161    /**
162     * Sets the text, and initiates the animation if the
163     * new value is different that the old one.
164     * @param text
165     */
166    @Override
167    public void setText(String text) {
168        super.setText(text);
169        animate();
170
171    }
172
173    /**
174     * Override paintComponent to make it fulfil our needs.
175     */
176    @Override
177    public void paintComponent(Graphics g) {
178        // Let the Label perform its normal painting.
179
180        Graphics2D g2 = (Graphics2D) g;
181        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
182        super.paintComponent(g);
183        // Now make the fade effect.
184        if (fade != 0) {
185            Insets i = this.getInsets();
186            g.setColor(fadeColor);
187            g.fillRect(i.left, i.top,
188                    getWidth() - i.left - i.right,
189                    getHeight() - i.top - i.bottom);
190        }
191// paintComponent() called, we can continue to the next
192// animation frame.
193
194        paintCalled = true;
195    }
196
197    public void setFadeColor(Color color) {
198        float[] f = color.getComponents(null);
199        red =
200                f[0];
201        green =
202                f[1];
203        blue =
204                f[2];
205        fadeColor =
206                new Color(red, green, blue, fade);
207//        fadeColor = new Color(fadeColor.getColorSpace(), f, fade);
208    }
209
210    public void reset() {
211        red = 0.4f;
212        green =
213                0.8f;
214        blue =
215                0f;
216        fadeColor =
217                new Color(red, green, blue, fade);
218    }
219
220    /**
221     * Initiates the animation Thread.
222     */
223    private void animate() {
224        // Only animate if there's not another animation in progress
225        // and this Label is actually added to a parent Container, i.e.
226        // if it is visible.
227        if (!animating && this.getParent() != null) {
228            Thread t = new Thread(this);
229            t.start();
230        }
231
232    }
233
234    /**
235     * The core of the animation.
236     */
237    @Override
238    public void run() {
239        animating = true;
240        fade = initFade;
241        alpha = 1.0f;
242
243        try {
244            while (fade != 0) {
245//            	while (!Thread.interrupted() && alpha > 0.0f) {
246                fadeColor = new Color(red, green, blue, fade);
247                fade += fadeStep;
248                if (fade < 0) {
249                    fade = 0;
250                }
251
252                paintCalled = false;
253                if (repaintCont == null) {
254                    repaint(); // This label controls it's painting.
255                } // This label controls it's painting.
256                else {
257                    repaintCont.repaint(); // Ask the container to repaint.
258                } // Ask the container to repaint.
259// Now wait until paintComponent() gets called.
260
261
262                while (!paintCalled && fade != 0) {
263                    Thread.sleep(512);
264                }
265
266            }
267            Thread.sleep(1111);
268            while (!Thread.interrupted() && alpha > 0f) {
269
270                alpha = Math.max(0f, alpha - 0.02f);
271                repaint();
272                try {
273                    Thread.sleep(33);
274                } catch (InterruptedException ignored) {
275                }
276            }
277            animating = false;
278        } catch (Exception e) {
279            animating = false;
280            System.out.println("FadeOnChangeLabel encountered an error: " + e.getMessage());
281        }
282    }
283}
284
285
286