不会产生多个弹跳球

时间:2019-03-24 17:43:33

标签: java oop

我需要创建一个包含多个弹跳球的屏幕。 已经做到了,但是不知何故卡在了多个随机球上。 另外,我需要每个球以不同的速度开始-更大的球要慢一些(但大于50的球都可以具有相同的慢速)。击中窗口边框时,每个球都会改变方向。

尝试使用2种方法,一种使用生成的GUI生成,另一种使用绘制的GUI绘制。

import biuoop.GUI;
import biuoop.DrawSurface;
import biuoop.Sleeper;
import java.util.Random;
import java.awt.Color;
/**
 * this class is a short program of a multiple bouncing balls.
 */
public class MultipleBouncingBallsAnimation {
    /**
    * this method converts an array from strings to integers.
    * @param numbers the strings array.
    * @return array of integers.
    */
    public int[] stringsToInts(String[] numbers) {
        int[] arr = new int[numbers.length];
        for (int i = 0; i < numbers.length; i++) {
            arr[i] = Integer.parseInt(numbers[i]);
        }
        return arr;
    }
    /**
     * Create array of random balls
     */
 public Ball[] createBalls(int[] radiuses, int maxRadius, int horizontalBound, int verticalBound) {
     Random rand = new Random();
     Ball[] balls = new Ball[radiuses.length];
     Color[] color = new Color[radiuses.length];
     for(int j=0; j < radiuses.length; j++)
     {
         color[j] = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
         balls[j] = new Ball(radiuses[j] + rand.nextInt(horizontalBound - 2 * radiuses[j]),
                 radiuses[j] + rand.nextInt(verticalBound - 2 * radiuses[j]),
                 radiuses[j], color[j], horizontalBound, verticalBound);
     Velocity v1 = Velocity.fromAngleAndSpeed(rand.nextInt(360),  (maxRadius) / (double) balls[j].getSize());
     balls[j].setVelocity(v1);
 }
 return balls;
     }
 /**
  * Method to draw balls.
  */
 public void drawBalls(String[] args, int horizontalBound, int verticalBound) {
     int[] radiuses = stringsToInts(args);
     int maxRadius = radiuses[0];
     for (int i = 1; i < radiuses.length; ++i) {
         if (radiuses[i] > maxRadius) {
             maxRadius = radiuses[i];
         }
     }
     Ball[] balls = createBalls(radiuses, maxRadius,
             horizontalBound, verticalBound);
             GUI gui = new GUI("title", horizontalBound, verticalBound);
             Sleeper sleeper = new Sleeper();
             while (true) {
                 DrawSurface d = gui.getDrawSurface();
                 for (int i = 0; i < balls.length; ++i) {
                     balls[i].moveOneStep();
                     balls[i].drawOn(d);

                 }
                 sleeper.sleepFor(3);  // wait for 3 milliseconds.
                 gui.show(d);
             }
 }
    /**
     * this is the main method.
     * @param args this array stores the user's input.
     */
 public static void main(String[] args) {
     int horizontalBound = 800;
     int verticalBound = 600;
     MultipleBouncingBallsAnimation m = new MultipleBouncingBallsAnimation();
     m.drawBalls(args, horizontalBound, verticalBound);
 }
}

预期-屏幕上出现多个球,出现错误:

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:0     在MultipleBouncingBallsAnimation.drawBalls(MultipleBouncingBallsAnimation.java:45)     在MultipleBouncingBallsAnimation.main(MultipleBouncingBallsAnimation.java:74)

0 个答案:

没有答案