---
title: "线程的基本应用--续"
author: "hwfantasy"
date: "2011-08-03T00:42:16Z"
tags: ["Java", "小球碰撞", "线程"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-08-03T00:42:16Z"
id: 1138689
url: "https://hwfantasy.iteye.com/blog/1138689"
---

今天在昨天的基础上新增加了一些线程的知识，包括：线程的监听和线程的控制。 线程的监听即是在开始start()主要运行的线程之前，先开始一个监听的线程，类似鼠标监听器。在监听线程中可以设置小球间的碰撞。 以下是程序代码： 1.界面类中的代码 \[code="java"]// 创建监听器对象(匿名内部类) 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("暂停"); } } }\[/code] 2.线程监听类 \[code="java"]public class BallListener extends Thread { // 创建队列以存放小球对象 ArrayList alis; // 得到的画布对象 Graphics g; BallUI ballui; /\** * 构造方法 * * @param alis * 传入存放小球的队列对象 \*/ public BallListener(ArrayList 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 &lt; alis.size() ; i++) { for(int j = i+1; j&lt; 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 &lt;= 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(); } } } }\[/code] 线程的控制即是改变while语句的判断条件的变量，并且在监听器类中用方法改变这些变量，以达到控制的目的。 \[code="java"]public void move() { while (!isStop) { while (!isPause) { // 擦除球 g.setColor(ballui.panel.getBackground()); g.fillOval(x, y, size, size); // 画球 g.setColor(Color.BLACK); if ((x &lt; (425-size) && x &gt; 0) && (y &gt; 0 && y &lt; (400-size))) {// 如果球未碰边 g.fillOval(x = x + dx, y = y + dy, 30, 30); } else if (!(x &lt; (425-size) && x &gt; 0) && !(y &gt; 0 && y &lt; (400-size))) {// 如果球同时碰2两条边 dx = -dx; dy = -dy; g.fillOval(x = x + dx, y = y + dy, 30, 30); } else if ((x &lt; (425-size) && x &gt; 0) && !(y &gt; 0 && y &lt; (400-size))) {// 如果球碰竖边 dy = -dy; g.fillOval(x = x + dx, y = y + dy, 30, 30); } else if (!(x &lt; (425-size) && x &gt; 0) && (y &gt; 0 && y &lt; (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(); } } }\[/code] \[code="java"]/\** * 暂停线程的方法 \*/ 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; }\[/code] 此程序的运行图 \[img]http://dl.iteye.com/upload/attachment/529256/003fb3eb-15bb-39bd-9b2f-9a860dd761f9.jpg\[/img]