java小程序--10个运动的小球

刚学java,老师带着我们一起写得一个小程序,设置十个小球,每个球的大小,颜色,位置,运动方向等都让它随机,然后碰到框架的边框时反弹,反弹之后小球颜色变为另一种随机颜色。做完这个,我想如果能够解决键盘控制这个面板上的坐标的话,就可以加上一个小木板,这样就做成了小时候经常玩的弹球游戏了,等后面再慢慢研究这个东西。

当中运动的轨迹的边界的处理,要考虑到画小球的那个横纵坐标是指小球外接方形的左上角坐标,所以要注意设置好参数。

下面是运动小球的代码:

首先是这个Ball类的代码:

package day04;

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

import javax.swing.JPanel;

public class Ball {
	//四个方向
	public final static int LEFT_UP=0;
	public final static int LEFT_DOWN=1;
	public final static int RIGHT_UP=2;
	public final static int RIGHT_DOWN=3;
	//球的属性
	private int x;
	private int y;
	private int r;
	private int direction;
	private Color color;
	private int speed;
	private JPanel panel;
	
	
	public void changeColor(){
		//改变当前小球的颜色为随机颜色
		Random ran=new Random();
		this.setColor(new Color(
		ran.nextInt(256),ran.nextInt(256),ran.nextInt(256)));
	}
	
	public void draw(Graphics g){
		//画一个圆
		g.setColor(color);
		g.fillOval(x, y, 2*r, 2*r);
	}
	
	public void move(){
		//小球的运动方法
		switch(direction){
		case LEFT_UP:
			x--;y--;
			if(x<=0) {direction=RIGHT_UP;changeColor();}
			if(y<=0) {direction=LEFT_DOWN;changeColor();}
			break;
		case LEFT_DOWN:
			x--;y++;
			if(x<=0) {direction=RIGHT_DOWN;changeColor();}
			if(y>=panel.getHeight()-2*r){direction=LEFT_UP;changeColor();}
			break;
			
		case RIGHT_UP:
			x++;y--;
			if(x>=panel.getWidth()-2*r){direction=LEFT_UP;changeColor();}
			if(y<=0) {direction=RIGHT_DOWN;changeColor();}
			break;
		case RIGHT_DOWN:
			x++;y++;
			if(x>=panel.getWidth()-2*r) {direction=LEFT_DOWN;changeColor();}
			if(y>=panel.getHeight()-2*r) {direction=RIGHT_UP;changeColor();}
			break;
		}
	}
	
	//各函数的构造方法
	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;
	}
	public int getDirection() {
		return direction;
	}
	public void setDirection(int direction) {
		this.direction = direction;
	}
	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}
	public int getSpeed() {
		return speed;
	}
	public void setSpeed(int speed) {
		this.speed = speed;
	}
	public void setPanel(JPanel panel) {
		this.panel = panel;
	}
	
	
	
}

然后是这个MyBallFrame框架的代码:

package day04;

import javax.swing.JFrame;

public class MyBallFrame {
	public static void main(String [] args) {
		//设置框架的各类属性
		JFrame frame=new JFrame();
		frame.setSize(800,600);
		frame.setTitle("一个运动的小球");
		frame.setLocation(100,50);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		MyBallPanel panel= new MyBallPanel();
		frame.add(panel);
		panel.startRun();
		frame.setVisible(true);

	}
}

最后是这个MyBallPanel的面板代码:

package day04;

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

import javax.swing.JPanel;

public class MyBallPanel extends JPanel{
	private Ball[]balls=new Ball[10];
	
	public MyBallPanel(){
		//构造方法
		Random ran=new Random();
		for(int i=0;i<balls.length;i++){
			//初始化10个小球的属性值
			Ball ball=new Ball();
			ball.setR(10+5*i);
			ball.setX(ran.nextInt(800-2*ball.getR()-10));
			ball.setY(ran.nextInt(600-2*ball.getR())-10);
			ball.setColor(new Color(
				ran.nextInt(256),ran.nextInt(256),ran.nextInt(256)));
			ball.setDirection(ran.nextInt(4));
			ball.setSpeed(3+ran.nextInt(20));
			ball.setPanel(this);
			balls[i]=ball;
		}
		}
	
	public void paint(Graphics g){
		//画出10个园
		super.paint(g);
		for(int i=0;i<balls.length;i++){
			balls[i].draw(g);
		}
	}
	
	public void startRun(){
		//kaishi
		for(int i=0;i<balls.length;i++){
			final int temp=i;
			new Thread(){
				public void run(){
					while(true){
						balls[temp].move();
						try {
							sleep(balls[temp].getSpeed());
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						repaint();
					}
					
				}
			}.start();
		}
	}
}


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个简单的多线程小球碰撞程序的 Java 代码示例: ``` import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class BallCollision extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; private static final int WIDTH = 600; private static final int HEIGHT = 400; private static final int DELAY = 10; private ArrayList<Ball> balls = new ArrayList<>(); private Timer timer; public BallCollision() { setBackground(Color.BLACK); timer = new Timer(DELAY, this); timer.start(); } public static void main(String[] args) { JFrame frame = new JFrame("Ball Collision"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(WIDTH, HEIGHT); frame.add(new BallCollision()); frame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { for (Ball ball : balls) { ball.move(); checkCollision(ball); } repaint(); } private void checkCollision(Ball ball) { for (Ball otherBall : balls) { if (ball != otherBall && ball.intersects(otherBall)) { ball.reverseDirection(); otherBall.reverseDirection(); } } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (Ball ball : balls) { ball.draw(g); } } private class Ball implements Runnable { private int x; private int y; private int dx; private int dy; private int radius; private Color color; private Thread thread; public Ball(int x, int y, int dx, int dy, int radius, Color color) { this.x = x; this.y = y; this.dx = dx; this.dy = dy; this.radius = radius; this.color = color; thread = new Thread(this); } public void start() { thread.start(); } public boolean intersects(Ball otherBall) { int distanceX = x - otherBall.getX(); int distanceY = y - otherBall.getY(); int radiusSum = radius + otherBall.getRadius(); return distanceX * distanceX + distanceY * distanceY <= radiusSum * radiusSum; } public void reverseDirection() { dx = -dx; dy = -dy; } public void move() { if (x <= radius || x >= WIDTH - radius) { dx = -dx; } if (y <= radius || y >= HEIGHT - radius) { dy = -dy; } x += dx; y += dy; } public void draw(Graphics g) { g.setColor(color); g.fillOval(x - radius, y - radius, radius * 2, radius * 2); } @Override public void run() { while (true) { try { Thread.sleep(DELAY); } catch (InterruptedException e) { e.printStackTrace(); } move(); checkCollision(this); } } public int getX() { return x; } public int getY() { return y; } public int getRadius() { return radius; } } public void addBall() { Random random = new Random(); int x = random.nextInt(WIDTH - 50) + 25; int y = random.nextInt(HEIGHT - 50) + 25; int dx = random.nextInt(10) + 1; int dy = random.nextInt(10) + 1; int radius = random.nextInt(20) + 10; Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); Ball ball = new Ball(x, y, dx, dy, radius, color); balls.add(ball); ball.start(); } } ``` 这个程序创建了一个 `BallCollision` 类,它继承了 `JPanel` 并实现了 `ActionListener` 接口。程序通过 `Timer` 定时器来触发多线程更新和绘制小球的动画效果。在 `Ball` 类中实现了小球运动、碰撞检测和绘制方法,并通过多线程来实现小球的异步运动。程序还提供了 `addBall()` 方法用于添加新的小球

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值