我如何在java中编写有关不同颜色,不同半径球的代码

时间:2014-03-29 16:07:29

标签: java arrays

主要课程这是我的课程的主要课程

    public class Main {

    public static void main(String[] args) {
        BallWorld frm = new BallWorld(3);
        frm.setVisible(true);           


        for (int i=0; i<1000; i++){
            frm.stepTheBall();
        }
    }
}

BallWorld.java类此类与JFrame相关

import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JFrame;


public class BallWorld extends JFrame {
    public final int FRAMEWIDTH = 600;
    public final int FRAMEHEIGHT = 400;

    private Ball[] ballArr;
    private int ballCnt;

    public BallWorld(int ballCnt){
        super();
        setSize(FRAMEWIDTH, FRAMEHEIGHT);
        setTitle("My Bouncing Ball Application");

        ballArr = new Ball[ballCnt];
        this.ballCnt = ballCnt;

        for (int i=0; i < ballCnt; i++){
            ballArr[i] = new Ball(new Point(50,50), 5);
            int ddx = (int) (5*Math.random()); //Exercise 1
            int ddy = (int) (4*Math.random());  //Exercise 1
            ballArr[i].setMotion(ddx, ddy); 
        }
    }

    public void paint(Graphics g){
        super.paint(g);
        for (int i=0; i < ballCnt; i++){
            ballArr[i].paint(g);    
        }
    }

    public void stepTheBall(){
        for (int i=0; i < ballCnt; i++){        
            ballArr[i].move();

            Point loc = ballArr[i].getLocation();

            if (loc.x < ballArr[i].getRadius() ||
                loc.x > FRAMEWIDTH-ballArr[i].getRadius()){
                ballArr[i].reclectVert();
            }

            if (loc.y < ballArr[i].getRadius() ||
                    loc.y > FRAMEHEIGHT-ballArr[i].getRadius()){
                ballArr[i].reclectHoriz();
            }
        }   
        repaint();
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Ball.java类此类与球信息相关

public class Ball {

        private Point location;
        private int radius;
        private Color color;
        private int dx, dy;

        public Ball(Point l, int r, Color c){
            location = l;
            radius = r;
            color = c;
        }

        public Ball(Point l, int r){
            location = l;
            radius = r;
            color = Color.RED;
        }

        public Point getLocation() {
            return location;
        }

        public void setLocation(Point location) {
            this.location = location;
        }

        public int getRadius() {
            return radius;
        }

        public void setRadius(int radius) {
            this.radius = radius;
        }

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
        }

        public void setMotion(int dx, int dy){
            this.dx = dx;
            this.dy = dy;
        }

        public void move(){
            location.translate(dx, dy);
        }

        public void moveTo(int x, int y){
            location.move(x, y);
        }

        public void paint (Graphics g) {
            g.setColor (color);
            g.fillOval (location.x-radius, location.y-radius, 2*radius, 2*radius);
        }

        public void reclectHoriz() {
            dy = -dy;       
        }   

        public void reclectVert() {
            dx = -dx;       
        }
    }

我想添加黄色,蓝色,红色球和不同的半径,包括。我怎样才能写出这些信息

2 个答案:

答案 0 :(得分:2)

这些是错误的路线:

ballArr[i] = new Ball(new Point(50,50), 5);

在这里,您从Ball调用双参数构造函数。它看起来像这样:

public Ball(Point l, int r){
        location = l;
        radius = r;
        color = Color.RED;
    }

所以你的球都是红色的,半径为5,位于50,50位置。你有一个三参数构造函数,也可以设置球的颜色。如果您希望这些内容是随机的,请使用Random对象,为每个球选择随机颜色,半径和点,然后就可以了。

应该清楚如何获得半径和点的随机数。如果你对Color有疑问,可以采用以下方法:定义一个包含Color对象的数组。

Color[] colors = {Color.Red, Color.Blue, Color.Yellow};

根据数组的大小获取一个随机数

int colornumber = random.nextInt(colors.length);

并检索颜色

Color c = colors[colornumber]

然后,创建具有随机属性的球。


修改

public class BallWorld {
....
private Random random = new Random();
private Color[] colors={Color.red,Color.blue,Color.yellow};

public BallWorld(int ballCnt){
    super();
    setSize(FRAMEWIDTH, FRAMEHEIGHT);
    setTitle("My Bouncing Ball Application");

    ballArr = new Ball[ballCnt];
    this.ballCnt = ballCnt;

    for (int i=0; i < ballCnt; i++){
---->   // Create attributes here
        int bcn = random.nextInt(colors.length);
        Color ballcolor = colors[bcn];
        int ballradius = random.nextInt(10); // change to suit your needs
---->   int posx = random.nextInt(200); // change to suit your needs
---->   int posy = random.nextInt(200); // change to suit your needs
        // this creates a ball given the above calculated parameters
---->   ballArr[i] = new Ball(new Point(posx,posy), ballradius, ballcolor);
        int ddx = (int) (5*Math.random()); //Exercise 1
        int ddy = (int) (4*Math.random());  //Exercise 1
        ballArr[i].setMotion(ddx, ddy); 
    }
}

答案 1 :(得分:1)

而不是:

ballArr[i] = new Ball(new Point(50,50), 5);

使用另一个构造函数:

ballArr[i] = new Ball(new Point(50,50), 5, Color.xxx);