线程的基本应用--续

今天在昨天的基础上新增加了一些线程的知识,包括:线程的监听和线程的控制。
线程的监听即是在开始start()主要运行的线程之前,先开始一个监听的线程,类似鼠标监听器。在监听线程中可以设置小球间的碰撞。
以下是程序代码:
1.界面类中的代码
// 创建监听器对象(匿名内部类)
ActionListener alis = new ActionListener() {

public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// 启动监视线程
BallListener blis = new BallListener(list,getUI());
blis.start();
if (command.equals("开始")) {
panel.paint(g);
Ball.initBall();
// 创建一个小球对象
Ball ball = new Ball(getUI());
ball.start();
list.add(ball);
//将开始按钮设为 暂停
jbu.setText("暂停");
}
}
}

2.线程监听类
public class BallListener extends Thread {

// 创建队列以存放小球对象
ArrayList<Ball> alis;
// 得到的画布对象
Graphics g;
BallUI ballui;

/**
* 构造方法
*
* @param alis
* 传入存放小球的队列对象
*/
public BallListener(ArrayList<Ball> alis,BallUI ballui) {
this.alis = alis;
this.ballui = ballui;

// 由窗体对象得到画布对象
g = ballui.panel.getGraphics();
}

public void run() {
while (!Ball.isStop) {
while (!Ball.isPause) {
//依次比较两个小球对象
for (int i = 0; i < alis.size() ; i++) {
for(int j = i+1; j< alis.size() ; j++){
Ball b = alis.get(i);
Ball b1 = alis.get(j);
double distance =Math.hypot( ((b.x+b.size/2)-(b1.x+b1.size/2)),((b.y+b.size/2)-(b1.y+b1.size/2)));
if(distance <= b.size){
int tempx,tempy;
tempx = b.x;
b.x = b1.x;
b1.x = tempx;
tempy = b.y;
b.y = b1.y;
b1.y = tempy;
// 擦除球
g.setColor(ballui.panel.getBackground());
g.fillOval(b.x, b.y, b.size, b.size);
g.fillOval(b1.x, b1.y, b1.size, b1.size);
// g.setColor(Color.BLACK);
// g.fillOval(b.x = b.x + b.dx, b.y = b.y + b.dy, b.size, b.size);
// g.fillOval(b1.x = b1.x + b1.dx, b1.y = b1.y + b1.dy, b.size, b.size);

}
}
}
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Thread.sleep(10);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}

线程的控制即是改变while语句的判断条件的变量,并且在监听器类中用方法改变这些变量,以达到控制的目的。
public void move() {
while (!isStop) {
while (!isPause) {
// 擦除球
g.setColor(ballui.panel.getBackground());
g.fillOval(x, y, size, size);
// 画球
g.setColor(Color.BLACK);
if ((x < (425-size) && x > 0) && (y > 0 && y < (400-size))) {// 如果球未碰边
g.fillOval(x = x + dx, y = y + dy, 30, 30);
} else if (!(x < (425-size) && x > 0) && !(y > 0 && y < (400-size))) {// 如果球同时碰2两条边
dx = -dx;
dy = -dy;
g.fillOval(x = x + dx, y = y + dy, 30, 30);
} else if ((x < (425-size) && x > 0) && !(y > 0 && y < (400-size))) {// 如果球碰竖边
dy = -dy;
g.fillOval(x = x + dx, y = y + dy, 30, 30);
} else if (!(x < (425-size) && x > 0) && (y > 0 && y < (400-size))) {// 如果球碰横边
dx = -dx;
g.fillOval(x = x + dx, y = y + dy, 30, 30);
}
try {
sleep(20);
} catch (Exception e1) {
e1.printStackTrace();
}
}
try {
Thread.sleep(1);// 沉睡时间,暂停时的休眠
} catch (Exception ef) {
ef.printStackTrace();
}
}
}

/**
* 暂停线程的方法
*/
public static void pauseThread() {
isPause = true;
}

/**
* 继续线程的方法
*/
public static void resumeThread() {
isPause = false;
}

/**
* 停止线程的方法
*/
public static void stopThread() {
isPause = true;
isStop = true;
}

/**
* 初始化线程的方法
*/
public static void initBall() {
isPause = false;
isStop = false;
}

此程序的运行图
[img]http://dl.iteye.com/upload/attachment/529256/003fb3eb-15bb-39bd-9b2f-9a860dd761f9.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值