
| 一个Java小球撞击墙面的小程序1 |
| 2010/2/2 发布人:qiantai |
一个Java小球撞击墙面的小程序 import java.awt.; import java.applet.; public class OnlyChangeUpdate extends Applet implements Runnable { int X,Y,moveX,moveY,width,height; Thread newThread; public void init() { X=0; Y=0; moveX=20; moveY=20; width=getSize().width; height=getSize().height; setBackground(Color.black); } public void start() { newThread=new Thread(this); newThread.start(); } public void stop() { newThread=null; } public void paint(Graphics g) { g.setColor(Color.white); g.fillOval(X,Y,15,15); } public void update(Graphics g) { paint(g); } public void run() { while(newThread !=null) { repaint(); try { Thread.sleep(80); } catch(InterruptedException E){} X=X+moveX; Y=Y+moveY; if(X = (width - 15)) { X=width-15; moveX=-moveX; } if(X=0) { X=0; moveX=-moveX; } if(Y=(height-15)) { Y=height-15; moveY=-moveY+5; } if(Y=0) { Y=0; moveY=-moveY+5; } } } } 关键是屏幕不能刷新,我已经认识到这点了 后面讲到的双缓冲区可以解决的 TAG java |