class="java" name="code">个人对于多线程这个概念还是比较陌生的,但是大体上已经感到线程应该也必须是java中的重
点,以前就对一边听歌一边浏览网页等等同时用电脑做很多事感到奇怪,现在终于明白了。现
在对线程还不是很了解,但是应该可以这样定义抢占cup之战,谁先抢到cup资源,谁就先运
行。线程有一个系统给的类Thread,所以可以直接继承这个类,然后重写run()方法,最后
再通过对象调用start()方法
?
????? 给个实例吧:一个小球从上面落下如果掉到通过鼠标控制的一个木板上就回弹起来,但弹起的
高度减小,如此反复;直到没有动力了了,下面是代码
package com.lw20130715;
import java.awt.Graphics;
import javax.swing.JFrame;
/**
 * 创建一个弹球的界面
 * @author Administrator
 *
 */
public class JugleBall extends JFrame{
	//创建界面的显示方法
	private Graphics g;
	
	public void showUI(){
		this.setTitle("弹跳的小球");
		this.setSize(500,500);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
		this.setVisible(true);
		g = this.getGraphics();
		MouseMotion mm = new MouseMotion(g,this);
		this.addMouseMotionListener(mm);
		BallThread go = new BallThread(g,mm,this,235);
//		BallThread go02 = new BallThread(g,mm,this,200);
		go.start();
//		go02.start();
	}
	//c创建窗体的重绘的方法
	public void repaint(){
		super.paint(g);
	}
}
?上面是小球运动的界面
package com.lw20130715;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class MouseMotion implements MouseMotionListener{
	private int x;
	private int y;
	private Graphics g;
	private JugleBall jugleBall;
	public MouseMotion(Graphics g, JugleBall jugleBall) {
		// TODO Auto-generated constructor stub
		this.g = g;
		this.jugleBall = jugleBall;
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public int getX1(){
		return this.x;
	
	}
	public int getY1(){
	
		return this.y;
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		x = e.getX();
		y = e.getY();
		this.jugleBall.repaint();
		if(450>=x&&x>=50){
			g.fillRect(x-50, 485, 100, 15);
		}else if(x<50){
			g.fillRect(0, 485, 100, 15);
		}else if(x>=450){
			g.fillRect(400, 485, 100, 15);
		}
	}
}
?
上面的是鼠标监听器,主要是控制下面的滑竿的,以及提供鼠标坐标
package com.lw20130715;
import java.awt.Graphics;
import javax.swing.JFrame;
/**
 * 创建一个弹球的界面
 * @author Administrator
 *
 */
public class JugleBall extends JFrame{
	//创建界面的显示方法
	private Graphics g;
	
	public void showUI(){
		this.setTitle("弹跳的小球");
		this.setSize(500,500);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
		this.setVisible(true);
		g = this.getGraphics();
		MouseMotion mm = new MouseMotion(g,this);
		this.addMouseMotionListener(mm);
		BallThread go = new BallThread(g,mm,this,235);
//		BallThread go02 = new BallThread(g,mm,this,200);
		go.start();
//		go02.start();
	}
	//c创建窗体的重绘的方法
	public void repaint(){
		super.paint(g);
	}
}
?上面是一个线程类
最后就是主程序入口类了,代码如下
package com.lw20130715;
/**
 * 创建程序的主入口类
 * @author Administrator
 *
 */
public class Manager {
	
	public static void main(String[]args){
		
		JugleBall ball = new JugleBall();
		ball.showUI();
		
	}
}
?
最后附上一张程序运行的图片
?
?
?