📄 rainbow.java
字号:
import java.applet.Applet;
import java.awt.*;
public class Rainbow extends Applet implements Runnable
{
Thread thread;
Image offScreenImage; //位图
Graphics offScreenG; //位图刷
String str; //输入文字
Font f; //显示字体
FontMetrics fm; //字体规格
char theChars[]; //文字分解后的字符数组
int charoffsets[]; //字符基线偏移量
Color colors[]; //显示字符颜色
int i=20;
int strlen; //文字长度
int phase;
public void init()
{
thread=new Thread(this);
str=this.getParameter("text");
if(str==null)
str="THIS IS A TEST";
f=new Font("TimesRoman",1,36);
fm=getFontMetrics(f);
resize(40+fm.stringWidth(str),40);
this.setBackground(Color.cyan);
strlen=str.length();
theChars=new char[strlen];
charoffsets=new int[strlen];
str.getChars(0,strlen,theChars,0);
colors=new Color[strlen];
for(int j=0;j<strlen;j++)
{
float f1=(float)j/(float)strlen;
colors[j]=new Color(Color.HSBtoRGB(f1,1F,1F));
charoffsets[j]=i;
i+=fm.charWidth(theChars[j]);
}
offScreenImage = createImage(size().width,size().height);
offScreenG=offScreenImage.getGraphics();
offScreenG.setFont(f);
thread.start();
}
public void update(Graphics g)
{
offScreenG.setColor(Color.black);
offScreenG.fillRect(0,0,size().width,size().height);
phase++;
phase %=str.length();
for(int j=0;j<strlen;j++)
{
int k=charoffsets[j];
offScreenG.setColor(colors[(phase+j)%strlen]);
offScreenG.drawChars(theChars,j,1,k,30);
}
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(offScreenImage,0,0,this);
}
public void run()
{
while(true)
{
try
{
Thread.currentThread().sleep(100);
repaint();
}catch(Exception e){}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -