⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ticker.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

import java.awt.*;

public class Ticker
        extends Canvas
        implements Runnable
{
    private int xPos, yPos;
    private int height, width;
    private Thread runner = null;
    private String text = null;
    private FontMetrics metrics = null;
    private Font font = new Font("Monospaced", Font.BOLD, 12);

    public Ticker(String _text)
    {
        text = _text;
        metrics = getFontMetrics(font);
        width = metrics.stringWidth(text);
        height = metrics.getHeight();
        xPos = getSize().width;
        yPos = height;
        start();
    }

    public void start()
    {
        if (runner == null)
        {
            runner = new Thread(this);
            runner.start();
        }
    }

    public void stop()
    {
        runner = null;
    }

    public void run()
    {
        Thread currentThread = Thread.currentThread();
        while (runner == currentThread)
        {
            computeCoordinates();
            repaint();
            try
            {
                runner.sleep(50);
            }
            catch (InterruptedException ie)
            {
                System.err.println("Error: " + ie);
            }
        }
    }

    public void paint(Graphics g)
    {
        g.setFont(font);
        g.drawString(text, xPos, yPos);
    }

    private void computeCoordinates()
    {
        if (xPos < -width)
        {
            xPos = getSize().width;
        }
        else
        {
            xPos -= 2;
        }
    }

    public Dimension getPreferredSize()
    {
        return new Dimension(width, height + 3);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -