import java.awt.*;
import java.applet.*;
/**
* <p>Title: 帶陰影的文字</p>
* <p>Description: 使用Applet和Graphics,實現一個文字的移動廣告。</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: ShadowText.java</p>
* @version 1.0
*/
public class ShadowText extends Applet implements Runnable
{
private Image img;
private Image offI;
private Graphics offG;
private Thread thread = null;
private int height,width;
private String text;
private int FontSize;
private Font font;
private int textcolor, backcolor, shadowcolor;
/**
*<br>方法說明:Applet初始化,浏覽器加載Applet是調用。
*<br>輸入參數:
*<br>返回類型:
*/
public void init()
{
width = this.size().width;
height = this.size().height;
//獲取顯示信息
String s = new String(getParameter("Text"));
text = new String("Hello");
if(s != null)
text = s;
//獲取字體大小
FontSize = 30;
s = new String(getParameter("FontSize"));
if(s != null)
FontSize = Integer.parseInt(s);
//獲得字體顏色
s = getParameter("Fore");
textcolor = (s==null) ? 0x000000 : Integer.parseInt(s, 16);
//獲取背景顏色
s = getParameter("Back");
backcolor = (s==null) ? 0x000000 : Integer.parseInt(s, 16);
//獲取陰影顏色
s = getParameter("shadow");
shadowcolor = (s==null) ? 0x000000 : Integer.parseInt(s, 16);
//設置背景顏色
this.setBackground(new Color(backcolor));
//使用Graphics創建一張圖片
img = createImage(width+300,height);
Graphics temp = img.getGraphics();
temp.setColor(new Color(backcolor));
temp.fillRect(0,0,width,height);
temp.setColor(new Color(shadowcolor));
font = new Font("TimesRoman",Font.BOLD,FontSize);
temp.setFont(font);
temp.drawString(text,10,height*3/4);
temp.setColor(new Color(textcolor));
temp.drawString(text,10-3,height*3/4 - 3);
//構造可控制的圖片對象
offI = createImage(width,height);
offG = offI.getGraphics();
}
/**
*<br>方法說明:重載start()方法,啟動線程
*<br>輸入參數:
*<br>返回類型:
*/
public void start()
{
if(thread == null)
{
thread = new Thread(this);
thread.start();
}
}
/**
*<br>方法說明:線程體。繪制屏幕
*<br>輸入參數:
*<br>返回類型:
*/
public void run()
{
int x=width;
while(thread != null)
{
try
{
offG.drawImage(img,x,0,this);
repaint();
thread.sleep(20);
}
catch(InterruptedException e){}
x-=3;
if(x < -img.getWidth(this))
{
x = width;
}
}
}
/**
*<br>方法說明:Appletg更新畫面調用的方法
*<br>輸入參數:Graphics g 繪圖對象
*<br>返回類型:
*/
public void update(Graphics g)
{
paint(g);
}
/**
*<br>方法說明:Applet繪制屏幕的方法
*<br>輸入參數:
*<br>返回類型:
*/
public void paint(Graphics g)
{
g.drawImage(offI,0,0,this);
}
}