`

“飞来飞去”的小球

 
阅读更多

     今天学习了多线程,总体来说,先从进程入手。

     什么是进程呢? 简单来说就是正在运行的程序,例如打开资源管理器,你将看到里面的进程,有些是你运行的程序创建出来的,有些是后台运行的程序创建的。

     什么又是线程呢?就是独立的运行单位。一个进程至少有一个线程。进程是程序的一次执行,那么线程可以理解为进程中执行的一段程序片段。

      而我们在学习线程之前,所写的程序都是单线程的,那么编译器便是逐行扫描代码并逐行执行。尽管所学的线程不是那么好控制,在精确度要求极高的行业很难执行,如金融什么的,但是它能够让资源最大化的利用起来,提升应用程序的效果。

 

 

    这一次的课后练习便是做小球,要求是:
                 1、小球不能超出边框
                 2、小球相互碰撞,进行反弹

package 线程;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;

public class BallMove extends JFrame{
	
	private static final Graphics g = null;

	public static void main(String [] args){
		BallMove ball = new BallMove();
		ball.initUI();
	}
	
	private void initUI() {
		
		this.setTitle("小球移动");
		this.setSize(800,500);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
		this.setResizable(false);
		
		/*这个地方是打算添加一个按钮,并加上监听器,按钮每按一下就增加一个小球,
		目前功能还未实现*/
//		this.setLayout(null);
//		JButton but = new JButton("添加");
//		but.setBounds(20, 20, 80, 30);
//		this.add(but);
		
		this.setVisible(true);
		
		Graphics g = this.getGraphics();
		ball1.start();
		ball2.start();
//		ball3.start();
//		ball4.start();
		
	}

/*	事实上这里可以不必每添加一个小球就要实例化一个,可以修改为数组*/

	Ball ball1 = new Ball(0,20,50,this,g);
	Ball ball2 = new Ball(100,180,50,this,g);
//	Ball ball3 = new Ball(Color.pink,300,400,50,50,this,g);
//	Ball ball4 = new Ball(Color.YELLOW,200,30,70,70,this,g);

	/*这里实现的是画布的重绘*/
	public void paint(Graphics g){
		super.paint(g);
		Random random = new Random();//随机
		Color color=new Color(random.nextInt(255),
random.nextInt(255),random.nextInt(255));//颜色
		g.setColor(color);
		g.fillOval(ball1.getX(), ball1.getY(), ball1.getR(), ball1.getR());
		g.setColor(color.BLACK);
		g.fillOval(ball2.getX(), ball2.getY(), ball2.getR(), ball2.getR());
		if(Math.sqrt((ball1.getX()-ball2.getX())*(ball1.getX()-ball2.getX())+
				(ball1.getY()-ball2.getY())*(ball1.getY()-ball2.getY())) <= ball1.getR()){
			int tempxdirection = ball2.getXdirection();
//			int tempydirection = ball2.getXdirection();
//			int tempxspeed = ball2.getXspeed();
//			int tempyspeed = ball2.getYspeed();
			
/*这里发生了很神奇的事情,我本来的想法是当两个小球相撞时,两个小球应该交换运动方向,那么x、y的方向都应该改变,可是都改变后两小球便会平行行走,而只改变小球的x方向,则可以实现我想要达到的效果,这个地方我就没想明白!!!*/
			ball2.setXdirection(ball1.getXdirection());
//			ball2.setYdirection(ball1.getYdirection());
//			ball2.setXspeed(ball1.getXspeed());
//			ball2.setYspeed(ball1.getYspeed());
			ball1.setXdirection(tempxdirection);
//			ball1.setYdirection(tempydirection);
//			ball1.setXspeed(tempxspeed);
//			ball1.setYspeed(tempyspeed);
			System.out.println("方向交换了.....");
		}
			
		
//		g.setColor(ball3.getC());
//		g.fillOval(ball3.getX(), ball3.getY(), ball3.getWidth(), ball3.getHeight());
//		g.setColor(ball4.getC());
//		g.fillOval(ball4.getX(), ball4.getY(), ball4.getWidth(), ball4.getHeight());
	}

}

 

package 线程;

import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;

public class Ball extends Thread{
	private Ball ball;//表示当前小球
	private JFrame jf;//声明一个窗体
	public Ball getBall() {
		return ball;
	}

	public void setBall(Ball ball) {
		this.ball = ball;
	}

	private Graphics g;//声明一个画板
	private int x,y;//小球的初始位置x、y
	private int r;//小球的半径
	//用四个整型数据表示四个方向
	int NORTH = 1;
	int SOURTH = 2;
	int WEST = 3;
	int EAST = 4;
	//创建一个随机数对象
	Random random = new Random();//随机
	private int xspeed,yspeed;//定义x、y方向上的速度
	private int xdirection = WEST,ydirection = NORTH;//定义x、y的方向
	
	
	public Ball(int x,int y,int r,JFrame jf,Graphics g){
		this.x = x;
		this.y = y;
		this.jf = jf;
		this.g = g;
		this.r = r;
		this.ball = ball;
	}
	
	public void run(){
		while(true){
			try {
				Thread.sleep(30);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			onHitwall();
			move();
			
			jf.repaint();
		}
	}
	
	public void move(){
		xspeed = 5+random.nextInt(10);//x方向速度
	    yspeed = 5+random.nextInt(10);//y方向速度
		
	    switch(xdirection){//对于x方向
        case 3:x = x - xspeed;break;//如果小球的运动方向向西,x值减小
        case 4:x = x + xspeed;break;//如果小球的运动方向向东,x值增大
        }

        switch(ydirection){//对于y方向
        case 1:y = y - yspeed;break;//如果小球的运动方向向北,y值减小
        case 2:y = y + yspeed;break;//如果小球的运动方向向难,y值增大
        }
	}

	public void onHitwall(){
		//如果碰到边界,方向改变
        if(x>jf.getWidth()-r){
            xdirection = 3;
        }
        
        if(x<0){
            xdirection = 4;
        }
        
         if(y>jf.getHeight()-r){
       	 ydirection = 1;
        }
         
        if(y<0+r/2){
            ydirection = 2;
        }
	}
	
//	public void onHitball(){
//		if(this-ball.getX()){
//			
//		}
//	}
	
	public int getXspeed() {
		return xspeed;
	}

	public void setXspeed(int xspeed) {
		this.xspeed = xspeed;
	}

	public int getYspeed() {
		return yspeed;
	}

	public void setYspeed(int yspeed) {
		this.yspeed = yspeed;
	}

	public int getXdirection() {
		return xdirection;
	}

	public void setXdirection(int xdirection) {
		this.xdirection = xdirection;
	}

	public int getYdirection() {
		return ydirection;
	}

	public void setYdirection(int ydirection) {
		this.ydirection = ydirection;
	}

	public JFrame getJf() {
		return jf;
	}

	public void setJf(JFrame jf) {
		this.jf = jf;
	}

	public Graphics getG() {
		return g;
	}

	public void setG(Graphics g) {
		this.g = g;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getR() {
		return r;
	}

	public void setR(int r) {
		this.r = r;
	}

}

 

 

  • 大小: 64.6 KB
3
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics