老师布置的作业代码写好了,三个球碰撞变色,可是出现闪烁了,模仿网上的双缓冲消除闪烁也没用,请大侠们看看错在哪里,怎么改。。。
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class lsn_4 extends Frame implements Runnable{
int x1=0,y1=100,x2=300,y2=100,x3=400,y3=400;
int d1,d2,d3;
int dx1=5,dy1=5,dx2=5,dy2=5,dy3=5,dx3=5;
Color color1,color2,color3;
int width,height;
Image ImageBuffer=null,img1,img2,img3;
Graphics GraImage=null;
int rgb1=(int)(Math.random()*0xFFFFFF),rgb2=(int)(Math.random()*0xFFFFFF),rgb3=(int)(Math.random()*0xFFFFFF);
public static void main(String args[]){
lsn_4 lsn=new lsn_4();
}
public lsn_4(){
GraphicsDevice gd=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
DisplayMode dm=gd.getDisplayMode();
width=dm.getWidth();
height=dm.getHeight();
setUndecorated(true);
setSize(width,height);
setVisible(true);
setBackground(Color.BLACK);
new Thread(this).start();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(e.getClickCount()==2)
System.exit(0);
}
});
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ESCAPE||e.getKeyCode()==KeyEvent.VK_SPACE)
System.exit(0);
}
});
}
public void run(){
while(true){
d1=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
d2=(x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
d3=(x3-x2)*(x3-x2)+(y3-y2)*(y3-y2);
if(x1<=0) {dx1=5;doColor1();}
else if((x1+100)>=getWidth()) {dx1=-5; doColor1();}
if(y1<=0) {dy1=5;doColor1();}
else if((y1+100)>=getHeight()) {dy1=-5; doColor1();}
if(x2<=0) {dx2=5;doColor2();}
else if((x2+100)>=getWidth()) {dx2=-5; doColor2();}
if(y2<=0) {dy2=5;doColor2();}
else if((y2+100)>=getHeight()) {dy2=-5; doColor2();}
if(x3<=0) {dx3=5;doColor3();}
else if((x3+100)>=getWidth()) {dx3=-5; doColor3();}
if(y3<=0) {dy3=5;doColor3();}
else if((y3+100)>=getHeight()) {dy3=-5; doColor3();}
if(d1<=10000){
dx1=-dx1;dy1=-dy1;
dx2=-dx2;dy2=-dy2;
doColor1();doColor2();
}
if(d2<=10000){
dx1=-dx1;dy1=-dy1;
dx3=-dx3;dy3=-dy3;
doColor1();doColor3();
}
if(d3<=10000){
dx2=-dx2;dy2=-dy2;
dx3=-dx3;dy3=-dy3;
doColor2();doColor3();
}
x1=x1+dx1;
y1=y1+dy1;
x2=x2+dx2;
y2=y2+dy2;
x3=x3+dx3;
y3=y3+dy3;
repaint();
try{Thread.sleep(50);}
catch(InterruptedException e){;}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(ImageBuffer==null)
ImageBuffer = createImage(getWidth(), this.getHeight()); //Image ImageBuffer=null;
GraImage=ImageBuffer.getGraphics(); // Graphics GraImage=null;
GraImage.drawImage(img1,x1,y1,this);
GraImage.drawImage(img2,x2,y2,this);
GraImage.drawImage(img3,x3,y3,this);
GraImage.dispose();
g.drawImage(ImageBuffer,0,0,this);
g.setColor(color1);
g.fillOval(x1, y1, 100, 100);
g.setColor(color2);
g.fillOval(x2, y2, 100, 100);
g.setColor(color3);
g.fillOval(x3, y3, 100, 100);
}
public void doColor1(){
rgb1=(int)(Math.random()*0xFFFFFF);
color1=new Color(rgb1);
}
public void doColor2(){
rgb2=(int)(Math.random()*0xFFFFFF);
color2=new Color(rgb2);
}
public void doColor3(){
rgb3=(int)(Math.random()*0xFFFFFF);