java线程第二节 多线程小球碰撞(粗糙)

该博客介绍了如何使用Java实现一个简单的多线程小球碰撞模拟。通过创建UI窗口,按钮触发线程,并在AddThread类中处理小球的运动和边界碰撞逻辑。代码利用Graphics对象绘制小球并实现了碰撞后的反弹效果。
摘要由CSDN通过智能技术生成

java线程第二节 多线程小球碰撞(粗糙)

UI

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;

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

public class UI extends JFrame{
	private int x=60,y=60,size=50;
	public static void main(String[] args) {
		new UI().initUI();
	}
	
	public void initUI() {
		this.setSize(800,600);
		this.setTitle("线程游戏");
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
		FlowLayout fly=new FlowLayout();
		this.setLayout(fly);
		
		JButton jbu=new JButton("开始");
		this.add(jbu);
		
	    this.setVisible(true);
	    ButtonListener Listener=new ButtonListener(this.getGraphics());
		jbu.addActionListener(Listener);
		
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		//g.setColor(Color.darkGray);
		//g.fillRect(0, 0, this.getWidth(), this.getHeight());
	}
}

ButtonListener

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonListener implements ActionListener{
	private Graphics g;
	private int x=60,y=60,size=50;
    public ButtonListener(Graphics g) {
		// TODO Auto-generated constructor stub
    	this.g=g;

	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		AddThread ad=new AddThread(g);//将循环放在线程中
		//使用ad.run(); 此时只是把run()当做主线程中的一个普通方法在用 和有没有添加线程没有任何关系
		ad.start();//启动线程
		//主线程在一直进行每一次点击会有一个新的执行通道(线程),且之间互不干扰,但是都基于当前的进程
		
	}
	

}

AddThread

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

public class AddThread extends Thread{
	   UI ui=new UI();
	private Graphics g;
	private int x,y=50,sizex=50,sizey=50;
	private int oldx,oldy,flag=0;
	private int a,b;
	Random ran = new Random();
	
	
	
	//右键 source /Generate Constructor using Fields可直接添加构造函数
	public AddThread(Graphics g) {
		super();
		this.g = g;
    	x=(int)(Math.random()*800);//800是随机数的范围
    	System.out.println("x="+x);
    	a=ran.nextInt(2);
    	if(a==0) {
    		sizex=sizex*-1;
    	}else {
    		sizex=sizex*1;
    	}
    	b=ran.nextInt(2);

    	x=50*ran.nextInt(16);
	}

    //继承线程类一定要重写run方法 这是启动线程时调用的方法
	@Override
	public void run() 
	{
		// TODO Auto-generated method stub
		for(int i=0;i<500;i++) //小球生存时间
		{
			try {
				Thread.sleep(80);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			//将上次画的小球变为画板的颜色
	      if(y<=600&&flag==1) //如果是&&y==600需要一直实现这不可能 反弹离开边界就会小于600
	      {
	    	   //System.out.println("下边框碰撞");
	    	   g.setColor(ui.getBackground());
			   g.fillOval(oldx, oldy, 50, 50);
					

		       g.setColor(Color.BLACK);
		       g.fillOval(x+=sizex, y-=50, 50, 50);	//只能反弹一次

		       oldx=x;
		       oldy=y;
		       
		       if(x==800) 
		       {
		    	   flag=2;
		    	   sizey=-1*sizey;
		       }else if(x==0) {
		    	   flag=3;
		    	   sizey=-1*sizey;
		       }
	      }
	      else if(y>=0&&flag==4)
	      {
	    	   //System.out.println("上边框碰撞");
	    	   g.setColor(ui.getBackground());
			   g.fillOval(oldx, oldy, 50, 50);
					

		       g.setColor(Color.BLACK);
		       g.fillOval(x+=sizex, y+=50, 50, 50);	//只能反弹一次

		       if(x==800) 
		       {
		    	   flag=2;
		    	   sizey=-1*sizey;
		       }else if(x==0) {
		    	   flag=3;
		    	   sizey=-1*sizey;
		       }
		       
		      
		       oldx=x;
		       oldy=y;
	      }
	      else if(x<=800&&flag==2) 
	      {
	    	   //System.out.println("右边框碰撞");
	    	   g.setColor(ui.getBackground());
			   g.fillOval(oldx, oldy, 50, 50);
					

		       g.setColor(Color.BLACK);
		       g.fillOval(x+=(sizex*-1), y+=sizey, 50, 50);	

		       if(y==600) {
		    	   flag=1;
		    	   System.out.println("flag="+1);
		    	   sizex=-1*sizex;
		       }else if(y==0){
		           flag=4;
		       sizex=-1*sizex;
		       }
		       
		       oldx=x;
		       oldy=y;
	      }
	      else if(x>=0&&flag==3) 
	      {
	    	  System.out.println("左边框碰撞");
	    	   g.setColor(ui.getBackground());
			   g.fillOval(oldx, oldy, 50, 50);
					

		       g.setColor(Color.BLACK);
		       g.fillOval(x+=(sizex*-1), y+=sizey, 50, 50);	

		       if(y==600) {
		    	   flag=1;
		    	   System.out.println("flag="+1);
		    	   sizex=-1*sizex;
		       }else if(y==0){
		           flag=4;
		       sizex=-1*sizex;
		       }
		       
		       oldx=x;
		       oldy=y;
	      }
	    	  
	      //未碰撞
	      else if(flag==0)
	      {
	       //System.out.println("冲冲冲");
		   g.setColor(ui.getBackground());
		   g.fillOval(oldx, oldy, 50, 50);
		   System.out.println("x="+x+"  y="+y);	

		   if(sizex<0&&sizey<0) {
			   System.out.println("sizex="+sizex+"  sizey="+sizey);
			   return;
		   }
		   else if(x<800&&x>0&&y<600&&y>0){
	       g.setColor(Color.BLACK);
	       g.fillOval(x+=sizex, y+=50, 50, 50);	
	      // g.fillOval(x, y+=50, 50, 50);
		   
	       oldx=x;
	       oldy=y;
	       System.out.println("sizex="+sizex+"  sizey="+sizey);
	       System.out.println("flag="+flag);
	       System.out.println("x="+x+"  y="+y);
	       if(x==800||x==0) 
	       {
	    	   flag=2;
	    	   System.out.println("flag="+flag);
	       }
	       else if(y==600||y==0)
	       {
	    	   flag=1;
	    	   System.out.println("flag="+flag);
	       }
		   
		   }

	       


		   }
		
	   }
	
   }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值