java实现小球碰撞功能
作者:mayifan_blog 发布时间:2023-04-05 19:22:41
本文实例为大家分享了java实现小球碰撞的具体代码,供大家参考,具体内容如下
这次我们做一个小球的碰撞的游戏,规则是:按下添加按钮,窗口的中心部分会产生一个小球(刚开始默认为黑色),四个方向随机产生,发射小球,再次按下即产生两个小球。当小球碰到窗体边缘的时候会产生反弹,当两个小球接触时会产生碰撞,双方交换速度,向相反方向移动。我们可以选择相应的颜色来改变下一个发射的小球颜色。当按下清除可以清除屏幕上的小球,当按下添加则会继续产生小球。最后我们还添加了自动产生小球的功能,按下开关,在屏幕中间会定时产生小球。接下来,我们来展示代码部分。
public class Jframe {
private Ball[] arrayball = new Ball[100];
public static void main(String[] args) {
Jframe frame = new Jframe();
frame.showUI();
}
public void showUI() {
javax.swing.JFrame jf = new javax.swing.JFrame();
jf.setSize(1000, 1000);
jf.getContentPane().setBackground(Color.WHITE);
jf.setTitle("小球");
jf.setDefaultCloseOperation(3);
// 设置居中显示
jf.setLocationRelativeTo(null);
JPanel jp1 =new JPanel();
JButton jb1 = new JButton("添加");
jp1.add(jb1);
// jb1.setBounds(100,50, 40, 20);
JButton jb2 = new JButton("暂停");
jp1.add(jb2);
// jb1.setBounds(200,50, 40, 20);
JButton jb3 = new JButton("清除");
jp1.add(jb3);
// jb1.setBounds(300,50, 40, 20);
JButton jb4 = new JButton("自动添加");
jp1.add(jb4);
jf.add(jp1,BorderLayout.NORTH);
Mouse mouse = new Mouse();
Color[] color = {Color.RED,Color.BLUE,Color.BLACK,Color.GREEN,Color.YELLOW};
for(int i=0;i<color.length;i++){
JButton jbu = new JButton();
jbu.setBackground(color[i]);
jbu.setPreferredSize(new Dimension(30, 30));
jp1.add(jbu);
jbu.addActionListener(mouse);
}
jb1.addActionListener(mouse);
jb2.addActionListener(mouse);
jb3.addActionListener(mouse);
jb4.addActionListener(mouse);
jf.addMouseListener(mouse);
jf.addMouseMotionListener(mouse);
BallJpanel cp = new BallJpanel();
cp.setBackground(Color.WHITE);
jf.add(cp,BorderLayout.CENTER);
jf.setVisible(true);
Graphics g = cp.getGraphics();
mouse.setcp(cp);
mouse.setg(g);
mouse.setarrayball(arrayball);
mouse.setmouse(mouse);
cp.setarrayball(arrayball);
}
}
这是窗体的基本配置,采用边框布局,上方放置按钮,中间是画布。我们为按钮添加了动作 * ,并使用了一系列的方法来把对象传递到其他类中。
public class Ball {
public int size = 90; // 小球的直径
public int x = 500; // 小球所在的x坐标
public int y = 500; // 小球所在的y坐标
public int vx = 5;
public int vy = 5;
public BallJpanel cp;
public Color color = Color.BLACK;
public int max_x, max_y, Min_x, Min_y;
private Ball[] arrayball;
public void setcp(BallJpanel cp) {
this.cp = cp;
}
public void setarrayball(Ball[] arrayball) {
this.arrayball = arrayball;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int setY() {
return y;
}
public Ball(int x, int y, int vx, int vy, Color color) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.color = color;
}
public void ballMove(Graphics g) {
x += vx;
y += vy;
max_y = cp.getHeight();
max_x = cp.getWidth();
if (x <= size / 2) {
x = size / 2;
vx = -vx;
}
if (y <= size / 2) {
y = size / 2;
vy = -vy;
}
if (x + size / 2 >= max_x) {
x = max_x - size / 2;
vx = -vx;
}
if (y + size / 2 >= max_y) {
y = max_y - size / 2;
vy = -vy;
}
for (int i = 0; i < arrayball.length; i++)
{
if (arrayball[i] == null)
break;
Ball ball = arrayball[i];
if (this.equals(ball))
continue;
if ((ball.x - this.x) * (ball.x - this.x) + (ball.y - this.y) * (ball.y - this.y) <= size * size)
{
int tempvx = this.vx;
int tempvy = this.vy;
this.vx = ball.vx;
this.vy = ball.vy;
ball.vx = tempvx;
ball.vy = tempvy;
while ((ball.x - this.x) * (ball.x - this.x) + (ball.y - this.y) * (ball.y - this.y) <= size * size)
{
this.x += this.vx;
this.y += this.vy;
System.out.println("等待");
}
}
}
}
}
考虑到这是一个小球的运动系统,我们为小球写了一个类,添加小球的时候,会创建小球对象,并使其获得位置,颜色,速度等参数,并将其存入数组。小球的方法就是运动,每当执行ballMove方法,便会为小球修改位置坐标(基于其速度),再判断是否撞击边框,以及判断是否和别的小球有坐标重叠,如果有重叠,则跑一个循环,修改位置坐标,使其分离。Ball这部分代码和 * 中的方法有所联系,我们接下来介绍 * 的方法。
public class Mouse implements MouseMotionListener, MouseListener, ActionListener {
private Graphics g;
private BallJpanel cp;
private Ball[] arrayball;
private int index = 0;
private int x;
private int y;
private int vx;
private int vy;
private int random=1;
private Color color=Color.black;
private ThreadBall tb;
private Mouse mouse;
public int selfFlag=0;
public void setmouse(Mouse mouse)
{
this.mouse= mouse;
}
public void setarrayball(Ball[] arrayball) {
this.arrayball = arrayball;
}
public void setg(Graphics g) {
this.g = g;
}
public void setcp(BallJpanel cp) {
this.cp = cp;
}
public void actionPerformed(ActionEvent e) {
if ("添加".equals(e.getActionCommand())) {
System.out.println("添加");
if (tb == null) {
// 创建线程对象
tb = new ThreadBall();
tb.setcp(cp);
tb.setarrayball(arrayball);
tb.setg(g);
tb.start();
tb.setmouse(mouse);
}
tb.stopFlag=0;
addBall();
}
if ("暂停".equals(e.getActionCommand())) {
if(tb!=null)
{
if(tb.stopFlag==0)
{
tb.stopFlag=1;
System.out.println("暂停");
}
else
{
tb.stopFlag=0;
System.out.println("开始");
}
}
}
if ("清除".equals(e.getActionCommand())) {
tb.stopFlag=1;
cp.paint1(g);
index=0;
System.out.println("清除");
}
if ("自动添加".equals(e.getActionCommand())){
if(selfFlag==0)
{selfFlag=1;System.out.println("自动添加打开");}
else
{selfFlag=0;System.out.println("自动添加关闭");}
}
if("".equals(e.getActionCommand())){
JButton jbu=(JButton)e.getSource();
color=jbu.getBackground();
g.setColor(color);
}
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void addBall() {
x = 500;
y = 500;
random=1+(int)(Math.random()*4);
switch(random)
{
case 1:
vx=5;
vy=5;
break;
case 2:
vx=-5;
vy=-5;
break;
case 3:
vx=5;
vy=-5;
break;
case 4:
vx=-5;
vy=5;
break;
}
Ball ball = new Ball(x, y,vx , vy, color);
arrayball[index++] = ball;
}
}
* 中,我们设置了一系列参数来控制一些方法的开启和关闭,以及写了添加小球的方法,为其赋初值,随机一个初始发射方向。这段代码我们用到了线程。线程的使用分为两步,创建线程对象并start线程。
public class ThreadBall extends Thread {
private Graphics g;
private BallJpanel cp;
private Ball[] arrayball;
public int stopFlag=0;
private int add=0;
private Mouse mouse;
public void setmouse(Mouse mouse)
{
this.mouse=mouse;
}
public void setcp(BallJpanel cp) {
this.cp = cp;
}
public void setg(Graphics g)
{
this.g=g;
}
public void setarrayball(Ball[] arrayball) {
this.arrayball = arrayball;
}
/**
* 启动线程执行的方法
*/
public void run() {
while (true) {
if(stopFlag==0)
{
for (int i = 0; i < arrayball.length; i++)
{
if(arrayball[i]==null)
break;
Ball ball = arrayball[i];
ball.setarrayball(arrayball);
ball.setcp(cp);
ball.ballMove(g);
}
cp.paint(g);
add++;
if(add==5000)
add=0;
if(add%50==0&&mouse.selfFlag==1)
mouse.addBall();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
以上是线程的属性和方法,此类继承Thread并重写了run方法。run方法的思路是循环调用ballMove方法修改小球坐标,并调用paint方法更新显示,我们加入了一个延时函数,来控制调用的频率。
public class BallJpanel extends JPanel {
private Ball[] arrayball;
public void setarrayball(Ball[] arrayball)
{
this.arrayball=arrayball;
}
public void paint(Graphics g)
{
super.paint(g);
for(int i=0;i<arrayball.length;i++)
{
if(arrayball[i]==null)
{
break;
}
Ball ball=arrayball[i];
g.setColor(ball.color);
g.fillOval(ball.x-ball.size/2, ball.y-ball.size/2, ball.size, ball.size);
}
}
public void paint1(Graphics g)
{
super.paint(g);
for(int i=0;i<arrayball.length;i++)
{
if(arrayball[i]==null)
{
break;
}
arrayball[i]=null;
}
}
}
BallJpanel类写的是画布,及小球的运动区域,画笔也是从其对象cp上获得。类里用paint写画面的重绘方法(包括画板小球的重绘),paint1用来清空画布及数组。
以上便是java小球运动的全部代码,我们来看一下效果。
来源:https://blog.csdn.net/mayifan_blog/article/details/83663138


猜你喜欢
- 一、认识AdapterViewFilpper AdapterViewFilpper 继承 了Adapte
- 条件:1、android:ellipsize=”marquee”2、TextView必须单行显示,即内容必须超出TextView
- 冒泡排序原理:从头(左边)开始比较每一对相邻的元素,如果第1个比第2个大,就交换它们的位置,执行完一轮后,最末尾(最右边)就是最大的元素。举
- SpringBoot 如何进行参数校验在日常的接口开发中,为了防止非法参数对业务造成影响,经常需要对接口的参数做校验,例如登录的时候需要校验
- 最近做一个C#项目,需要对radis进行读写。首先引入System.Configuration,如下实现代码如下:public class
- 智能终端上的游戏目前风头正劲,试问哪个智能手机上没有几款企鹅公司出品的游戏呢!之前从未涉猎过游戏开发,但知道游戏开发前要挑选一款合适的游戏引
- 方法一:通过Theme.Translucent@android:style/Theme.Translucent@android:style/
- progressDialog, 它有两个方法dialog.cancel() 和 dialog.dimiss()1. public void
- 目录一、集合框架的概述二、集合框架(Java集合可分为Collection 和 Map 两种体系)三、Collection接口中的方法的使用
- 经常会遇到这样的情况,我们在响应客户端请求的数据的时候需要对数据进行处理,比如数据库中的数据是int型,它可能表示某个枚举,或者其它的逻辑意
- 在我们对数组或者集合类进行操作的时候,经常会遇到这样的需求,比如:是否包含某一个“匹配规则”的元素是
- 本文实例讲述了C#实现最完整的文件和目录操作类。分享给大家供大家参考。具体如下:using System;using System.Text
- 大多数浏览器会对同一域名的请求限制请求数量,一般是在8个以内。每次最多可以同时请求8个,要是资源多于8个,那么剩下的就要排队等待请求了。所以
- 本文实例讲述了C#编程实现统计文件夹内文件和隐藏文件的方法。分享给大家供大家参考,具体如下:C#统计文件夹内的文件,包括隐藏文件,显示那个隐
- 懒加载 ,也称为嵌套查询 需要查询关联信息时,使用 Mybatis 懒加载特性可有效的减
- 本文实例为大家分享了java使用HashMap实现斗地主的具体代码,供大家参考,具体内容如下案例介绍按照斗地主的规则,完成洗牌发牌的动作。
- 在实际工作中,重试重发请求处理是一个非常常见的场景,比如:1、发送消息失败。2、调用远程服务失败。3、争抢锁失败。这些错误可能是因为网络波动
- 本文实例为大家分享了Android画笔屏幕锁小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下1.如果使用GestureOverlay
- springboot配置文件中属性变量引用@@这种属性应用方式是field_name=@field_value@。两个@符号是springb
- 最近项目需要在浏览器中通过URL预览图片。但发现浏览器始终默认下载,而不是预览。研究了一下,发现了问题:// 设置response的Head