Java学习之道:多线程——让小球动起来(一)

本文介绍了Java中实现多线程的基本概念,并通过一个控制小球移动的例子来阐述多线程的运用。通过创建Ball类继承Thread,实现小球的移动、边界反弹和碰撞检测。通过启动多个小球线程,使得多个小球同时运动,展示了多线程在程序中的实际应用。

在讲线程之前,我们先来谈谈进程。在我接触电脑的一段时间后,开始发现当打开任务管理器后,会有一个进程的显示,下面的映像名称里有很多的可执行文件。这些可执行文件有的是系统默认的,有的是我们打开的,他们都会占用一定内存。
知道了进程,那么什么是线程呢?线程就是运行中程序的调度单位。
我们知道,以前我们写的代码,程序都是从上往下进行,就是必须要执行完上一句才会执行下一句。我们称这种为单线程的程序。那么多线程就是指同时有多个单线程同时进行运作,比如我们今天要做的小球游戏,
如果面板中有多个小球在运动,那么我们就要建立多个小球对象,然后让他们同时运动,但是如果按我们以前的方式,代码从上往下执行,那么想控制多个小球显然很困难,这时我们就要运用到多线程。

 

讲了那么多,多线程怎么实现呢?
这里有两个方法,一个是继承Thread类,一个是实现runnable接口。具体的应用我们在练习中体现。


练习:控制小球的移动,要求:1.小球不能超出边框2.小球相互碰撞,进行反弹.

这个练习首先要求我们掌握基本界面,知道主界面继承JPanel,重写paint方法以保存面板。然后创建小球类,写出构造方法以及get,set方法,继承线程类,重写run方法。最关键的就是小球要在边界处反弹,还有就是小球遇到其他的小球要发生碰撞。最后让线程run起来~~~

 

首先我们做出小球界面:

Java代码  收藏代码
  1. public class BallFrame extends JPanel {  
  2.     // 实例化一个数组对象  
  3.     private Ball[] ball = new Ball[10];  
  4.     // 实例化一个随机数对象  
  5.     private Random r = new Random();  
  6.   
  7.     public static void main(String[] args) {  
  8.         // 实例化一个面板对象  
  9.         BallFrame bf = new BallFrame();  
  10.         // 调用initUI方法  
  11.         bf.initUI();  
  12.     }  
  13.   
  14.     // 界面函数  
  15.     public void initUI() {  
  16.         JFrame jf = new JFrame();// 实例化面板对象  
  17.         jf.setSize(new Dimension(600600));// 设置面板大小  
  18.         jf.setResizable(false);// 设置不可调节大小  
  19.         jf.setDefaultCloseOperation(3);// 设置关闭按钮  
  20.         jf.setLocationRelativeTo(null);// 设置窗体居中  
  21.         this.setBackground(Color.white);// 设置面板背景为白色  
  22.         jf.setVisible(true);// 设置窗体可见  
  23.         jf.add(this, BorderLayout.CENTER);// 将面板添加到窗体上  
  24.   
  25.         for (int i = 0; i < ball.length; i++) {  
  26.             // 实例化每个小球对象  
  27.             ball[i] = new Ball(new Color(r.nextInt(255), r.nextInt(255),  
  28.                     r.nextInt(255)), r.nextInt(550), r.nextInt(550), 50,  
  29.                     r.nextInt(4) + 1, r.nextInt(4) + 1this, i);  
  30.         }  
  31.         for (int i = 0; i < ball.length; i++) {  
  32.             // 将每个小球线程运行起来  
  33.             ball[i].start();  
  34.         }  
  35.     }  
  36.   
  37.     // 重写paint方法  
  38.     public void paint(Graphics g) {  
  39.         // 调用父类的paint方法  
  40.         super.paint(g);  
  41.         for (int i = 0; i < ball.length; i++) {  
  42.             // 从ball中获取颜色并设置  
  43.             g.setColor(ball[i].getcolor());  
  44.             // 画出小球  
  45.             g.fillOval(ball[i].getX(), ball[i].getY(), ball[i].getRadiu(),  
  46.                     ball[i].getRadiu());  
  47.         }  
  48.         // 调用碰撞函数  
  49.         collision();  
  50.     }  
  51.   
  52.     // 碰撞函数  
  53.     private void collision() {  
  54.         // 距离数组,存储两小球间的距离  
  55.         double[][] dis = new double[ball.length][ball.length];  
  56.         for (int i = 0; i < ball.length; i++) {  
  57.             for (int j = 0; j < ball.length; j++) {  
  58.                 // 计算两个小球间的距离  
  59.                 dis[i][j] = Math.sqrt(Math.pow(ball[i].getX() - ball[j].getX(),  
  60.                         2) + Math.pow(ball[i].getY() - ball[j].getY(), 2));  
  61.             }  
  62.         }  
  63.         for (int i = 0; i < ball.length; i++) {  
  64.             for (int j = i + 1; j < ball.length; j++) {  
  65.                 if (dis[i][j] < (ball[i].getRadiu() + ball[j].getRadiu()) / 2) {  
  66.                     int t;  
  67.                     // 交换小球x方向的速度  
  68.                     t = ball[i].getVx();  
  69.                     ball[i].setVx(ball[j].getVx());  
  70.                     ball[j].setVx(t);  
  71.                     // 交换小球y方向的速度  
  72.                     t = ball[i].getVy();  
  73.                     ball[i].setVy(ball[j].getVy());  
  74.                     ball[j].setVy(t);  
  75.                     // 确定碰撞后第二个小球的位置  
  76.                     int x2 = ball[j].getX() - ball[i].getX(), y2 = ball[j]  
  77.                             .getY() - ball[i].getY();  
  78.                     ball[j].setX(ball[i].getX() + x2);  
  79.                     ball[j].setY(ball[j].getY() + y2);  
  80.                 } else {  
  81.                 }  
  82.             }  
  83.         }  
  84.     }  
  85. }  

 

