用Java写一个多线程小球碰撞程序
时间: 2023-08-04 14:11:08 浏览: 93
JAVA运用多线程实现碰撞的小球
5星 · 资源好评率100%
好的,以下是一个简单的多线程小球碰撞程序的 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()` 方法用于添加新的小球。
阅读全文