?? wavetextapplet.java
字號:
import java.awt.*;
import java.applet.*;
//波浪文字
public class WaveTextApplet extends Applet implements Runnable {
String message; //顯示文本
int direct,phase; //運動方向參數
Thread thread; //波浪運動線程
char words[]; //顯示文本的字符數組
Image image; //Image對象
Graphics graphics; //Graphics對象
Color colors[]; //顯示文本顏色
private Font font; //顯示字體
private FontMetrics fontMetric; //顯示字體的 FontMetrics對象
public void init() {
direct=1; //初始方向值
phase = 0;
message = getParameter("Text"); //得以顯示文本
if (message==null){ //如果文本為空
message="波浪文字"; //設置默認文本
}
setBackground(Color.black); //設置背景色
words = new char [message.length()]; //初始化顯示字符數組
message.getChars(0,message.length(),words,0);
image = createImage(getSize().width,getSize().height); //得到Image實例
graphics = image.getGraphics(); //得到Graphics實例
font = new Font("TimesRoman",Font.BOLD,36); //設置顯示字體
fontMetric=getFontMetrics(font); //得到字體的FontMetric對象
graphics.setFont(font); //設置顯示字體
float h;
colors = new Color[message.length()]; //初始化顏色數組
for (int i = 0; i < message.length(); i++) {
h = ((float)i)/((float)message.length());
colors[i] = new Color(Color.HSBtoRGB(h,1.0f,1.0f)); //填充顏色數組數據
}
}
public void start() {
if(thread == null) {
thread = new Thread(this); //實例化線程
thread.start(); //運行線程
}
}
public void run() {
while (thread != null) {
try {
Thread.sleep(200); //線程休眠
}catch (InterruptedException e) {
}
repaint(); //重繪屏幕
}
}
public void update(Graphics g) {
int x, y; //顯示字符的X坐標,Y坐標
double ang;
int Hrad = 12;
int Vrad = 12;
graphics.setColor(Color.black); //設置當前顏色
graphics.fillRect(0,0,getSize().width,getSize().height); //填充背景
phase+=direct;
phase%=8;
for(int i=0;i<message.length();i++) {
ang = ((phase-i*direct)%8)/4.0*Math.PI; //運動角度
x = 20+fontMetric.getMaxAdvance()*i+(int)(Math.cos(ang)*Hrad); //字符的X坐標
y = 60+ (int) (Math.sin(ang)*Vrad); //字符的Y坐標
graphics.setColor(colors[(phase+i)%message.length()]); //設置文本顏色
graphics.drawChars(words,i,1,x,y); //顯示字符
}
g.drawImage(image,0,0,this); //繪制Image
}
public void paint(Graphics g) {
update(g);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -