?? extendtextapplet.java
字號:
import java.awt.*;
import java.applet.*;
//伸展文字
public class ExtendTextApplet extends Applet implements Runnable{
Image image; //該Applet的Image對象
Graphics graphics; //該Applet的Graphics對象
int appletWidth,appletHeight; //Applet的高度,寬度
String message; //顯示文本
Thread thread; //動畫線程
int ypoint=0, xheight; //顯示文本的Y坐標和高度
int phase = 0; //狀態參數
Font font; //顯示文本的字體
public void init(){
font=new Font("TimesRoman",Font.BOLD,30); //實例化字體
appletWidth = getSize().width; //得到Applet的寬度
appletHeight = getSize().height; //得到Applet的高度
xheight = appletHeight / 3; //顯示文字高度值
ypoint = xheight; //顯示文字的Y坐標值
message = getParameter("Text"); //得到顯示信息
if(message==null)
message = "伸展文字"; //設置默認信息
image = createImage(appletWidth,appletHeight); //得到Image實例
graphics = image.getGraphics(); //得到Graphics實例
}
public void start(){
if(thread == null){
thread = new Thread(this); //實例化線程
thread.start(); //運行線程
}
}
public void update(Graphics g){
paint(g); //繪制屏幕
}
public void paint(Graphics g){
g.drawImage(image, 0, ypoint, appletWidth, xheight ,this); //繪制Image對象
}
public void run() {
try{
while(true){
ypoint = 0;
xheight = appletHeight;
graphics.setColor(Color.white); //設置當前顏色
graphics.fillRect(0 ,0, appletWidth, appletHeight); //填充背景
repaint(); //重繪屏幕
thread.sleep(100); //線程休眠100毫秒
if(phase==0){
graphics.setColor(Color.orange); //設置當前顏色
for(int i = appletWidth; i>=0; i--) { //線條的伸展效果
graphics.fillRect(i, appletHeight / 3 ,appletWidth, appletHeight /10); //填充矩形
repaint(); //重繪屏幕
thread.sleep(10); //線程休眠10毫秒
}
}
else if(phase == 1){
graphics.setColor(Color.pink); //設置當前顏色
for(int i = 0; i<=appletWidth; i++) {
graphics.fillRect(0, appletHeight / 3 ,i, appletHeight /10); //填充矩形
repaint(); //重繪屏幕
thread.sleep(10); //線程休眠10毫秒
}
}
ypoint = appletHeight/3;
xheight = appletHeight/3;
for(int i = appletHeight / 3; i>=0; i--){
ypoint--;
xheight += 2;
if(phase == 0){
graphics.setColor(Color.orange); //設置當前顏色
graphics.fillRect(0,0, appletWidth,appletHeight); //填充矩形
graphics.setFont(font); //設置當前字體
graphics.setColor(Color.white); //設置當前顏色
graphics.drawString(message,0,35); //繪制字符串
phase++; //改變狀態參數
}
else if(phase == 1){
graphics.setColor(Color.pink); //設置當前顏色
graphics.fillRect(0,0, appletWidth,appletHeight); //填充矩形
graphics.setFont(font); //設置當前字體
graphics.setColor(Color.black); //設置當前顏色
graphics.drawString(message,0,35); //繪制字符串
phase=0; //改變狀態參數
}
repaint(); //重繪屏幕
thread.sleep(100); //線程休眠100毫秒
}
thread.sleep(2000); //線程休眠2500毫秒
}
}
catch(InterruptedException e){}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -