移动背景图像 站点:爱心种子小博士 关键字:移动背景图像
|
移动背景图像
//一个移动背景图像的动画
import java.awt.*;
import java.applet.Applet;
public class Pan extends Applet implements Runnable{
Thread runner;
Image back,fore,workspace;
Graphics offscreen;
String text;
String fontName;
int fontSize=24;
int x1=0;
int x2;
public void init(){
workspace=createImage(size().width,size().height);
offscreen=workspace.getGraphics();
String imageBack=getParameter("background");
if(imageBack!=null)
back=getImage(getDocumentBase(),imageBack);
String imageFore=getParameter("foreground");
if(imageFore!=null)
back=getImage(getDocumentBase(),imageFore);
x2=size().width;
text=getParameter("text");
fontName=getParameter("font");
if(fontName==null)
fontName="Arial";
String param=getParameter("fontsize");
if(param!=null)
fontSize=Integer.parseInt("0"+param);
}
public void start(){
if(runner==null){
runner=new Thread(this);
runner.start();
}
}
public void stop(){
if(runner!=null){
runner=null;
}
}
public void run(){
Thread thisThread=Thread.currentThread();
while(runner==thisThread){
repaint();
try{ Thread.sleep(200);}
catch(InterruptedException e){}
x1=x1-5;
x2=x2-5;
if(x1<=(size().width*(-1)))
x1=size().width;
if(x2<=(size().width*(-1)))
x2=size().width;
}
}
public void paint(Graphics g){
offscreen.drawImage(back,x1,0,null);
offscreen.drawImage(back,x2,0,null);
if(fore!=null)
offscreen.drawImage(fore,0,0,null);
if(text!=null){
offscreen.setColor(Color.black);
Font f=new Font(fontName,Font.BOLD,fontSize);
FontMetrics fm=g.getFontMetrics();
offscreen.setFont(f);
int xStart=size().width/2-fm.stringWidth(text)/2-100;
int yStart=size().height/2+fm.getHeight()/2;
offscreen.drawString(text,xStart,yStart);
offscreen.setColor(Color.white);
offscreen.drawString(text,xStart-2,yStart-2);
}
g.drawImage(workspace,0,0,this);
}
public void update(Graphics g){
paint(g);
}
}
|
|
|
|