我有这个代码,它应该让文字“简单动画”以旋转的颜色滚动屏幕。现在,它确实如此,但即使在文本移动之后,颜色仍然存在。我想知道是否有办法让背景中的文字。例如,我以为我可以打印出完全相同的“简单动画”,但颜色与背景颜色相同,实际文本后面约10个像素。然而,当我尝试这个时,白色文字(这是背景颜色)只是覆盖了旋转的颜色。如果我可以有背景文本,我尝试使用谷歌搜索,但从我读到,背景可以做的唯一事情就是设置颜色。那么,有没有办法在Java Graphics文件的后台使用文本?
这是我的代码:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
public class Scrolling_Sign extends JApplet implements Runnable {
String mesag = "Simple Animation";
Font f = new Font("Bauhaus 93",Font.BOLD,72);
Color colors[] = new Color[100000];
Thread runThread;
int Xposition = 600;
public void init() {
setBackground(Color.white);
}
public void start() {
if (runThread == null) {
runThread = new Thread(this);
runThread.start();
}
}
public void stop() {
if (runThread != null) {
runThread.stop();
runThread = null;
}
}
public void run() {
float c = 0;
for (int i = 0; i < colors.length; i++) {
colors[i] = Color.getHSBColor(c, (float)1.0,(float)1.0);
c += .02;
}
int i = 0;
while (true) {
setForeground(colors[i]);
repaint();
i++;
try { Thread.sleep(100); }
catch (InterruptedException e) { }
if (i == colors.length ) i = 0;
}
}
public void paint(Graphics g) {
g.setFont(f);
g.drawString(mesag,Xposition,100);
Xposition--;
if (Xposition < -290) {
Xposition = 600;
}
}
}
谢谢!
答案 0 :(得分:2)
建议:
super.paintComponent(g)
方法,然后再次阅读Swing tutorials以了解原因。有关更多教程,请参阅:Swing Info Thread#stop()
或使用任何其他已弃用的方法。请阅读Why is Thread.stop deprecated?。drawString(...)
。或者将JLabel放在背景图像上。