?? shadowtextapplet.java
字號:
import java.awt.*;
import java.applet.*;
import java.util.Random;
//跳動文字
public class ShadowTextApplet extends Applet implements Runnable{
String message; //待顯示的文本信息
Thread thread; //實現文字運動的線程
int fontHeight,speed,baseline; //字體高度,運動速度和基線
Color textColor,bgColor,shadomColor; //文字顏色、背景顏色與陰影顏色
Image newImage; //實現跳動的Image對象
Graphics newGraphics; //實現跳動的Graphics對象
boolean normal; //文字是否跳動的標志
Font font; //顯示字體
FontMetrics fontMetric; //顯示字體的FontMetrics對象
public void init(){ //初始化
Graphics graphics = getGraphics(); //得到graphics對象
Dimension dim=getSize(); //得到尺寸
fontHeight=dim.height-10; //根據Applet尺寸設置文字高度
newImage=createImage(dim.width,dim.height); //創建newImage對象
newGraphics = newImage.getGraphics(); //得到Graphics對象
message=getParameter("text"); //得到顯示文字
if (message==null){
message="陰影文字"; //設置默認文字
}
int textWidth=dim.width-(message.length() + 1)*5-10; //設置文字寬度
do{
graphics.setFont(new Font("TimesRoman", 1, fontHeight)); //設置顯示字體
fontMetric = graphics.getFontMetrics(); //得到FontMetric對象
if(fontMetric.stringWidth(message)>textWidth) //根據文字寬度調整其高度
fontHeight--;
}
while(fontMetric.stringWidth(message) > textWidth);{
baseline = getSize().height - fontMetric.getMaxDescent(); //調整顯示基線位置
}
font = new Font("TimesRoman", 1, fontHeight); //得到字體實例
String param; //參數字符串
if((param = getParameter("TEXTCOLOR")) == null) //得到文本顏色
textColor = Color.black; //設置默認文本顏色
else
textColor = new Color(Integer.parseInt(param)); //設置文本顏色
if((param = getParameter("BGCOLOR")) == null) //得到背景顏色
bgColor = Color.white; //設置默認背景顏色
else
bgColor = new Color(Integer.parseInt(param));
if((param = getParameter("SHADOMCOLOR")) == null) //得到陰影顏色
shadomColor = Color.lightGray; //設置默認陰影顏色
else
shadomColor = new Color(Integer.parseInt(param));
if((param = getParameter("NORMAL")) != null) //是否是靜態文本
normal = (Integer.valueOf(param).intValue()!=0); //參數值不為零,則為靜態文本
setBackground(bgColor); //設置背景顏色
if((param = getParameter("SPEED")) != null) //得到運動速度
speed = Integer.valueOf(param).intValue();
if(speed == 0)
speed = 200; //設置默認運動速度
thread = new Thread(this); //實例化運動文字線程
}
public void start(){ //開始運行線程
if(thread == null) {
thread = new Thread(this); //實例化線程
}
thread.start(); //線程運行
}
public void run(){ //線程運行主體
while(thread!=null) {
try{
Thread.sleep(speed); //線程休眠,即跳動間隔時間
}
catch(InterruptedException ex) {}
repaint(); //重繪屏幕
}
System.exit(0); //退出程序
}
public void paint(Graphics g) { //繪制Applet
if(normal) { //如果是靜態文本
g.setColor(bgColor); //設置當前顏色
g.fillRect(0, 0, getSize().width, getSize().height); //繪制填充矩形
g.setColor(textColor); //設置當前顏色
g.setFont(font); //設置當前字體
g.drawString(message, (getSize().width - fontMetric.stringWidth(message)) / 2, baseline); //繪出字符串
}
}
public void update(Graphics g){ //更新Applet
newGraphics.setColor(bgColor); //設置當前顏色
newGraphics.fillRect(0, 0, getSize().width, getSize().height); //繪制填充矩形
newGraphics.setColor(textColor); //設置當前顏色
newGraphics.setFont(font); //設置字體
if(!normal){ //如果是跳動文字
java.util.Random r=new java.util.Random();
int xpoint = r.nextInt(fontMetric.stringWidth(message)); //生成隨機X坐標
font = new Font("TimesRoman",Font.BOLD,30); //設置字體
newGraphics.setFont(font); //設置當前字體
newGraphics.setColor(shadomColor); //設置當前顏色
newGraphics.drawString(message,xpoint+3,baseline +3); //繪制陰影
newGraphics.setColor(textColor); //設置文本顏色
newGraphics.drawString(message,xpoint,baseline); //繪字符串
}
else { //如果是靜態文本
font = new Font("TimesRoman",Font.BOLD,30); //設置字體
newGraphics.setFont(font); //設置當前字體
newGraphics.setColor(shadomColor); //設置當前顏色
newGraphics.drawString(message,xpoint+3,baseline +3); //繪制陰影
newGraphics.setColor(textColor); //設置文本顏色
newGraphics.drawString(message,xpoint,baseline); //繪字符串
}
g.drawImage(newImage, 0, 0, this); //繪制Image
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -