Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中编写关于不同颜色、不同半径球的代码_Java_Arrays - Fatal编程技术网

如何在java中编写关于不同颜色、不同半径球的代码

如何在java中编写关于不同颜色、不同半径球的代码,java,arrays,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(); }

主课这是我课程的主课

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

我想添加黄色,蓝色,红色的球和不同的半径,包括。如何写入信息这些是故障线路:

ballArr[i]=新球(新点(50,50),5)

这里,您从Ball调用两参数构造函数。看起来是这样的:

public Ball(Point l, int r){
        location = l;
        radius = r;
        color = Color.RED;
    }
所以你的球都是红色的,半径为5,在50,50的位置。您确实有一个三参数构造函数,它也可以设置球的颜色。如果你希望这些东西是随机的,使用一个
random
对象,为每个球选择一个随机的颜色、半径和点,就可以了

应该清楚地知道如何获得半径和点的随机数。如果您想知道颜色,这里有一种方法:定义一个包含颜色对象的数组

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

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

int colornumber=random.nextInt(colors.length)

并检索颜色

Color c=colors[colornumber]

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


编辑

public class BallWorld{
....
私有随机=新随机();
私有颜色[]颜色={Color.red,Color.blue,Color.yellow};
公共球场(国际球场){
超级();
设置大小(帧宽度、帧高度);
setTitle(“我的弹跳球应用程序”);
ballArr=新球[ballCnt];
this.ballCnt=ballCnt;
对于(int i=0;i//在此处创建属性
int bcn=random.nextInt(colors.length);
颜色=颜色[bcn];
int ballradius=random.nextInt(10);//根据需要进行更改
---->int posx=random.nextInt(200);//根据需要进行更改
---->int posy=random.nextInt(200);//根据需要进行更改
//这将在给定上述计算参数的情况下创建一个球
---->ballArr[i]=新球(新点(posx、posy)、球半径、球颜色);
int ddx=(int)(5*Math.random());//练习1
int ddy=(int)(4*Math.random());//练习1
巴拉尔[i].setMotion(ddx,ddy);
}
}

这些是故障线路:

ballArr[i]=新球(新点(50,50),5);

在这里,您从Ball调用双参数构造函数。它如下所示:

public Ball(Point l, int r){
        location = l;
        radius = r;
        color = Color.RED;
    }
所以你的球都是红色的,半径为5,位置为50,50。你有一个三参数构造器,也可以设置球的颜色。如果你想让这些东西是随机的,使用一个
random
对象,为每个球选择一个随机的颜色、半径和点,然后你就可以开始了

应该很清楚你是如何得到半径和点的随机数的。如果你想知道颜色,这里有一个方法:定义一个包含颜色对象的数组

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

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

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

并检索颜色

Color c=colors[colornumber]

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


编辑

public class BallWorld{
....
私有随机=新随机();
私有颜色[]颜色={Color.red,Color.blue,Color.yellow};
公共球场(国际球场){
超级();
设置大小(帧宽度、帧高度);
setTitle(“我的弹跳球应用程序”);
ballArr=新球[ballCnt];
this.ballCnt=ballCnt;
对于(int i=0;i//在此处创建属性
int bcn=random.nextInt(colors.length);
颜色=颜色[bcn];
int ballradius=random.nextInt(10);//根据需要进行更改
---->int posx=random.nextInt(200);//根据需要进行更改
---->int posy=random.nextInt(200);//根据需要进行更改
//这将在给定上述计算参数的情况下创建一个球
---->ballArr[i]=新球(新点(posx、posy)、球半径、球颜色);
int ddx=(int)(5*Math.random());//练习1
int ddy=(int)(4*Math.random());//练习1
巴拉尔[i].setMotion(ddx,ddy);
}
}

这些是故障线路:

ballArr[i]=新球(新点(50,50),5);

在这里,您从Ball调用双参数构造函数。它如下所示:

public Ball(Point l, int r){
        location = l;
        radius = r;
        color = Color.RED;
    }
所以你的球都是红色的,半径为5,位置为50,50。你有一个三参数构造器,也可以设置球的颜色。如果你想让这些东西是随机的,使用一个
random
对象,为每个球选择一个随机的颜色、半径和点,然后你就可以开始了

应该很清楚你是如何得到半径和点的随机数的。如果你想知道颜色,这里有一个方法:定义一个包含颜色对象的数组

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

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

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

并检索颜色

Color c=colors[colornumber]

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


编辑

public class BallWorld{
....
私有随机=新随机();
私有颜色[]颜色={Color.red,Color.blue,Color.yellow};
公共球场(国际球场){
超级();
设置大小(帧宽度、帧高度);
setTitle(“我的弹跳球应用程序”);
ballArr=新球[ballCnt];
this.ballCnt=ballCnt;
对于(int i=0;i//在此处创建属性
int bcn=random.nextInt(colors.length);
颜色=颜色[bcn];
int ballradius=random.nextInt(10);//根据需要进行更改
---->int posx=random.nextInt(200);//根据需要进行更改
---->int posy=random.nextInt(200);//更改以适合您的n
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); 
    }
}
ballArr[i] = new Ball(new Point(50,50), 5);
ballArr[i] = new Ball(new Point(50,50), 5, Color.xxx);