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

弹跳球类

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);
}
}

最佳答案

您要对球保持两种不同的引用。

您有一个名为Ball的单个ball和一个球的List的引用。您的updatepaint方法仅引用单个ballBall似乎没有start方法(我可以看到),因此此balls.get(count-1).start();没有任何意义。

更新了

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

  • 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,这将是更简单的解决方案,也可以在通用监视器锁定周围同步对列表的访问。给出您的示例,我将使用java.util.Vector

    09-28 03:57