java 多线程 动画_简单的JAVA多线程动画

求把这个程序改成两个小球随机地移动。最好把程序加上注释importjava.awt.*;importjava.awt.event.*;publicclassExample9_5{publicstaticvoidmain(Stringargs[]){MyFrameframe=newMyF...

求把这个程序改成两个小球随机地移动。最好把程序加上注释import java.awt.*;import java.awt.event.*;public class Example9_5 { public static void main(String args[]) { MyFrame frame=new MyFrame(); frame.setBounds(10,10,500,500) ; frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }}class MyFrame extends Frame implements Runnable{ Thread 红色球,兰色球; MyCanvas red,blue; double t=0; MyFrame() { 红色球=new Thread(this); 兰色球=new Thread(this); red=new MyCanvas(Color.red); blue=new MyCanvas(Color.blue); setLayout(null); add(red); add(blue); red.setLocation(60,100); blue.setLocation(60,100); 红色球.start(); 兰色球.start(); } public void run() { while(true) { t=t+0.2; if(t>20) t=0; if(Thread.currentThread()==红色球) { int x=60; int h=(int)(1.0/2*t*t*3.8)+60; red.setLocation(x,h); try{ 红色球.sleep(50); } catch(InterruptedException e){} } else if(Thread.currentThread()==兰色球) { int x=60+(int)(26*t); int h=(int)(1.0/2*t*t*3.8)+60; blue.setLocation(x,h); try{ 兰色球.sleep(50); } catch(InterruptedException e){} } } } }class MyCanvas extends Canvas{ Color c; MyCanvas(Color c) { setSize(20,20); this.c=c; } public void paint(Graphics g) { g.setColor(c); g.fillOval(0,0,20,20); }}

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值