- /**
- * <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.*;
- import java.util.Random;
-
- /**
- * 走马灯。
- * 以指定的时间间隔、步长以及滚动方向、滚动模式以及顺序显示文字。
- * 目前可以以循环、反弹以及随机模式滚动;方向则支持水平滚动和垂直滚动,顺序则支持升序和倒序。
- * 需要说明的还有步长,它可以为负值,因此如果滚动模式为循环、顺序为升序而步长为负数,则实际的效果应该是倒序。
- * @since 0.6
- */
- public class HorseLight
- extends JLabel {
- /**
- * 滚动方向,如果是水平滚动则表示从左到右,如果是垂直滚动则表示从上到下。
- */
- public static final int ASCENT = 1;
- /**
- * 滚动方向,如果是水平滚动则表示从右到左,如果是垂直滚动则表示从下到上。
- */
- public static final int REVERSE = -1;
- /**
- * 停止滚动。可以用于滚动方向也可以用于滚动模式。
- * 在此状态下虽然并没有变化,但是在指定的时间间隔中组件还是会进行重新绘制。
- */
- public static final int HALT = 0;
- /**
- * 循环滚动模式。
- */
- public static final int CIRCLE = 1;
- /**
- * 反弹滚动模式。<b>在反弹滚动模式下滚动方向没有意义。</b>
- */
- public static final int REFLECT = 2;
- /**
- * 随机滚动模式。<b>在随机滚动模式下滚动方向没有意义。</b>
- */
- public static final int RANDOM = 8;
-
- private int endX, endY; //终点坐标
- int spaceInterval; //行走间距
- int interval; //刷新速度
- String text; //显示文本
- private int currentX, currentY; //当前位置
- int order = ASCENT; //变化顺序
- int model = CIRCLE; //滚动模式
- int direction = HORIZONTAL; //滚动方向
- int scope; //滚动方向的长度
- Timer timer; //定时器
- private int directionFixer = 1;
- private static final String TEXT = "走马灯";
- private static final int INTERVAL = 100;
- private static final int SPACE_INTERVAL = 5;
-
- /**
- * 构造一个缺省的HorseLight。
- */
- public HorseLight() {
- this(TEXT, SPACE_INTERVAL, INTERVAL, -1);
- }
-
- /**
- * 以指定的文本构造一个HorseLight。
- * @param text 显示文本
- */
- public HorseLight(String text) {
- this(text, SPACE_INTERVAL, INTERVAL, -1);
- }
-
- /**
- * 以指定的文本以及时间间隔构造一个HorseLight。
- * @param text 显示文本
- * @param interval 时间间隔
- */
- public HorseLight(String text, int interval) {
- this(text, SPACE_INTERVAL, interval, -1);
- }
-
- /**
- * 以指定的文本以及时间间隔构造一个HorseLight。
- * @param text 显示文本
- * @param spaceInterval 间距
- * @param interval 时间间隔
- */
- public HorseLight(String text, int spaceInterval, int interval) {
- this(text, spaceInterval, interval, -1);
- }
-
- /**
- * 以指定的文本、间距以及时间间隔构造一个HorseLight。
- * @param text 显示文本
- * @param spaceInterval 间距
- * @param interval 时间间隔
- * @param scope 滚动范围
- */
- public HorseLight(String text, int spaceInterval, int interval, int scope) {
- this.text = text;
- this.spaceInterval = spaceInterval;
- if (interval > 0) {
- this.interval = interval;
- }
- else {
- this.interval = INTERVAL;
- }
- if (scope > 0) {
- this.scope = scope;
- }
- else {
- this.scope = -1;
- }
- currentX = 0;
- currentY = 0;
- timer = new Timer(interval, new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- repaint();
- }
- });
- timer.start();
- }
-
- /**
- * 设置循环模式。
- * 运动位置会恢复到从最开始开始。
- * @param model 滚动模式
- */
- public void setModel(int model) {
- currentX = 0;
- currentY = 0;
- this.model = model;
- }
-
- /**
- * 设置显示文本。
- * 运动位置会恢复到从最开始开始。
- * @param text 文本
- */
- public void setText(String text) {
- currentX = 0;
- currentY = 0;
- this.text = text;
- }
-
- /**
- * 设置滚动的时候间隔。
- * @param interval 时间间隔
- */
- public void setInterval(int interval) {
- if (interval > 0) {
- this.interval = interval;
- timer.setDelay(interval);
- }
- }
-
- /**
- * 设置滚动的间距。
- * @param spaceInterval 间距
- */
- public void setSpaceInterval(int spaceInterval) {
- this.spaceInterval = spaceInterval;
- }
-
- /**
- * 设置滚动的方向。
- * 运动位置会恢复到从最开始开始。
- * @param direction 滚动方向
- */
- public void setDirection(int direction) {
- currentX = 0;
- currentY = 0;
- this.direction = direction;
- }
-
- /**
- * 设置滚动顺序。
- * @param order 滚动顺序
- */
- public void setOrder(int order) {
- this.order = order;
- }
-
- /**
- * 得到组件的最佳大小。
- * 如果滚动方向是水平滚动而又没有设置滚动范围则最佳宽度为文本的宽度加上步长的五倍。
- * 如果滚动方向是垂直滚动而又没有设置滚动范围则最佳高度为文本的高度加上步长的五倍。
- * @return 组件的最佳大小
- */
- public Dimension getPreferredSize() {
- Graphics g = getGraphics();
- FontMetrics fontMetrics = g.getFontMetrics();
- int height = fontMetrics.getHeight() + 8;
- int width = fontMetrics.stringWidth(text) + 10;
- switch (direction) {
- case HORIZONTAL:
- if (scope > 0) {
- return new Dimension(scope, height);
- }
- else {
- return new Dimension(width + spaceInterval * 5, height);
- }
- case VERTICAL:
- if (scope > 0) {
- return new Dimension(width, scope);
- }
- else {
- return new Dimension(width, height + spaceInterval * 5);
- }
- default:
- if (scope > 0) {
- return new Dimension(scope, height);
- }
- else {
- return new Dimension(width + spaceInterval * 5, height);
- }
- }
- }
-
- /**
- * 绘制组件。
- * @param g 图形设备
- */
- protected void paintComponent(Graphics g) {
- int width, height;
- width = getSize().width;
- height = getSize().height;
- FontMetrics fontMetrics = g.getFontMetrics();
- int fontHeight = fontMetrics.getHeight();
- int textWidth = fontMetrics.stringWidth(text);
- g.drawString(text, currentX, currentY);
- switch (direction) {
- case HORIZONTAL:
- endX = width - textWidth;
- break;
- case VERTICAL:
- endY = height;
- break;
- default:
- endX = width - textWidth;
- break;
- }
- setNextPosition();
- }
-
- /**
- * 设置下一次绘制的位置。
- */
- private void setNextPosition() {
- switch (model) {
- case HALT:
- return;
- case CIRCLE:
- switch (direction) {
- case HORIZONTAL:
- switch (order) {
- case HALT:
- return;
- case ASCENT:
- currentX += spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = 0;
- }
- return;
- case REVERSE:
- currentX -= spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = endX;
- }
- return;
- default:
- currentX += spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = 0;
- }
- return;
- }
- case VERTICAL:
- switch (order) {
- case HALT:
- return;
- case ASCENT:
- currentY += spaceInterval;
- if (currentY > endY || currentY < 0) {
- currentY = 0;
- }
- System.out.println(spaceInterval + ",currentY:" + currentY +
- ",endY:" + endY);
- return;
- case REVERSE:
- currentY -= spaceInterval;
- if (currentY > endY || currentY < 0) {
- currentY = endY;
- }
- return;
- default:
- currentY += spaceInterval;
- if (currentY > endY || currentY < 0) {
- currentY = 0;
- }
- return;
- }
- default:
- switch (order) {
- case HALT:
- return;
- case ASCENT:
- currentX += spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = 0;
- }
- return;
- case REVERSE:
- currentX -= spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = endX;
- }
- return;
- default:
- currentX += spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = 0;
- }
- return;
- }
- }
- case REFLECT:
- switch (direction) {
- case HORIZONTAL:
- currentX += (directionFixer * spaceInterval);
- if (currentX > endX || currentX < 0) {
- directionFixer = -directionFixer;
- }
- return;
- case VERTICAL:
- currentY += (directionFixer * spaceInterval);
- if (currentY > endY || currentY < 0) {
- directionFixer = -directionFixer;
- }
- return;
- default:
- currentX += (directionFixer * spaceInterval);
- if (currentX > endX || currentX < 0) {
- directionFixer = -directionFixer;
- }
- return;
- }
- case RANDOM:
- Random random = new Random();
- switch (direction) {
- case HORIZONTAL:
- currentX = random.nextInt(endX);
- return;
- case VERTICAL:
- currentY = random.nextInt(endY);
- return;
- default:
- currentX = random.nextInt(endX);
- return;
- }
- default:
- switch (direction) {
- case HORIZONTAL:
- switch (order) {
- case HALT:
- return;
- case ASCENT:
- currentX += spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = 0;
- }
- return;
- case REVERSE:
- currentX -= spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = endX;
- }
- return;
- default:
- currentX += spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = 0;
- }
- return;
- }
- case VERTICAL:
- switch (order) {
- case HALT:
- return;
- case ASCENT:
- currentY += spaceInterval;
- if (currentY > endY || currentY < 0) {
- currentY = 0;
- }
- System.out.println(spaceInterval + ",currentY:" + currentY +
- ",endY:" + endY);
- return;
- case REVERSE:
- currentY -= spaceInterval;
- if (currentY > endY || currentY < 0) {
- currentY = endY;
- }
- return;
- default:
- currentY += spaceInterval;
- if (currentY > endY || currentY < 0) {
- currentY = 0;
- }
- return;
- }
- default:
- switch (order) {
- case HALT:
- return;
- case ASCENT:
- currentX += spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = 0;
- }
- return;
- case REVERSE:
- currentX -= spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = endX;
- }
- return;
- default:
- currentX += spaceInterval;
- if (currentX > endX || currentX < 0) {
- currentX = 0;
- }
- return;
- }
- }
- }
- }
-
- /**
- * 重新开始运动显示。
- */
- public void start() {
- currentX = 0;
- currentY = 0;
- timer.start();
- }
-
- /**
- * 停止运动。
- */
- public void stop() {
- timer.stop();
- }
- }