import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;public class ExampleApplet extends java.applet.Applet implements Runnable{
int frame;
int delay;
Thread animator;
Image world; //要定义在这里
Image car;
public void init(){
world=getImage(getCodeBase(),"a.gif");
car=getImage(getCodeBase(),"b.gif"); //不要盘符,将图片放在和class在一个目录下
String str=getParameter("fps");
int fps=(str!=null)?Integer.parseInt(str):10;
delay=(fps>0)?(1000/fps):100;
}
public void start(){
animator=new Thread(this);
animator.start();
}
public void run(){
while(Thread.currentThread()==animator){
repaint();
try{
Thread.sleep(delay);
}catch(InterruptedException e){
break;
}
frame++;
}
}
public void stop(){
animator=null;
}
/*public void paint(Graphics g){
g.setColor(Color.black);
g.drawString("Frame"+frame,0,30);
}*/ public void update(Graphics g){
Dimension d=getSize(); //最好不要用size
Image offImage=null; //这里要赋值
Dimension offDimension;
Graphics offGraphics;
offGraphics=g; //这里也要赋值
offDimension=new Dimension(); //这个不知道什么意思,没有初始值,暂时这么写把.
if((offGraphics==null)||(d.width!=offDimension.width)||(d.height!=offDimension.height)){
offDimension=d;
offImage=createImage(d.width,d.height);
offGraphics=offImage.getGraphics();
}
offGraphics.setColor(getBackground());
offGraphics.fillRect(0,0,d.width,d.height);
offGraphics.setColor(Color.black);
paintFrame(offGraphics);
g.drawImage(offImage,0,0,null);
} //这里缺少一个"}"
public void paintFrame(Graphics g){
Dimension d=getSize();
int w=world.getWidth(this);
int h=world.getHeight(this);
if((w>0)&&(h>0)){
g.drawImage(world,(d.width-w)/2,(d.height-h)/2,this);
}
w=car.getWidth(this);
h=car.getHeight(this);
if((w>0)&&(h>0)){
w+=d.width;
g.drawImage(car,d.width-((frame*5)%w),(d.height-h)/3,this);
g.drawImage(car,d.width-((frame*7)%w),(d.height-h)/2,this);
}
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;public class ExampleApplet extends java.applet.Applet implements Runnable{
int frame;
int delay;
Thread animator;
Image world; //要定义在这里
Image car;
public void init(){
world=getImage(getCodeBase(),"a.gif");
car=getImage(getCodeBase(),"b.gif"); //不要盘符,将图片放在和class在一个目录下
String str=getParameter("fps");
int fps=(str!=null)?Integer.parseInt(str):10;
delay=(fps>0)?(1000/fps):100;
}
public void start(){
animator=new Thread(this);
animator.start();
}
public void run(){
while(Thread.currentThread()==animator){
repaint();
try{
Thread.sleep(delay);
}catch(InterruptedException e){
break;
}
frame++;
}
}
public void stop(){
animator=null;
}
/*public void paint(Graphics g){
g.setColor(Color.black);
g.drawString("Frame"+frame,0,30);
}*/ public void update(Graphics g){
Dimension d=getSize(); //最好不要用size
Image offImage=null; //这里要赋值
Dimension offDimension;
Graphics offGraphics;
offGraphics=g; //这里也要赋值
offDimension=new Dimension(); //这个不知道什么意思,没有初始值,暂时这么写把.
if((offGraphics==null)||(d.width!=offDimension.width)||(d.height!=offDimension.height)){
offDimension=d;
offImage=createImage(d.width,d.height);
offGraphics=offImage.getGraphics();
}
offGraphics.setColor(getBackground());
offGraphics.fillRect(0,0,d.width,d.height);
offGraphics.setColor(Color.black);
paintFrame(offGraphics);
g.drawImage(offImage,0,0,null);
} //这里缺少一个"}"
public void paintFrame(Graphics g){
Dimension d=getSize();
int w=world.getWidth(this);
int h=world.getHeight(this);
if((w>0)&&(h>0)){
g.drawImage(world,(d.width-w)/2,(d.height-h)/2,this);
}
w=car.getWidth(this);
h=car.getHeight(this);
if((w>0)&&(h>0)){
w+=d.width;
g.drawImage(car,d.width-((frame*5)%w),(d.height-h)/3,this);
g.drawImage(car,d.width-((frame*7)%w),(d.height-h)/2,this);
}
}
}
Thank you very much!!!