package 碰壁球;
import java.applet.*;
import java.awt.*;
public class UsedoubleBuffer extends Applet implements Runnable{
int X,Y,moveX,moveY,width,height;
Image offI;
Graphics offG;
Thread thread;
public void init()
{
X=0; //X Y轴
Y=0;
moveX=2; //移动距离
moveY=3;
setSize(500,450);
width=getSize().width;
height=getSize().height;
offI=createImage(width,height);
offG=offI.getGraphics();
}
public void start()
{
if(thread==null)
{
thread=new Thread(this);
thread.start();
}
}
public void stop()
{
thread=null;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
offG.setColor(Color.black); //清除子画面
offG.fillRect(0, 0, width, height);
offG.setColor(Color.white);
offG.fillOval(X, Y, 30, 30);
g.drawImage(offI, 0, 0, this);
}
public void run()
{
while(true)
{
repaint();
try{
Thread.sleep(10);
}
catch(InterruptedException e){}
X=X+moveX;
Y=Y+moveY;
if(X>=width-30)
moveX=-moveX;
if(X<=0)
moveX=-moveX;
if(Y>=height-30)
moveY=-moveY;
if(Y<=0)
moveY=-moveY;
}
}
}