rollenholt的博文:http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html

弹球例子:

0. 创建Bounce框架 JFrame frame = new BounceFrame();
    BounceFrame自定义了addButton、addBall方法,用于在frame内的panel添加按钮、小球

1. 设置标题 setTitle("bounce");
2. 创建BallComponent comp = new Ballcomponent();
  BallComponent继承于JPanel。专门用于容纳并绘制Ball
3. 将comp添加到框架frame

4. 创建JPanel buttonPanel = new JPanel();用于存放start、close按钮
5. 创建start、close按钮,并添加到buttonPanel中
6. 将buttonPanel添加到框架frame

代码:

Bounce.java 

public class Bounce {
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
} class BallRunnable implements Runnable{
private Ball ball;
private Component component;
public static final int STEPS =2000;
public static final int DELAY = 5 ; public BallRunnable(Ball ball , Component component){
this.ball=ball;
this.component = component;
} public void run(){
try {
for(int i = 0 ; i < STEPS ; i++){
ball.move(component.getBounds()); //移动小球
//repaint会激活component的paintComponent方法
component.repaint();
Thread.sleep(DELAY);
}
}catch(InterruptedException e){
//处理异常
}
}
} class BounceFrame extends JFrame{
private BallComponent comp; public BounceFrame(){
setTitle("Bounce");
comp = new BallComponent();
add(comp,BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel,"Start",new ActionListener(){
public void actionPerformed(ActionEvent e){
addBall();
}
}); addButton(buttonPanel , "Close" , new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
add(buttonPanel,BorderLayout.SOUTH);
pack();
} public void addButton(Container c, String title , ActionListener listener){
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
} public void addBall(){
try{
Ball ball = new Ball();
comp.add(ball);
Runnable r = new BallRunnable(ball,comp);
Thread t = new Thread(r);
t.start(); }catch(Exception e){
//处理异常
}
} }

  

Ball.java

package com.thread.Multithreadball;

import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D; public class Ball {
private static final int XSIZE = 15;
private static final int YSIZE = 15;
private double x = 0;
private double y = 0;
private double dx = 1;
private double dy = 1; public void move(Rectangle2D bounds){
x += dx;
y += dy;
if(x<bounds.getMinX()){
x=bounds.getMinX();
dx=-dx;
} if(x + XSIZE >= bounds.getMaxX()){
x=bounds.getMaxX()-XSIZE;
dx=-dx;
} if(y<bounds.getMinY()){
y=bounds.getMinY();
dy=-dy;
} if(y + YSIZE >= bounds.getMaxY()){
y = bounds.getMaxY() - YSIZE;
dy=-dy;
}
} //获取当前的小球
public Ellipse2D getShape(){
return new Ellipse2D.Double(x,y,XSIZE,YSIZE);
}
}

BallComponent.java

public class BallComponent extends JPanel{
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 350; private List<Ball> balls = new ArrayList<>(); //将一个球加入到component
public void add(Ball b ){
balls.add(b);
} public void paintComponent (Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for(Ball b : balls){
g2.fill(b.getShape());
}
} public Dimension getPreferredSize(){
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}

  

Java学习笔记--多线程的更多相关文章

  1. 0037 Java学习笔记-多线程-同步代码块、同步方法、同步锁

    什么是同步 在上一篇0036 Java学习笔记-多线程-创建线程的三种方式示例代码中,实现Runnable创建多条线程,输出中的结果中会有错误,比如一张票卖了两次,有的票没卖的情况,因为线程对象被多条 ...

  2. Java学习笔记-多线程-创建线程的方式

    创建线程 创建线程的方式: 继承java.lang.Thread 实现java.lang.Runnable接口 所有的线程对象都是Thead及其子类的实例 每个线程完成一定的任务,其实就是一段顺序执行 ...

  3. java学习笔记 --- 多线程(多线程的创建方式)

    1.创建多线程方式1——继承Thread类. 步骤:  A:自定义类MyThread继承Thread类.  B:MyThread类里面重写run()? 为什么是run()方法呢? C:创建对象 D:启 ...

  4. 0040 Java学习笔记-多线程-线程run()方法中的异常

    run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...

  5. 0039 Java学习笔记-多线程-线程控制、线程组

    join线程 假如A线程要B线程去完成一项任务,在B线程完成返回之前,不进行下一步执行,那么就可以调用B线程的join()方法 join()方法的重载: join():等待不限时间 join(long ...

  6. JAVA学习笔记 -- 多线程之共享资源

    在多线程程序执行过程中,可能会涉及到两个或者多个线程试图同一时候訪问同一个资源.为了防止这样的情况的发生,必须在线程使用共享资源时给资源"上锁",以阻挡其他线程的訪问. 而这样的机 ...

  7. 0041 Java学习笔记-多线程-线程池、ForkJoinPool、ThreadLocal

    什么是线程池 创建线程,因为涉及到跟操作系统交互,比较耗费资源.如果要创建大量的线程,而每个线程的生存期又很短,这时候就应该使用线程池了,就像数据库的连接池一样,预先开启一定数量的线程,有任务了就将任 ...

  8. 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题

    调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...

  9. 0036 Java学习笔记-多线程-创建线程的三种方式

    创建线程 创建线程的三种方式: 继承java.lang.Thread 实现java.lang.Runnable接口 实现java.util.concurrent.Callable接口 所有的线程对象都 ...

随机推荐

  1. 使用GCD的dispatch_once创建单例

    使用GCD的dispatch_once创建单例 介绍了创建单例的方式,不过后来发现其实在ios 4.0后有更加简单的方式. 就是使用GCD的功能 代码如下: + (instantClass *)sha ...

  2. Caffe : Layer Catalogue(1)

    原文:http://caffe.berkeleyvision.org/tutorial/layers.html 参考:http://blog.csdn.net/u011762313/article/d ...

  3. cf B. Permutation

    #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ]; ...

  4. LeetCode_Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. C8051F学习笔记:单片机的驱动能力

    学习51单片机的时候我们就知道51单片机的I/O口的特点:P0口没有弱上拉,所以做地址线时不用上拉,但输出“1”时就要加上拉电阻,不然输出电平到不了高电平,P1~P3则不存在这个问题,每个输出管脚都有 ...

  6. 绝美Sysinternals

    啥也不说了,自己看吧: https://technet.microsoft.com/en-us/sysinternals/bb545046 新地址: https://technet.microsoft ...

  7. SQL判断一个数是整数还是小数

    DECLARE @number1 AS numeric(10,2),@number2 AS numeric(10,2) SELECT @number1=10.00,@number2=10.2 SELE ...

  8. Json帮助类以及如何使用

    首先要添加引用System.Runtime.Serialization. public class JSONHelper { public static string Serialize<T&g ...

  9. Codeforce 218 div2

    D 一开始想错了,试图用"前缀和-容量"来求从上层流下来了多少水",但这是错的,因为溢出可能发生在中间. 然后发现对于每层,溢出事件只会发生一次,所以可以用类似并查集的办 ...

  10. wpf新增记录时用多线程的问题

    多线程虽然可以增加用户操作体验,但是有时候会出现意想不到的错误. 如果采用分布式,数据库在另外服务器上,当网络出现问题,或者数据库繁忙,那么新增数据就会等待,这时候用户如果以为没有操作,而多次点击新增 ...