1. /**
  2. * <p>Copyright: Copyright (c) 2002-2004</p>
  3. * <p>Company: JavaResearch(http://www.javaresearch.org)</p>
  4. * <p>最后更新日期:2003年4月30日
  5. * @author cherami
  6. */
  7. package org.jr.swing;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. /**
  12. * 闪烁标签类。
  13. * 以指定的颜色和时间间隔闪烁显示文字。
  14. * @since 0.6
  15. */
  16. public class BlinkLabel
  17. extends JLabel {
  18. String text; //显示字符串
  19. Color colors[]; //变化颜色数组
  20. int interval; //颜色变化间隔
  21. int length; //颜色值总长
  22. int count = 0; //当前颜色值计数
  23. Timer timer;//定时器
  24. static final int INTERVAL = 1000;
  25. static final String TEXT="变色文字";
  26. /**
  27. * 以文字“变色文字”为显示值,以1000毫秒为间隔,颜色渐变模式构造一个BlinkLabel。
  28. */
  29. public BlinkLabel() {
  30. this(TEXT, INTERVAL, 0xff0000, 0xff);
  31. }
  32. /**
  33. * 以指定文字,以1000毫秒为间隔,颜色渐变模式构造一个BlinkLabel。
  34. * @param text 显示字符串
  35. */
  36. public BlinkLabel(String text) {
  37. this(text, INTERVAL, 0xff0000, 0xff);
  38. }
  39. /**
  40. * 以指定文字和颜色、以1000毫秒为间隔构造一个BlinkLabel。
  41. * @param text 显示字符串
  42. * @param colors 颜色数组
  43. */
  44. public BlinkLabel(String text, Color colors[]) {
  45. this(text, colors, INTERVAL);
  46. }
  47. /**
  48. * 以指定文字和时间间隔,颜色渐变模式构造一个BlinkLabel。
  49. * @param text 显示字符串
  50. * @param interval 时间间隔
  51. */
  52. public BlinkLabel(String text, int interval) {
  53. this(text, interval, 0xff0000, 0xff);
  54. }
  55. /**
  56. * 以指定的文字、时间间隔以及颜色构造一个BlinkLabel。
  57. * @param text 显示字符串
  58. * @param colors 颜色数组
  59. * @param interval 时间间隔
  60. */
  61. public BlinkLabel(String text, Color colors[], int interval) {
  62. if (text != null) {
  63. this.text = text;
  64. }
  65. else {
  66. this.text = TEXT;
  67. }
  68. this.colors = colors;
  69. length = colors.length;
  70. if (interval <= 0) {
  71. this.interval = INTERVAL;
  72. }
  73. else {
  74. this.interval = interval;
  75. }
  76. timer=new Timer(interval,new ActionListener() {
  77. public void actionPerformed(ActionEvent e) {
  78. repaint();
  79. }
  80. });
  81. timer.start();
  82. }
  83. /**
  84. * 以指定的文字、时间间隔以及颜色构造一个BlinkLabel。
  85. * @param text 显示字符串
  86. * @param interval 时间间隔
  87. * @param startColor 开始颜色
  88. * @param endColor 结束颜色
  89. */
  90. public BlinkLabel(String text, int interval, int startColor, int endColor) {
  91. if (text != null) {
  92. this.text = text;
  93. }
  94. else {
  95. this.text = TEXT;
  96. }
  97. if (interval <= 0) {
  98. this.interval = INTERVAL;
  99. }
  100. else {
  101. this.interval = interval;
  102. }
  103. length = 20;
  104. colors = new Color[length];
  105. setColors(startColor, endColor);
  106. timer=new Timer(interval,new ActionListener() {
  107. public void actionPerformed(ActionEvent e) {
  108. repaint();
  109. }
  110. });
  111. timer.start();
  112. }
  113. /**
  114. * 设置显示的文字。
  115. * @param text 显示的文字
  116. */
  117. public void setText(String text) {
  118. if (text != null) {
  119. this.text = text;
  120. }
  121. }
  122. /**
  123. * 设置颜色变化的时间间隔。
  124. * @param interval 时间间隔
  125. */
  126. public void setInterval(int interval) {
  127. if (interval >= 0) {
  128. this.interval = interval;
  129. timer.setDelay(interval);
  130. }
  131. }
  132. /**
  133. * 设置颜色数组。
  134. * 设置完成后会从头开始显示颜色。
  135. * @param colors 颜色数组
  136. */
  137. public void setColors(Color colors[]) {
  138. if (colors != null) {
  139. this.colors = colors;
  140. length = colors.length;
  141. count = 0;
  142. }
  143. }
  144. /**
  145. * 根据起止颜色设置颜色数组。
  146. * 设置完成后会从头开始显示颜色。
  147. * @param startColor 开始颜色
  148. * @param endColor 终了颜色
  149. */
  150. public void setColors(int startColor, int endColor) {
  151. float inte = (endColor - startColor) / (length - 2);
  152. float temp;
  153. for (int i = 0; i < length; i++) {
  154. temp = startColor + i * inte;
  155. if (temp > 0xffffff) {
  156. temp = 0xffffff;
  157. }
  158. if (temp < 0) {
  159. temp = 0;
  160. }
  161. colors[i] = new Color( (int) temp);
  162. }
  163. count = 0;
  164. }
  165. /**
  166. * 得到组件的最佳大小。
  167. * @return 组件的最佳大小
  168. */
  169. public Dimension getPreferredSize() {
  170. Graphics g = getGraphics();
  171. FontMetrics fontMetrics = g.getFontMetrics();
  172. int height = fontMetrics.getHeight() + 8;
  173. int width = fontMetrics.stringWidth(text) + 10;
  174. return new Dimension(width, height);
  175. }
  176. /**
  177. * 绘制组件。
  178. * @param g 图形设备
  179. */
  180. protected void paintComponent(Graphics g) {
  181. g.setColor(colors[count]);
  182. g.drawString(text, 5, 20);
  183. count++;
  184. if (count == colors.length) {
  185. count = 0;
  186. }
  187. }
  188. /**
  189. * 开始闪烁。
  190. */
  191. public void start() {
  192. count = 0;
  193. timer.start();
  194. }
  195. /**
  196. * 停止闪烁。
  197. */
  198. public void stop() {
  199. timer.stop();
  200. }
  201. }