Java学习笔记--多线程
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学习笔记--多线程的更多相关文章
- 0037 Java学习笔记-多线程-同步代码块、同步方法、同步锁
什么是同步 在上一篇0036 Java学习笔记-多线程-创建线程的三种方式示例代码中,实现Runnable创建多条线程,输出中的结果中会有错误,比如一张票卖了两次,有的票没卖的情况,因为线程对象被多条 ...
- Java学习笔记-多线程-创建线程的方式
创建线程 创建线程的方式: 继承java.lang.Thread 实现java.lang.Runnable接口 所有的线程对象都是Thead及其子类的实例 每个线程完成一定的任务,其实就是一段顺序执行 ...
- java学习笔记 --- 多线程(多线程的创建方式)
1.创建多线程方式1——继承Thread类. 步骤: A:自定义类MyThread继承Thread类. B:MyThread类里面重写run()? 为什么是run()方法呢? C:创建对象 D:启 ...
- 0040 Java学习笔记-多线程-线程run()方法中的异常
run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...
- 0039 Java学习笔记-多线程-线程控制、线程组
join线程 假如A线程要B线程去完成一项任务,在B线程完成返回之前,不进行下一步执行,那么就可以调用B线程的join()方法 join()方法的重载: join():等待不限时间 join(long ...
- JAVA学习笔记 -- 多线程之共享资源
在多线程程序执行过程中,可能会涉及到两个或者多个线程试图同一时候訪问同一个资源.为了防止这样的情况的发生,必须在线程使用共享资源时给资源"上锁",以阻挡其他线程的訪问. 而这样的机 ...
- 0041 Java学习笔记-多线程-线程池、ForkJoinPool、ThreadLocal
什么是线程池 创建线程,因为涉及到跟操作系统交互,比较耗费资源.如果要创建大量的线程,而每个线程的生存期又很短,这时候就应该使用线程池了,就像数据库的连接池一样,预先开启一定数量的线程,有任务了就将任 ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- 0036 Java学习笔记-多线程-创建线程的三种方式
创建线程 创建线程的三种方式: 继承java.lang.Thread 实现java.lang.Runnable接口 实现java.util.concurrent.Callable接口 所有的线程对象都 ...
随机推荐
- Scala学习笔记--Actor和并发
感谢博主lyrebing 博文地址:http://blog.csdn.net/lyrebing/article/details/20446061 1. Actor用法 1.1 Actor的基本使用 ...
- Codeforces 533B Work Group
http://codeforces.com/problemset/problem/533/B 题目大意: 每个人有一个直接的领导,1是总裁,现在要找一个最大的集合,每个领导领导的人的数量都是偶数,问最 ...
- Unity3D中C#编写脚本
1.继承MonoBehaviour类:任何一个游戏脚本都需要去继承MonoBehaviour这个类,只是在创建javascript脚本的时候,系统会将其类名与继承关系隐藏起来. 2.声明变量:使用Ja ...
- C#中使用SendMessage进行进程通信的实例
原文:C#中使用SendMessage进行进程通信的实例 1 新建解决方案SendMessageSecondExample 在解决方案下面新建两个项目:Sender和Receiver,两者的输出类型均 ...
- scriptol图像处理算法
神奇的图像处理算法 相似图片搜索是利用数学算法,进行高难度图像处理的一个例子.事实上,图像处理的数学算法,已经发展到令人叹为观止的地步. Scriptol列出了几种神奇的图像处理算法,让我们一起来 ...
- 特征提取(Detect)、特征描述(Descriptor)、特征匹配(Match)的通俗解释
特征匹配(Feature Match)是计算机视觉中很多应用的基础,比如说图像配准,摄像机跟踪,三维重建,物体识别,人脸识别,所以花一些时间去深入理解这个概念是不为过的.本文希望通过一种通俗易懂的方式 ...
- tyvj1038忠诚
描述 Description 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意.但是由于一些人的挑拨, ...
- html5上传本地图片,在线预览及裁剪(filereader,canvas)
1 我们常常需要上传头像,点击上传按钮时候需要预览一下,使用filereader方法无需和后台交互,代码如下: //本地图片在上传之前的预览效果 //图片上传预览 function previewIm ...
- android分享到新浪微博,认证+发送微博,
分享到新浪微博,折腾了大半个月,现在终于弄出来了,心里的那个爽呀,太痛快了,哈哈!! 废话少说,首先是认证, 1.进入新浪微博提供的开放平台http://open.weibo.com/ 注册新浪账号. ...
- How to face setbacks
I’ve been in a bad mood since I started on the American Accent. I became even more upset when I adde ...