- /**
- * <p>Copyright: Copyright (c) 2002-2004</p>
- * <p>Company: JavaResearch(http://www.javaresearch.org)</p>
- * <p>最后更新日期:2003年4月30日
- * @author cherami
- */
- package org.jr.swing;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- /**
- * 闪烁标签类。
- * 以指定的颜色和时间间隔闪烁显示文字。
- * @since 0.6
- */
- public class BlinkLabel
- extends JLabel {
- String text; //显示字符串
- Color colors[]; //变化颜色数组
- int interval; //颜色变化间隔
- int length; //颜色值总长
- int count = 0; //当前颜色值计数
- Timer timer;//定时器
- static final int INTERVAL = 1000;
- static final String TEXT="变色文字";
- /**
- * 以文字“变色文字”为显示值,以1000毫秒为间隔,颜色渐变模式构造一个BlinkLabel。
- */
- public BlinkLabel() {
- this(TEXT, INTERVAL, 0xff0000, 0xff);
- }
-
- /**
- * 以指定文字,以1000毫秒为间隔,颜色渐变模式构造一个BlinkLabel。
- * @param text 显示字符串
- */
- public BlinkLabel(String text) {
- this(text, INTERVAL, 0xff0000, 0xff);
- }
-
- /**
- * 以指定文字和颜色、以1000毫秒为间隔构造一个BlinkLabel。
- * @param text 显示字符串
- * @param colors 颜色数组
- */
- public BlinkLabel(String text, Color colors[]) {
- this(text, colors, INTERVAL);
- }
-
- /**
- * 以指定文字和时间间隔,颜色渐变模式构造一个BlinkLabel。
- * @param text 显示字符串
- * @param interval 时间间隔
- */
- public BlinkLabel(String text, int interval) {
- this(text, interval, 0xff0000, 0xff);
- }
-
- /**
- * 以指定的文字、时间间隔以及颜色构造一个BlinkLabel。
- * @param text 显示字符串
- * @param colors 颜色数组
- * @param interval 时间间隔
- */
- public BlinkLabel(String text, Color colors[], int interval) {
- if (text != null) {
- this.text = text;
- }
- else {
- this.text = TEXT;
- }
- this.colors = colors;
- length = colors.length;
- if (interval <= 0) {
- this.interval = INTERVAL;
- }
- else {
- this.interval = interval;
- }
- timer=new Timer(interval,new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- repaint();
- }
- });
- timer.start();
- }
-
- /**
- * 以指定的文字、时间间隔以及颜色构造一个BlinkLabel。
- * @param text 显示字符串
- * @param interval 时间间隔
- * @param startColor 开始颜色
- * @param endColor 结束颜色
- */
- public BlinkLabel(String text, int interval, int startColor, int endColor) {
- if (text != null) {
- this.text = text;
- }
- else {
- this.text = TEXT;
- }
- if (interval <= 0) {
- this.interval = INTERVAL;
- }
- else {
- this.interval = interval;
- }
- length = 20;
- colors = new Color[length];
- setColors(startColor, endColor);
- timer=new Timer(interval,new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- repaint();
- }
- });
- timer.start();
- }
-
- /**
- * 设置显示的文字。
- * @param text 显示的文字
- */
- public void setText(String text) {
- if (text != null) {
- this.text = text;
- }
- }
-
- /**
- * 设置颜色变化的时间间隔。
- * @param interval 时间间隔
- */
- public void setInterval(int interval) {
- if (interval >= 0) {
- this.interval = interval;
- timer.setDelay(interval);
- }
- }
-
- /**
- * 设置颜色数组。
- * 设置完成后会从头开始显示颜色。
- * @param colors 颜色数组
- */
- public void setColors(Color colors[]) {
- if (colors != null) {
- this.colors = colors;
- length = colors.length;
- count = 0;
- }
- }
-
- /**
- * 根据起止颜色设置颜色数组。
- * 设置完成后会从头开始显示颜色。
- * @param startColor 开始颜色
- * @param endColor 终了颜色
- */
- public void setColors(int startColor, int endColor) {
- float inte = (endColor - startColor) / (length - 2);
- float temp;
- for (int i = 0; i < length; i++) {
- temp = startColor + i * inte;
- if (temp > 0xffffff) {
- temp = 0xffffff;
- }
- if (temp < 0) {
- temp = 0;
- }
- colors[i] = new Color( (int) temp);
- }
- count = 0;
- }
- /**
- * 得到组件的最佳大小。
- * @return 组件的最佳大小
- */
- public Dimension getPreferredSize() {
- Graphics g = getGraphics();
- FontMetrics fontMetrics = g.getFontMetrics();
- int height = fontMetrics.getHeight() + 8;
- int width = fontMetrics.stringWidth(text) + 10;
- return new Dimension(width, height);
- }
-
- /**
- * 绘制组件。
- * @param g 图形设备
- */
- protected void paintComponent(Graphics g) {
- g.setColor(colors[count]);
- g.drawString(text, 5, 20);
- count++;
- if (count == colors.length) {
- count = 0;
- }
- }
- /**
- * 开始闪烁。
- */
- public void start() {
- count = 0;
- timer.start();
- }
- /**
- * 停止闪烁。
- */
- public void stop() {
- timer.stop();
- }
- }