1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java小球左右_java小球碰撞窗体边缘来回反弹的代码

java小球左右_java小球碰撞窗体边缘来回反弹的代码

时间:2022-04-10 03:03:31

相关推荐

java小球左右_java小球碰撞窗体边缘来回反弹的代码

展开全部

没办法了 只能32313133353236313431303231363533e78988e69d8331333337393536注册一个马甲了importjava.awt.*;

importjava.awt.event.*;

importjava.awt.Graphics;

publicclassyes{

publicstaticvoidmain(Stringargs[])

{

Mywindowwin=newMywindow();

Ballqiu1=newBall(5,5);

Threadthread1=newThread(qiu1);

Ballqiu2=newBall(10,5);

Threadthread2=newThread(qiu2);

Ballqiu3=newBall(15,5);

Threadthread3=newThread(qiu3);

thread1.start();

thread2.start();

thread3.start();

}

}

classMywindowextendsFrame{

Mywindow(){

setSize(350,350);

setVisible(true);

setBackground(Color.BLACK);

validate();

addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

System.exit(0);

}

});

}

}

classBallextendsMywindowimplementsRunnable

{

intrgb=0;

Colorcolor;

intx,y;

intdx=5,dy=5;

Ball(intx,inty){

this.x=x;

this.y=y;

}

publicvoiddoColor(){

rgb=(int)(Math.random()*0xFFFFFF);

color=newColor(rgb);

}

publicvoidpaint(Graphicsg){

g.setColor(color);

g.fillOval(x,y,50,50);

}

publicvoidrun(){

while(true){

if(x<=0){dx=5;doColor();}

elseif((x+50)>=getWidth()){dx=-5;doColor();}

if(y<=0){dy=5;doColor();}

elseif((y+50)>=getHeight()){dy=-5;doColor();}

x=x+dx;

y=y+dy;

repaint();

try{Thread.sleep(50);}

catch(InterruptedExceptione){;}

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。