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

展开全部

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

import java.awt.event.*;

import java.awt.Graphics;

public class yes {

public static void main(String args[])

{

Mywindow win=new Mywindow();

Ball qiu1=new Ball(5,5);

Thread thread1=new Thread(qiu1);

Ball qiu2=new Ball(10,5);

Thread thread2=new Thread(qiu2);

Ball qiu3=new Ball(15,5);

Thread thread3=new Thread(qiu3);

thread1.start();

thread2.start();

thread3.start();

}

}

class Mywindow extends Frame {

Mywindow (){

setSize(350,350);

setVisible(true);

setBackground(Color.BLACK);

validate();

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

}

class Ball extends Mywindow implements Runnable

{

int rgb=0;

Color color;

int x,y;

int dx=5,dy=5;

Ball(int x,int y){

this.x=x;

this.y=y;

}

public void doColor(){

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

color=new Color(rgb);

}

public void paint(Graphics g){

g.setColor(color);

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

}

public void run(){

while(true){

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

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

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

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

x=x+dx;

y=y+dy;

repaint();

try{Thread.sleep(50);}

catch(InterruptedException e) {;}

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值