PageRenderTime 41ms CodeModel.GetById 23ms 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
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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. */
  17. package mpv5.ui.misc;
  18. import java.awt.AlphaComposite;
  19. import java.awt.Color;
  20. import java.awt.Container;
  21. import java.awt.Graphics;
  22. import java.awt.Graphics2D;
  23. import java.awt.Insets;
  24. import javax.swing.JLabel;
  25. import mpv5.utils.ui.PanelUtils;
  26. /**
  27. * <p>Title: FadeOnChangeLabel </p>
  28. *
  29. * <p>Description: A label that animates when it's text value change.</p>
  30. *
  31. * <p>Copyright: Copyright (c) 2006 - 2007</p>
  32. *
  33. * @author Chris Fragoulides
  34. * @version 1.0
  35. */
  36. public class FadeOnChangeLabel
  37. extends JLabel implements Runnable {
  38. private static final long serialVersionUID = 1L;
  39. /**
  40. * The last value of the label.
  41. */
  42. private String lastValue = "";
  43. /**
  44. * True when an animation is in progress.
  45. */
  46. private boolean animating = false;
  47. /**
  48. * Fade value.
  49. */
  50. private static float fade = 0;
  51. /**
  52. * Initial fade value.
  53. */
  54. private float initFade = .2f;
  55. /**
  56. * This variable controls how match we
  57. * will fade in each animation loop.
  58. */
  59. private float fadeStep = -0.1f;
  60. /**
  61. * Red component of the fade color.
  62. */
  63. private static float red = 0.4f;
  64. /**
  65. * Green component of the fade color.
  66. */
  67. private static float green = 0.8f;
  68. /**
  69. * Blue component of the fade color.
  70. */
  71. private static float blue = 0f;
  72. /**
  73. * When a fade is initiated, the label will have this color.
  74. */
  75. private static Color fadeColor = new Color(red, green, blue, fade);
  76. /**
  77. * Flag to control the fade process.
  78. */
  79. private boolean paintCalled = false;
  80. /**
  81. * If this label doesn't control it's painting, we must set
  82. * the Container responsible for it.
  83. */
  84. private Container repaintCont = null;
  85. private volatile float alpha = 1.0f;
  86. /**
  87. * Constructor. It contains only a call to setOpaque(),
  88. * because we want the color changes to be visible.
  89. */
  90. public FadeOnChangeLabel() {
  91. setOpaque(true);
  92. }
  93. //
  94. // class Fader implements Runnable {
  95. //
  96. // public void run() {
  97. // while (!Thread.interrupted() && alpha > 0.0f) {
  98. // repaint();
  99. // alpha = Math.max(0.0f, alpha - 0.01f);
  100. // try {
  101. // Thread.sleep(100);
  102. // } catch (InterruptedException ignored) {
  103. // }
  104. // }
  105. // }
  106. // }
  107. /**
  108. * Sets animation parameters. Will only accept values in the
  109. * range [0,1] for color components and initial fade,
  110. * and in (-1,0) for fade step.
  111. * @param r - float value of the red color component.
  112. * @param g - float value of the green color component.
  113. * @param b - float value of the blue color component.
  114. * @param fStep - float value of the fade step.
  115. * @param iFade - float value of the initial fade.
  116. */
  117. public void setParams(float r, float g, float b,
  118. float fStep, float iFade) {
  119. if (r >= 0 && r <= 1) {
  120. red = r;
  121. }
  122. if (g >= 0 && g <= 1) {
  123. green = g;
  124. }
  125. if (b >= 0 && b <= 1) {
  126. blue = b;
  127. }
  128. if (fStep > 0 && fStep < 1) {
  129. fadeStep = fStep;
  130. }
  131. if (iFade >= 0 && iFade <= 1) {
  132. initFade = iFade;
  133. }
  134. }
  135. /**
  136. * Sets the Container that controls this label's painting.
  137. * Call this method if the label is a child component of a container
  138. * that does not control the painting process.
  139. * @param c - Container.
  140. */
  141. public void setRepaintContainer(Container c) {
  142. repaintCont = c;
  143. }
  144. /**
  145. * Sets the text, and initiates the animation if the
  146. * new value is different that the old one.
  147. * @param text
  148. */
  149. @Override
  150. public void setText(String text) {
  151. super.setText(text);
  152. animate();
  153. }
  154. /**
  155. * Override paintComponent to make it fulfil our needs.
  156. */
  157. @Override
  158. public void paintComponent(Graphics g) {
  159. // Let the Label perform its normal painting.
  160. Graphics2D g2 = (Graphics2D) g;
  161. g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
  162. super.paintComponent(g);
  163. // Now make the fade effect.
  164. if (fade != 0) {
  165. Insets i = this.getInsets();
  166. g.setColor(fadeColor);
  167. g.fillRect(i.left, i.top,
  168. getWidth() - i.left - i.right,
  169. getHeight() - i.top - i.bottom);
  170. }
  171. // paintComponent() called, we can continue to the next
  172. // animation frame.
  173. paintCalled = true;
  174. }
  175. public void setFadeColor(Color color) {
  176. float[] f = color.getComponents(null);
  177. red =
  178. f[0];
  179. green =
  180. f[1];
  181. blue =
  182. f[2];
  183. fadeColor =
  184. new Color(red, green, blue, fade);
  185. // fadeColor = new Color(fadeColor.getColorSpace(), f, fade);
  186. }
  187. public void reset() {
  188. red = 0.4f;
  189. green =
  190. 0.8f;
  191. blue =
  192. 0f;
  193. fadeColor =
  194. new Color(red, green, blue, fade);
  195. }
  196. /**
  197. * Initiates the animation Thread.
  198. */
  199. private void animate() {
  200. // Only animate if there's not another animation in progress
  201. // and this Label is actually added to a parent Container, i.e.
  202. // if it is visible.
  203. if (!animating && this.getParent() != null) {
  204. Thread t = new Thread(this);
  205. t.start();
  206. }
  207. }
  208. /**
  209. * The core of the animation.
  210. */
  211. @Override
  212. public void run() {
  213. animating = true;
  214. fade = initFade;
  215. alpha = 1.0f;
  216. try {
  217. while (fade != 0) {
  218. // while (!Thread.interrupted() && alpha > 0.0f) {
  219. fadeColor = new Color(red, green, blue, fade);
  220. fade += fadeStep;
  221. if (fade < 0) {
  222. fade = 0;
  223. }
  224. paintCalled = false;
  225. if (repaintCont == null) {
  226. repaint(); // This label controls it's painting.
  227. } // This label controls it's painting.
  228. else {
  229. repaintCont.repaint(); // Ask the container to repaint.
  230. } // Ask the container to repaint.
  231. // Now wait until paintComponent() gets called.
  232. while (!paintCalled && fade != 0) {
  233. Thread.sleep(512);
  234. }
  235. }
  236. Thread.sleep(1111);
  237. while (!Thread.interrupted() && alpha > 0f) {
  238. alpha = Math.max(0f, alpha - 0.02f);
  239. repaint();
  240. try {
  241. Thread.sleep(33);
  242. } catch (InterruptedException ignored) {
  243. }
  244. }
  245. animating = false;
  246. } catch (Exception e) {
  247. animating = false;
  248. System.out.println("FadeOnChangeLabel encountered an error: " + e.getMessage());
  249. }
  250. }
  251. }