?? jumptextapplet.java
字號:
import java.awt.*;
import java.applet.*;
import java.util.Random;
//跳動文字
public class JumpTextApplet extends Applet implements Runnable{
String message; //待顯示的文本信息
Thread jumpThread; //實現跳動文字的線程
int fontHeight,speed,baseline; //字體高度,跳動速度和基線
Color textColor,bgColor; //文字顏色與背景顏色
Image jumpImage; //實現跳動的Image對象
Graphics jumpGraphics; //實現跳動的Graphics對象
boolean normal; //文字是否跳動的標志
Font font; //顯示字體
FontMetrics fontMetric; //顯示字體的FontMetrics對象
Color randomColors[]; //隨機生成顏色
boolean randomColor; //是否是隨機顏色
public void init(){ //初始化
Graphics graphics = getGraphics(); //得到graphics對象
Dimension dim=getSize(); //得到尺寸
fontHeight=dim.height-10; //根據Applet尺寸設置文字高度
jumpImage=createImage(dim.width,dim.height); //創建Image對象
jumpGraphics = jumpImage.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.lightGray; //設置默認背景顏色
else
bgColor = new Color(Integer.parseInt(param));
setBackground(bgColor); //設置背景顏色
if((param = getParameter("SPEED")) != null) //得到跳動速度
speed = Integer.valueOf(param).intValue();
if(speed == 0)
speed = 200; //設置默認跳動速度
if((param = getParameter("RANDOMCOLOR")) != null) //得到是否是隨機顏色參數
randomColor = (Integer.valueOf(param).intValue()!=0); //參數值不為零,則為隨機顏色
if((param = getParameter("NORMAL")) != null) //得到是否是隨機顏色參數
normal = (Integer.valueOf(param).intValue()!=0); //參數值不為零,則為隨機顏色
if (randomColor){ //初始化隨機顏色數組
Random random=new Random(); //實例化Random對象
int r,g,b; //顏色的RGB值
for (int i=0;i<10;i++){
r=random.nextInt(255); //得到0到255之間的隨機值
g=random.nextInt(255);
b=random.nextInt(255);
Color randomc=new Color(r,g,b); //生成顏色實例
randomColors[i]=randomc; //設置數組值
}
}
jumpThread = new Thread(this); //實例化跳動文字線程
}
public void start(){ //開始運行線程
if(jumpThread == null) {
jumpThread = new Thread(this);
}
jumpThread.start();
}
public void run(){ //線程運行主體
while(jumpThread!=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
jumpGraphics.setColor(bgColor); //設置當前顏色
jumpGraphics.fillRect(0, 0, getSize().width, getSize().height); //繪制填充矩形
jumpGraphics.setColor(textColor); //設置當前顏色
jumpGraphics.setFont(font); //設置字體
if(!normal){ //如果是跳動文字
int xpoint = 0;
for(int j = 0; j < message.length(); j++){
if(randomColor){
Color color;
while(bgColor == (color = randomColors[Math.min(9, (int)(Math.random()*10))])); //得到顏色數組中與背景色不同的一個隨機顏色
jumpGraphics.setColor(color); //設置當前顏色
}
xpoint += (int)(Math.random() * 10); //單個字符的X坐標
int ypoint = baseline - (int)(Math.random() * 10); //單個字符的Y坐標
String s1 = message.substring(j, j + 1);
jumpGraphics.drawString(s1, xpoint, ypoint);
xpoint += fontMetric.stringWidth(s1);
}
}
else { //如果是靜態文本
jumpGraphics.drawString(message, (getSize().width - fontMetric.stringWidth(message)) / 2, baseline); //繪制字符串
}
g.drawImage(jumpImage, 0, 0, this); //繪制Image
}
public JumpTextApplet() { //構造函數
speed = 100; //初始速度
normal = false; //初始時為動態文本
randomColors = new Color[10]; //初始化隨機顏色數組
randomColor = false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -