?? flytextapplet.java
字號(hào):
import java.awt.*;
import java.applet.*;
public class FlyTextApplet extends Applet implements Runnable{
Image image; //該Applet的Image對(duì)象
Graphics graphics; //該Applet的Graphics對(duì)象
Font font; //顯示字體
String message; //顯示文本
Thread thread; //飛行運(yùn)動(dòng)線程
int xpos, ypos, fontsize; //X坐標(biāo),Y坐標(biāo)及字體大小
public void init(){
image = createImage(getSize().width, getSize().height); //得到Image實(shí)例
graphics = image.getGraphics(); //得到Graphics實(shí)例
message = getParameter("Text"); //得到文本參數(shù)
if(message == null){ //如果顯示文本為空
message = "飛行文字"; //設(shè)置默認(rèn)文本
}
font = new Font("TimesRoman", Font.BOLD, 10); //實(shí)例化字體
}
public void start(){
if(thread == null){
thread = new Thread(this); //實(shí)例化線程
thread.start(); //運(yùn)行線程
}
}
public void run(){
while(thread != null){
if(fontsize >getSize().height) //如果字體尺寸過大
fontsize = 0; //重設(shè)字體尺寸
try{
Thread.sleep(50); //線程休眠
}catch (InterruptedException e) {}
repaint(); //重繪屏幕
}
}
public void stop(){
thread=null;
}
public void update(Graphics g){
graphics.setColor(Color.black); //設(shè)置當(dāng)前顏色
graphics.fillRect(0,0,getSize().width, getSize().height); //填充背景
font = new Font("TimesRoman", Font.BOLD, fontsize); //得到字體實(shí)例
graphics.setFont(font); //設(shè)置字體
graphics.setColor(Color.pink); //設(shè)置當(dāng)前顏色
FontMetrics fontMetrics = graphics.getFontMetrics(font); //得到字體的FontMetrics對(duì)象
int fontheight = fontMetrics.getHeight(); //得到字體高度
int width; //字體寬度
int baseline = getSize().height / 2 + fontheight / 2; //顯示文本基線
width = fontMetrics.stringWidth(message); //字符串寬度
width = (getSize().width - width) / 2; //顯示字符串寬度
graphics.drawString(message, width, baseline-=20); //繪制字符串
g.drawImage(image,0,0, this); //繪制Image對(duì)象
fontsize++; //增加字體尺寸
}
public void paint(Graphics g){
update(g);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -