gpt4 book ai didi

java - 多个弹跳球线程问题

转载 作者:行者123 更新时间:2023-11-30 11:36:19 24 4
gpt4 key购买 nike

我创建了一个程序,可以制作多个具有随机颜色、速度和半径的弹跳球。当用户点击屏幕时,应该会出现一个新的随机球并在屏幕上移动。但是我有一个多线程问题。当我点击屏幕时,一个球出现并且根本不动。当再次点击时,什么也没有发生。

弹跳球类

public class BouncingBalls extends JPanel implements MouseListener{

private Ball ball;
protected List<Ball> balls = new ArrayList<Ball>(20);
private Container container;
private DrawCanvas canvas;
private int canvasWidth;
private int canvasHeight;
public static final int UPDATE_RATE = 30;

int x = random(480);
int y = random(480);
int speedX = random(30);
int speedY = random(30);
int radius = random(20);
int red = random(255);
int green = random(255);
int blue = random(255);
int count = 0;

public static int random(int maxRange) {
return (int) Math.round((Math.random() * maxRange));
}

public BouncingBalls(int width, int height){

canvasWidth = width;
canvasHeight = height;

ball = new Ball(x, y, speedX, speedY, radius, red, green, blue);
container = new Container();

canvas = new DrawCanvas();
this.setLayout(new BorderLayout());
this.add(canvas, BorderLayout.CENTER);
this.addMouseListener(this);

}

public void start(){

Thread t = new Thread(){

public void run(){

while(true){

update();
repaint();
try {
Thread.sleep(1000 / UPDATE_RATE);
} catch (InterruptedException e) {}
}
}
};
t.start();
}

public void update(){

ball.move(container);
}

class DrawCanvas extends JPanel{

public void paintComponent(Graphics g){

super.paintComponent(g);
container.draw(g);
ball.draw(g);
}

public Dimension getPreferredSize(){

return(new Dimension(canvasWidth, canvasHeight));
}
}

public static void main(String[] args){

javax.swing.SwingUtilities.invokeLater(new Runnable(){

public void run(){

JFrame f = new JFrame("Bouncing Balls");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setContentPane(new BouncingBalls(500, 500));
f.pack();
f.setVisible(true);
}
});
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {

count++;
balls.add(new Ball(x, y, speedX, speedY, radius, red, green, blue));
balls.get(count-1).start();
start();
}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}
}

球类

import java.awt.Color;
import java.awt.Graphics;

public class Ball{

public static int random(int maxRange) {
return (int) Math.round((Math.random() * maxRange));
}

private BouncingBalls balls;
int x = random(480);
int y = random(480);
int speedX = random(30);
int speedY = random(30);
int radius = random(20);
int red = random(255);
int green = random(255);
int blue = random(255);
int i = 0;

public Ball(int x, int y, int speedX, int speedY, int radius, int red, int green, int blue){

this.x = x;
this.y = y;
this.speedX = speedX;
this.speedY = speedY;
this.radius = radius;
this.red = red;
this.green = green;
this.blue = blue;
}

public void draw(Graphics g){

for(Ball ball : balls){

g.setColor(new Color(red, green, blue));
g.fillOval((int)(x - radius), (int)(y - radius), (int)(2 * radius), (int)(2 * radius));
}
}

public void move(Container container){

x += speedX;
y += speedY;

if(x - radius < 0){

speedX = -speedX;
x = radius;
}
else if(x + radius > 500){

speedX = -speedX;
x = 500 - radius;
}

if(y - radius < 0){

speedY = -speedY;
y = radius;
}
else if(y + radius > 500){

speedY = -speedY;
y = 500 - radius;
}
}
}

容器类

import java.awt.Color;
import java.awt.Graphics;

public class Container {

private static final int HEIGHT = 500;
private static final int WIDTH = 500;
private static final Color COLOR = Color.WHITE;

public void draw(Graphics g){

g.setColor(COLOR);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
}

最佳答案

您正在维护对您的球的两种不同引用。

您有一个名为ballBall 引用和一个List 球。您的 updatepaint 方法仅引用单个 ball

Ball 似乎没有 start 方法(我可以看到)所以这个 balls.get(count-1).start() ; 没有意义...

已更新

  • 您不需要对ball的引用
  • 虽然不错,但在测试时,您应该在构造函数中调用 start
  • BouncingBalls 中的update 方法应该遍历 balls 列表,对列表中的每个球调用 move列表...
  • DrawCanvaspaintComponent 方法需要访问并且应该使用balls 列表。这可能通过模型​​界面更好地实现
  • 不要构造带有参数的新Ball,因为它会赋予每个球相同的属性,尤其是当您以任何方式构造它时为其分配随机值...
  • Ball 没有(或不需要)start 方法

enter image description here

public class BouncingBalls extends JPanel implements MouseListener {

// private Ball ball;
protected List<Ball> balls = new ArrayList<Ball>(20);
private Container container;
private DrawCanvas canvas;
private int canvasWidth;
private int canvasHeight;
public static final int UPDATE_RATE = 30;
int x = random(480);
int y = random(480);
int speedX = random(30);
int speedY = random(30);
int radius = random(20);
int red = random(255);
int green = random(255);
int blue = random(255);
int count = 0;

public static int random(int maxRange) {
return (int) Math.round((Math.random() * maxRange));
}

public BouncingBalls(int width, int height) {

canvasWidth = width;
canvasHeight = height;

// ball = new Ball(x, y, speedX, speedY, radius, red, green, blue);
container = new Container();

canvas = new DrawCanvas();
this.setLayout(new BorderLayout());
this.add(canvas, BorderLayout.CENTER);
this.addMouseListener(this);

start();

}

public void start() {

Thread t = new Thread() {
public void run() {

while (true) {

update();
repaint();
try {
Thread.sleep(1000 / UPDATE_RATE);
} catch (InterruptedException e) {
}
}
}
};
t.start();
}

public void update() {
for (Ball ball : balls) {
ball.move(container);
}
}

class DrawCanvas extends JPanel {

public void paintComponent(Graphics g) {

super.paintComponent(g);
container.draw(g);
for (Ball ball : balls) {
ball.draw(g);
}
// ball.draw(g);
}

public Dimension getPreferredSize() {

return (new Dimension(canvasWidth, canvasHeight));
}
}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("Bouncing Balls");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setContentPane(new BouncingBalls(500, 500));
f.pack();
f.setVisible(true);
}
});
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}

@Override
public void mousePressed(MouseEvent e) {

count++;
balls.add(new Ball());
// balls.add(new Ball(x, y, speedX, speedY, radius, red, green, blue));
// balls.get(count - 1).start();
// start();
}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}

public static class Ball {

public int random(int maxRange) {
return (int) Math.round(Math.random() * maxRange);
}
int x = random(480);
int y = random(480);
int speedX = random(30);
int speedY = random(30);
int radius = random(20);
int red = random(255);
int green = random(255);
int blue = random(255);
int i = 0;

public Ball() { //int x, int y, int speedX, int speedY, int radius, int red, int green, int blue) {

// this.x = x;
// this.y = y;
// this.speedX = speedX;
// this.speedY = speedY;
// this.radius = radius;
// this.red = red;
// this.green = green;
// this.blue = blue;
}

public void draw(Graphics g) {

g.setColor(new Color(red, green, blue));
g.fillOval((int) (x - radius), (int) (y - radius), (int) (2 * radius), (int) (2 * radius));

}

public void move(Container container) {

x += speedX;
y += speedY;

if (x - radius < 0) {

speedX = -speedX;
x = radius;
} else if (x + radius > 500) {

speedX = -speedX;
x = 500 - radius;
}

if (y - radius < 0) {

speedY = -speedY;
y = radius;
} else if (y + radius > 500) {

speedY = -speedY;
y = 500 - radius;
}
}
}

public static class Container {

private static final int HEIGHT = 500;
private static final int WIDTH = 500;
private static final Color COLOR = Color.WHITE;

public void draw(Graphics g) {

g.setColor(COLOR);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
}
}

已更新

正如评论员所指出的,ArrayList 不是线程安全的,让多个线程尝试同时访问它不是一个好主意。虽然添加比删除稍微安全一些,但它仍然是不好的做法。

您可以将 ArrayList 替换为 Vector,这将是更简单的解决方案,或者围绕公共(public)监视器锁同步对列表的访问。根据您的示例,我将使用 java.util.Vector

关于java - 多个弹跳球线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14593678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com