JAVA 多彩小球的碰撞

//老师代码
//
public class Ball {
	private double x;
	private double y;
	
	private static final double X_SIZE = 20;
	private static final double Y_SIZE = 20;
	
	private double xMoveDis;
	private double yMoveDis;
	
	private Color color;
	private Ellipse2D shape;
	
	private static Random random;
	static {
		random = new Random();
	}
	
	public Ball() {
		color = new Color(random.nextInt(256)
				,random.nextInt(256),random.nextInt(256));
		xMoveDis = 5 + random.nextInt(5)-2;
		yMoveDis = 5 + random.nextInt(5)-2;
	}
	
	public void move(Rectangle rect) {
		x += xMoveDis;
		y += yMoveDis;
		//判断边界问题
		if (x < rect.getMinX()) {
			x = rect.getMinX();
			xMoveDis = -xMoveDis;
		} else if ((x+X_SIZE) > rect.getMaxX()) {
			x = rect.getMaxX()-X_SIZE;
			xMoveDis = -xMoveDis;
		}
		
		if (y < rect.getMinY()) {
			y = rect.getMinY();
			yMoveDis = -yMoveDis;
		} else if ((y+Y_SIZE) > rect.getMaxY()) {
			y = rect.getMaxY()-Y_SIZE;
			yMoveDis = -yMoveDis;
		}
	}
	
	public Shape getShape() {
		if (shape == null) {
			shape = new Ellipse2D.Double(x, y, X_SIZE, Y_SIZE);
		} else {
			shape.setFrame(x, y, X_SIZE, Y_SIZE);
		}
		return shape;
	}

	public Color getColor() {
		return color;
	}
}

public class BallFrame extends JFrame {
	private JButton startBtn;
	private JButton endBtn;
	private BallPanel ballPanel;
	
	public BallFrame() {
		ballPanel = new BallPanel();
		this.add(ballPanel, BorderLayout.CENTER);
		JPanel btnPanel = new JPanel();
		startBtn = new JButton("start");
		btnPanel.add(startBtn);
		endBtn = new JButton("end");
		btnPanel.add(endBtn);
		this.add(btnPanel, BorderLayout.SOUTH);
		actionHandler();
		showMe();
	}
	
	private void actionHandler() {
		startBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.out.println("startBtn pressed");
				//1.创建小球
					Ball ball = new Ball();
				//2.启动小球线程
					new BallThread(ball, ballPanel.getBounds()).start();
				//3.存入BallPanel
					ballPanel.addBall(ball);
					
			}
		});
		
		endBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});
	}
	
	private void showMe() {
		this.setTitle("小球碰撞");
		this.setSize(400,300);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		double width = toolkit.getScreenSize().getWidth();
		double height = toolkit.getScreenSize().getHeight();
		this.setLocation((int)(width-this.getWidth())/2
				,(int)(height-this.getHeight())/2);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}
///
public class BallPanel extends JPanel{
	private List<Ball> balls;
	
	public BallPanel() {
		balls = new ArrayList<Ball>();
		startPaintThread();
	}
	
	private void startPaintThread() {
		new Thread(){
			public void run() {
				while(true) {
//					System.out.println("repaint");
					repaint();
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		for (Ball ball : balls) {
			g2.setColor(ball.getColor());
			g2.fill(ball.getShape());
		}
	}

	public void addBall(Ball ball) {
		balls.add(ball);
	}
}
/
public class BallThread extends Thread {
	private Ball ball;
	private Rectangle rect;
	
	public BallThread(Ball ball, Rectangle rect) {
		this.ball = ball;
		this.rect = rect;
	}

	@Override
	public void run() {
		int count = 0;
		while (count < 10000) {
			ball.move(rect);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			count++;
		}
	}
}

public class BallMain {
	public static void main(String[] args) {
		new BallFrame();
	}
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值