1

I'm creating a defined amount of objects from another class, and attempting to randomize the color for each one using java.awt.Color.

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

Where Color.BLUE currently is, I'd like to call one of Color's constructors that uses three integers for the red, green, and blue values (Color(int r, int g, int b)).

How do I call that constructor? I am relatively new to Java and I'm having some trouble wrapping my head around this.

nana
  • 4,426
  • 1
  • 34
  • 48
soltk
  • 13
  • 2
  • 2
    replace Color.BLUE with new Color(r,g,b). r, g, and b can be fields that you set, such as: Random rand = new Random(); float r = rand.nextFloat(); – Jeff Hedgpeth Apr 19 '15 at 19:16
  • That could very well be it. I forgot to type the 'new' keyword. Rookie mistake. I'll edit my code and post what happens. – soltk Apr 19 '15 at 19:26

6 Answers6

1

If you call the constructor with the arguments you want the compiler will select the correct one.

Leon
  • 12,013
  • 5
  • 36
  • 59
  • 1
    actually this definition is wrong http://stackoverflow.com/questions/12893907/is-polymorphism-overloading-and-overriding-are-same-concepts – Tommaso Bertoni Apr 19 '15 at 19:19
  • I find it interesting that Overloading is not considered polymorphic, but a sentence later it is called static polymorphism. It is an interesting way of looking at it. I'll update the answer – Leon Apr 19 '15 at 19:22
  • I have tried doing that. It gives me a "cannot find the method specified". I made sure that I imported the Color class and wrote the Constructor correctly. – soltk Apr 19 '15 at 19:24
  • It could be that you have the wrong Color class. It's probably worth while posting the whole piece of code showing the imports – Leon Apr 19 '15 at 19:27
1

In order to achieve what you want, simply add the following:

new Color(0, 0, 255)

So in essence, it will look like this:

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

In order to achieve random colors each time:

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

To know more about the Color Class, refer to this: Color: Java 7

Hope this helps

TejjD
  • 2,571
  • 1
  • 17
  • 37
  • 1
    would that not be `Random(256)`? – Jeff Hedgpeth Apr 19 '15 at 19:29
  • 1
    I'm most likely only going to use one Random object and call it for each value I want. For example: `Random rn = new Random(); int r = rn.nextInt(255);' – soltk Apr 19 '15 at 19:33
  • @TejjD "Flexibility"? You mean that the random numbers are a bit *randomer*? In the current form, the code is plainly *wrong*. – Marco13 Apr 19 '15 at 19:37
  • @soltk You should definitely use `nextInt(256)`. Otherwise (with `nextInt()`), you'd quickly receive an `IllegalArgumentException`. – Marco13 Apr 19 '15 at 20:37
1

Regarding the actual question: There are multiple constructors for Color. Some of them only differ in the argument type, namely these:

Color(float r, float g, float b)
Color(int r, int g, int b)

When you call

Color c = new Color(r,g,b);

the question about which constructor will be called is actually a bit tricky: It depends on the type of the given arguments. For details, you may refer to the Section 15.9.3. Choosing the Constructor and its Arguments of the Java Language Specification.

However, in this case, you can make it simple: If you pass in three float values, then the float version will be called. If you pass in three int values, then the int version will be called.

Concerning the implementation: If your intention is to create random colors, I'd recommend creating a small helper method:

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

Then, you can simply call this method everywhere, for example...

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));
}
Marco13
  • 53,703
  • 9
  • 80
  • 159
1

It's rather simple, Java picks the constructor to use based on the arguments you supply to the constructor. In case of Color you have 7 options as you can see below:

Color(ColorSpace cspace, float[] components, float alpha)

Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.

Color(float r, float g, float b)

Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0).

Color(float r, float g, float b, float a)

Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0).

Color(int rgb)

Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Color(int rgba, boolean hasalpha)

Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Color(int r, int g, int b)

Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255).

Color(int r, int g, int b, int a)

Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).

If you supply three ints, the last one will be used. In case of three floats it will be the third one.

In your case all you have to do is replace Colour.BLUE with:

new Colour(
    new Random().nextInt(256),
    new Random().nextInt(256),
    new Random().nextInt(256),
)
nana
  • 4,426
  • 1
  • 34
  • 48
0

Assuming you're after this constructor, then

new Color(r, g, b)

is instantiating the class using that constructor, where r, g and b are variables / fields that correspond to red, green and blue.

async
  • 1,537
  • 11
  • 28
  • I forgot the 'new' keyword. Rookie mistake. However, I'm after the constructor that uses integers instead of floating point decimals. It should work the same. – soltk Apr 19 '15 at 19:28
  • @soltk Then just use that one: http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#Color%28int, int, int%29 – async Apr 19 '15 at 19:37
0

Just do this:

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

where r, g, b are previously defined random integers from 0 to 255;

Kuba
  • 839
  • 7
  • 16