然后我们创建线程对象Ball继承Thread:

Java代码  收藏代码
  1. public class Ball extends Thread {  
  2.     // 初始化一些对象名  
  3.     private Color color;  
  4.     private int x, y, radiu, vx, vy;  
  5.     private BallFrame bf;  
  6.     private int id;  
  7.   
  8.     /** 
  9.      * 构造函数 
  10.      *  
  11.      * @param color小球颜色 
  12.      * @param x小球横坐标 
  13.      * @param y小球纵坐标 
  14.      * @param radiu小球直径 
  15.      * @param vx小球横向速度 
  16.      * @param vy小球纵向速度 
  17.      * @param bf面板 
  18.      * @param id标志 
  19.      */  
  20.     public Ball(Color color, int x, int y, int radiu, int vx, int vy,  
  21.             BallFrame bf, int id) {  
  22.         this.color = color;  
  23.         this.x = x;  
  24.         this.y = y;  
  25.         this.radiu = radiu;  
  26.         this.vx = vx;  
  27.         this.vy = vy;  
  28.         this.bf = bf;  
  29.         this.id = id;  
  30.     }  
  31.   
  32.     // 重写run方法  
  33.     public void run() {  
  34.         super.run();// 调用父类run方法  
  35.         // 执行无限循环  
  36.         while (true) {  
  37.             // System.out.println("第"+id+"个球的x:"+x +"   y:"+y);  
  38.             x += vx;// 改变x的速度  
  39.             y += vy;// 改变y的速度  
  40.             // 如果x越界  
  41.             if (x <= 0 || x + radiu >= bf.getWidth()) {  
  42.                 vx = -vx;// x速度反向  
  43.                 if (x < 0)  
  44.                     x = 0;  
  45.                 else if (x > bf.getWidth() - radiu)  
  46.                     x = bf.getWidth() - radiu;  
  47.                 else {  
  48.                 }  
  49.             }  
  50.             // 如果y越界  
  51.             else if (y <= 0 || y + radiu >= bf.getHeight()) {  
  52.                 vy = -vy;// y速度反向  
  53.                 if (y < 0)  
  54.                     y = 0;  
  55.                 else if (y > bf.getHeight() - radiu)  
  56.                     y = bf.getHeight() - radiu;  
  57.                 else {  
  58.                 }  
  59.             } else {  
  60.             }  
  61.   
  62.             try {  
  63.                 Thread.sleep(10);// 设置睡眠时间为10ms  
  64.             } catch (InterruptedException e) {  
  65.                 e.printStackTrace();  
  66.             }  
  67.             // 重绘  
  68.             bf.repaint();  
  69.         }  
  70.     }  
  71.   
  72.     public Color getcolor() {  
  73.         return color;  
  74.     }  
  75.   
  76.     public void setcolor(Color color) {  
  77.         this.color = color;  
  78.     }  
  79.   
  80.     public int getX() {  
  81.         return x;  
  82.     }  
  83.   
  84.     public void setX(int x) {  
  85.         this.x = x;  
  86.     }  
  87.   
  88.     public int getY() {  
  89.         return y;  
  90.     }  
  91.   
  92.     public void setY(int y) {  
  93.         this.y = y;  
  94.     }  
  95.   
  96.     public int getRadiu() {  
  97.         return radiu;  
  98.     }  
  99.   
  100.     public void setRadiu(int radiu) {  
  101.         this.radiu = radiu;  
  102.     }  
  103.   
  104.     public int getVx() {  
  105.         return vx;  
  106.     }  
  107.   
  108.     public void setVx(int vx) {  
  109.         this.vx = vx;  
  110.     }  
  111.   
  112.     public int getVy() {  
  113.         return vy;  
  114.     }  
  115.   
  116.     public void setVy(int vy) {  
  117.         this.vy = vy;  
  118.     }  
  119.   
  120. }  

 

这样我们的小球就run起来了~~


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值