java 歌词_请问吧内有大神用JAVA做过桌面歌词吗

这篇博客展示了如何使用Java创建一个简单的桌面歌词组件。通过一个TextChangePane类,实现了文字颜色渐变和滚动效果。利用Timer进行定时更新,LinearGradientPaint实现颜色过渡,并通过repaint()方法更新显示。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

写了个简单的例子给你:

public class TextChangePane extends JComponent implements ActionListener {

private static final int CYCLE_TIME = 10000;

private long startTime = 0;

private long nowTime = 0;

private float move = 0.0001f;

private Timer timer;

private String text = "一二三四五六七八九零";

private Color startColor = Color.RED;

private Color endColor = Color.BLUE;

public TextChangePane() {

timer = new Timer(30, this);

timer.setInitialDelay(1000);

startTime = 1000 + System.nanoTime() / 1000000;

timer.start();

}

@Override

protected void paintComponent(Graphics g) {

float[] fractions = {0.f, move, move + 0.0001f, 1.f};

Color[] colors = {startColor, startColor, endColor, endColor};

FontMetrics metrics = g.getFontMetrics();

int length = metrics.stringWidth(text);

LinearGradientPaint paint = new LinearGradientPaint(0, 0, length, 0, fractions, colors);

Graphics2D g2d = (Graphics2D) g;

g2d.setPaint(paint);

g2d.drawString(text, 0, 20);

}

@Override

public void actionPerformed(ActionEvent e) {

nowTime = System.nanoTime() / 1000000;

long moveTime = nowTime - startTime;

if (moveTime > CYCLE_TIME) {

timer.stop();

}

move = (float)moveTime / (float)CYCLE_TIME;

if (move < 0.0001f) {

move = 0.0001f;

}

if (move > 0.9998f) {

move = 0.9998f;

}

repaint();

}

/* 测试用方法 */

private static void createAndShowGUI() {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 300);

frame.setLocationRelativeTo(null);

frame.add(new TextChangePane());

frame.setVisible(true);

}

/* 测试用方法 */

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

createAndShowGUI();

}

});

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值