线程小球
2016-01-28 17:38
162 查看
线程小球也是这几天写的,就是让小球运动然后碰撞,通过这个程序来学习线程。
核心代码是运用线程判断小球碰撞和让小球碰撞
核心代码是运用线程判断小球碰撞和让小球碰撞
package BallThread; public class BallThread extends Thread{ public int LocationX = 100,LocationY = 100; public int r = 50 ; public MyBall myball; public int directX = 2,directY = 2; public BallThread(MyBall myball){ this.myball = myball; MyBall.list.add(this); } public BallThread(MyBall myball,int reLocationX,int reLocationY){ this.myball = myball; this.LocationX = reLocationX; this.LocationY = reLocationY; } public void run (){ while(true){ if(LocationX<=0||LocationX+r>=500){ directX = -(directX); } if(LocationY<=0||LocationY+r>=500){ directY = -(directY); } LocationX += directX; LocationY += directY; try { this.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } //判断小球碰撞 for(int i=0;i<MyBall.list.size();i++){ BallThread bt = MyBall.list.get(i);//获取容器中的小球 if((this.LocationX-bt.LocationX)*(this.LocationX-bt.LocationX)+(this.LocationY-bt.LocationY)*(this.LocationY-bt.LocationY)<=(this.r+bt.r)){ this.directX = -(this.directX); this.directY = -(this.directY); bt.directX = -(bt.directX); bt.directY = -(bt.directY); } } myball.repaint(); } } public void setLocationX(int reLocationX){ LocationX = reLocationX; } public void setLocationY(int reLocationY){ LocationY = reLocationY; } }
相关文章推荐
- POI dataFormat种类
- C#中Override关键字和New关键字的用法详解
- 反射
- 一条线
- Redis (一) CentOS 安装Redis 2.8.9
- sizeof与类的大小
- 专题4嵌入式文件系统制作
- 如何看懂源代码--(分析源代码方法)
- 【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)
- 图
- 纯真IP库PHP查询
- web开发中上传图片保存base64 好还是保存文件
- The Same Game-POJ1027模拟
- 英国调查报告称中国梦吸引力首超美国梦
- The Same Game-POJ1027模拟
- List<T> ForEach break
- js权威指南---学习笔记02
- Android开发——思路源泉
- java利用微信企业号将位置信息推送到普通微信号并在微信中直接查看地图位置
- Linux多线程编程