【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-WaveText.JAVA的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
import java.awt.*;
import java.applet.*;
public class WaveText extends Applet implements Runnable { //有线程运行接口
String myString = null;
int direct = 1;
int Hrad = 12;
int Vrad = 12;
Thread mythread = null; //设置一个线程
char[] words;
int phase = 0;
Image offI;
Graphics offG;
Color[] colors;
private Font f; //字体
private FontMetrics fm; //字模
public void init() {
String param = null;
myString = getParameter("word");
setBackground(Color.black);
words = new char [myString.length()];
myString.getChars(0,myString.length(),words,0); //将字符串中的各个字符保存到数组中
offI = createImage(getSize().width,getSize().height);
offG = offI.getGraphics();
f = new Font("TimesRoman",Font.BOLD,36);
fm=getFontMetrics(f);
offG.setFont(f);
float hue; //颜色的色元
colors = new Color[myString.length()];
for (int i = 0; i < myString.length(); i++) {
hue = ((float)i)/((float)myString.length());
colors[i] = new Color(Color.HSBtoRGB(hue,1.0f,1.0f)); //颜色分配
}
}
//线程开始的类
public void start() {
if(mythread == null) { //如果线程为空,则
mythread = new Thread(this); //开始新的线程
mythread.start(); //开始
}
}
//终止线程
public void stop() {
if (mythread != null) { //如果线程不为空,则
mythread.stop(); //终止线程,使它
mythread = null; //为空
}
}
//运行线程
public void run() {
while (mythread != null) {
try {
mythread.sleep(200); //让线程沉睡200毫秒
}catch (InterruptedException e) { }
repaint(); //重新绘制界面
}
}
public void update(Graphics g) { //重写update方法,解决闪烁问题
int x, y;
double angle;
offG.setColor(Color.black);
offG.fillRect(0,0,getSize().width,getSize().height);
phase+=direct;
phase%=8;
for(int i=0;i<myString.length();i++) {
angle = ((phase-i*direct)%8)/4.0*Math.PI;
x = 20+fm.getMaxAdvance()*i+(int) (Math.cos(angle)*Hrad);
y = 60+ (int) (Math.sin(angle)*Vrad);
offG.setColor(colors[(phase+i)%myString.length()]);
offG.drawChars(words,i,1,x,y);
}
paint(g);
}
public void paint(Graphics g) {
g.drawImage(offI,0,0,this); }
}
上一篇:
Water_MIDlet.java
下一篇:
计算机网络毕业论文