Java 如何指定要调用的类的构造函数?
我正在从另一个类创建定义数量的对象,并尝试使用java.awt.color对每个对象的颜色进行随机化Java 如何指定要调用的类的构造函数?,java,Java,我正在从另一个类创建定义数量的对象,并尝试使用java.awt.color对每个对象的颜色进行随机化 for (int i = 0; i < numBalls; i++){ ballsInSim.add( new BoxBall( 0, 0, (int) boxWidth, (int) boxHeight, rng.nextInt(3
for (int i = 0; i < numBalls; i++){
ballsInSim.add(
new BoxBall(
0,
0,
(int) boxWidth,
(int) boxHeight,
rng.nextInt(35) + 15,
rng.nextInt(500) + 25,
rng.nextInt(500) + 25,
Color.BLUE, // Create new Colour here using constructor
myCanvas
)
);
}
for(inti=0;i
在当前的Color.BLUE
中,我想调用一个Color的构造函数,它使用三个整数表示红色、绿色和蓝色值(Color(int r、int g、int b))
我怎么称呼这个构造函数?我对Java相对来说是新手,我在思考这个问题时遇到了一些困难 如果您使用所需参数调用构造函数,编译器将选择正确的构造函数。如果您使用所需参数调用构造函数,编译器将选择正确的构造函数。假设您在后面,则 正在使用该构造函数实例化类,其中
r
、g
和b
是对应于红色、绿色和蓝色的变量/字段 假设你在找,那么
正在使用该构造函数实例化类,其中
r
、g
和b
是对应于红色、绿色和蓝色的变量/字段 为了实现您想要的,只需添加以下内容:
new Color(0, 0, 255)
因此,从本质上看,它将是这样的:
ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight, rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25, new Color(0, 0, 255), myCanvas));
为了每次获得随机颜色:
Random R = new Random(256);
Random G = new Random(256);
Random B = new Random(256);
//your color constructor will then be:
new Color(R.nextInt(), G.nextInt(), B.nextInt());
要了解有关颜色类的更多信息,请参阅以下内容:
希望这有助于为了实现您的目标,只需添加以下内容:
new Color(0, 0, 255)
因此,从本质上看,它将是这样的:
ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight, rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25, new Color(0, 0, 255), myCanvas));
为了每次获得随机颜色:
Random R = new Random(256);
Random G = new Random(256);
Random B = new Random(256);
//your color constructor will then be:
new Color(R.nextInt(), G.nextInt(), B.nextInt());
要了解有关颜色类的更多信息,请参阅以下内容:
希望这能有所帮助只要这样做:
ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight, rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25, new Color(r, g, b), myCanvas));}
其中,r,g,b
是先前定义的0到255之间的随机整数 只要这样做:
ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight, rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25, new Color(r, g, b), myCanvas));}
其中,
r,g,b
是先前定义的0到255之间的随机整数 关于实际问题:Color
有多个构造函数。其中一些仅在参数类型上有所不同,即:
Color(float r, float g, float b)
Color(int r, int g, int b)
当你打电话的时候
Color c = new Color(r,g,b);
关于调用哪个构造函数的问题实际上有点棘手:它取决于给定参数的类型。有关详细信息,请参阅Java语言规范的
但是,在这种情况下,您可以简化:如果传入三个float
值,则将调用float
版本。如果传入三个int
值,则将调用int
版本
关于实现:如果您打算创建随机颜色,我建议您创建一个小助手方法:
private static final Random COLOR_RANDOM = new Random(0);
private static Color createRandomColor() {
int r = COLOR_RANDOM.nextInt(256);
int g = COLOR_RANDOM.nextInt(256);
int b = COLOR_RANDOM.nextInt(256);
return new Color(r,g,b);
}
然后,您可以在任何地方调用此方法,例如
for (int i = 0; i < numBalls; i++)
{
ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight,
rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25,
createRandomColor(), // <---- ... here!
myCanvas));
}
for(int i=0;i createRandomColor(),//关于实际问题:Color
有多个构造函数。其中一些构造函数仅在参数类型上有所不同,即:
Color(float r, float g, float b)
Color(int r, int g, int b)
当你打电话的时候
Color c = new Color(r,g,b);
关于调用哪个构造函数的问题实际上有点棘手:它取决于给定参数的类型
但是,在这种情况下,您可以简化:如果传入三个float
值,则将调用float
版本。如果传入三个int
值,则将调用int
版本
关于实现:如果您打算创建随机颜色,我建议您创建一个小助手方法:
private static final Random COLOR_RANDOM = new Random(0);
private static Color createRandomColor() {
int r = COLOR_RANDOM.nextInt(256);
int g = COLOR_RANDOM.nextInt(256);
int b = COLOR_RANDOM.nextInt(256);
return new Color(r,g,b);
}
然后,您可以在任何地方调用此方法,例如
for (int i = 0; i < numBalls; i++)
{
ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight,
rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25,
createRandomColor(), // <---- ... here!
myCanvas));
}
for(int i=0;i createRandomColor(),//非常简单,Java根据您提供给构造函数的参数选择要使用的构造函数。对于颜色,您有7个选项,如下所示:
Color(ColorSpace cspace, float[] components, float alpha)
使用浮点数组中指定的颜色组件和指定的alpha在指定的颜色空间中创建颜色
Color(float r, float g, float b)
使用范围(0.0-1.0)内指定的红色、绿色和蓝色值创建不透明的sRGB颜色
使用范围(0.0-1.0)内指定的红色、绿色、蓝色和alpha值创建sRGB颜色
使用指定的组合RGB值创建不透明的sRGB颜色,该RGB值由位16-23中的红色分量、位8-15中的绿色分量和位0-7中的蓝色分量组成
Color(int rgba, boolean hasalpha)
Color(int r, int g, int b)
使用指定的组合RGBA值创建sRGB颜色,该值由位24-31中的alpha分量、位16-23中的红色分量、位8-15中的绿色分量和位0-7中的蓝色分量组成
Color(int rgba, boolean hasalpha)
Color(int r, int g, int b)
使用范围(0-255)内的指定红、绿和蓝值创建不透明的sRGB颜色
使用范围(0-255)内指定的红色、绿色、蓝色和alpha值创建sRGB颜色
如果您提供三个整数,将使用最后一个整数。如果有三个浮点,则将使用第三个整数
在您的情况下,只需将颜色.蓝色
替换为:
很简单,Java根据您提供给构造函数的参数选择要使用的构造函数。对于颜色,您有7个选项,如下所示:
Color(ColorSpace cspace, float[] components, float alpha)
使用浮点数组中指定的颜色组件和