我是靠谱客的博主 友好便当,最近开发中收集的这篇文章主要介绍java 运动的大球吃小球_大球吃小球,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Ball.java

package day7;

import java.awt.Color;

import java.awt.Graphics;

public class Ball {

int x,y;

int d;

Color ballColor;

int speed;

int position;

/*小球运动方向*/

public static final int LEFT_UP=0;

public static final int RIGHT_UP=1;

public static final int LEFT_DOWN=2;

public static final int RIGHT_DOWN=3;

public Ball(int x,int y,int d,Color ballColor,int speed,int position){

this.x=x;

this.y=y;

this.d=d;

this.ballColor=ballColor;

this.speed=speed;

this. position=position;

}

//画小球

public void drawBall(Graphics g){

g.setColor(ballColor);

g.fillOval(x, y, d, d);

}

//小球运动方向

public void ballMove(){

switch(this.position){

case LEFT_UP:

x-=speed;

y-=speed;

if(x<=0){

this.position=RIGHT_UP;

}else if(y<=0){

this.position=LEFT_DOWN;

}

break;

case RIGHT_UP:

x+=speed;

y-=speed;

if(x>=700){

this.position=LEFT_UP;

}else if (y<=0) {

this.position=RIGHT_DOWN;

}

break;

case LEFT_DOWN:

x-=speed;

y+=speed;

if (x<=0) {

this.position=RIGHT_DOWN;

}else if (y>=470) {

this.position=LEFT_UP;

}

break;

case RIGHT_DOWN:

x+=speed;

y+=speed;

if (x>=700) {

this.position=LEFT_DOWN;

}else if (y>=470) {

this.position=RIGHT_UP;

}

break;

}

}

}

BallMain.java

package day7;

import java.awt.Dimension;

import java.awt.Toolkit;

import javax.swing.JFrame;

import day2.TJPanel;

public class BallMain extends JFrame {

//屏幕的宽高设置

public static final int SCREEN_WIDTH=750;

public static final int SCREEN_HEIGHT=550;

//

Dimension d=Toolkit.getDefaultToolkit().getScreenSize();

int width=(int)d.getWidth();

int height=(int)d.getHeight();

/*构造方法*/

public BallMain(){

this.setTitle("V1.0");

//this.setBounds(0, 0, width, height);

this.setBounds(100, 100, SCREEN_WIDTH, SCREEN_HEIGHT);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//添加小球到窗体上

BallJPanel bj=new BallJPanel();

this.add(bj);

this.setVisible(true);//显示窗体,调用paint方法

bj.startBalls();

}

public static void main(String[] args) {

new BallMain();

}

}

BallJPanel.java

package day7;

import java.awt.Color;

import java.awt.Graphics;

import java.util.List;

import java.util.ArrayList;

import javax.swing.JPanel;

import sun.awt.RepaintArea;

public class BallJPanel extends JPanel {

//存储小球的集合

List ballList=new ArrayList();

//小球数量

private int ballNumber=50;

public BallJPanel(){

addBall();

}

//产生小球的方法

public void addBall(){

for(int i=0;i

int x=(int) (Math.random()*BallMain.SCREEN_WIDTH);

int y=(int) (Math.random()*BallMain.SCREEN_HEIGHT);

int position=(int) (Math.random()*4);

//int d=(int) (Math.random()*15+1);

int d=50;

int speed=(int) (Math.random()*50);

//int speed=50;

//颜色 rgb

int red=(int) (Math.random()*255+1);

int green=(int) (Math.random()*255+1);

int blue=(int) (Math.random()*255+1);

Color ballColor=new Color(red,green,blue);

Ball b=new Ball(x, y, d, ballColor, speed, position);

//将小球添加到集合中

ballList.add(b);}

}

public void paint(Graphics g){

super.paint(g);

for(int i=0;i

Ball ball=ballList.get(i);

ball.drawBall(g);

/*g.setColor(ball.ballColor);

g.fillOval(ball.x,ball.y,ball.d,ball.d);*/}

}

//小球运动

public void startBalls(){

//启动线程------匿名内部类

new Thread(){

public void run() {

while (true) {

//1.遍历小球集合

for(int i=0;i

//2.取出每一个小球

Ball b=ballList.get(i);

//3.让每一个小球移动

b.ballMove();

}

for (int i = 0; i < ballList.size(); i++) {

Ball b1=ballList.get(i);

for (int j = i+1; j < ballList.size(); j++) {

Ball b2=ballList.get(j);

BallAndBall bab=new BallAndBall();

bab.ballCrach(b1, b2);

/*if (bab.isballCrach(b1, b2)) {

if (b1.d>=b2.d) {

b1.d+=b2.d/3;

ballList.remove(b2);

break;

}else if (b2.d>=b1.d) {

b2.d+=b1.d/3;

ballList.remove(b1);

break;

}

}*/

}

}

try {

Thread.sleep(200);

} catch (Exception e) {

// TODO: handle exception

}

repaint();

}}

}.start();

}

}

BallAndBall.java

package day7;

public class BallAndBall {

public void ballCrach(Ball b1,Ball b2){

int x1=b1.x+b1.d/2;

int y1=b1.y+b1.d/2;

int x2=b2.x+b2.d/2;

int y2=b2.y+b2.d/2;

//计算圆心距

double e=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

//如果碰撞上

if (e<=b1.d/2+b2.d/2) {

switch (b1.position) {

case Ball.LEFT_UP:

b1.position=Ball.RIGHT_DOWN;

break;

case Ball.RIGHT_UP:

b1.position=Ball.LEFT_DOWN;

break;

case Ball.LEFT_DOWN:

b1.position=Ball.RIGHT_UP;

break;

case Ball.RIGHT_DOWN:

b1.position=Ball.LEFT_UP;

break;

}

switch (b2.position) {

case Ball.LEFT_UP:

b2.position=Ball.RIGHT_DOWN;

break;

case Ball.RIGHT_UP:

b2.position=Ball.LEFT_DOWN;

break;

case Ball.LEFT_DOWN:

b2.position=Ball.RIGHT_UP;

break;

case Ball.RIGHT_DOWN:

b2.position=Ball.LEFT_UP;

break;

}}

}

//检测碰撞

public boolean isballCrach(Ball b1,Ball b2){

boolean flag=false;

int x1=b1.x+b1.d/2;

int x2=b1.x+b2.d/2;

int y1=b1.y+b1.d/2;

int y2=b1.y+b2.d/2;

//计算圆心距

double e=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

//如果碰撞上

if (e<=b1.d/2+b2.d/2) {

return true;

}

return false;

}

}

标签:java,int,小球,大球,b1,b2,import,public

来源: https://blog.csdn.net/xuexi____/article/details/90143465

最后

以上就是友好便当为你收集整理的java 运动的大球吃小球_大球吃小球的全部内容,希望文章能够帮你解决java 运动的大球吃小球_大球吃小球所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部