tags:

views:

350

answers:

6

I was trying to make a new color in java using

Color temp = new Color(foo.getBackground());

and it kept telling me cannot find symbol.

But this works

Color temp = (foo.getbackground());

Why?

+11  A: 

That's because foo.getBackground() returns a Color instance, and there's no Color constructor which takes a Color instance as an argument.

Vinay Sajip
if you want to make a copy of foo's color, you can do this: `Color tmp = new Color(foo.getBackground().getRGB());`
akf
Though there's not much point, as Color instances are immutable.
Vinay Sajip
Indeed, and I suspect that kokokok really just wants to say`Color temp = foo.getBackground();`
Eli Courtwright
A: 

Yes, you can do it, the problem is that maybe foo.getBackground does'nt returns an integer or something similar.

Color c = new Color(23,32,43)

works perfectly

Rodrigo Asensio
A: 

There isn't a constructor for Color that takes just a Color. In the second instance you're assigning a variable that was returned from a function.

Jon
A: 

The Color class does not have a constructor taking an other instance of Color as an argument, and that is what foo.getBackground() returns. IIRC, the Color class in Java is immutable - so there is simply no point in providing a constructor that would create a copy of an existing Color object.

cg
+2  A: 

Check this link Color (Java 2 Platform SE v1.4.2).

If you want this code to work:

Color temp = new Color(foo.getBackground());

foo.getBackground() must return an integer. Since it returns an object Color you have type mismatch.

You can always do:

Color temp = new Color(foo.getbackground().getRGB());

or:

Color color = foo.getBackground();
Color temp = new Color(color.getRed(), color.getGreen(), color.getBlue(),color.getAlpha());
Bruno Simões
A: 

Apparently the type that foo.getBackground() returns is of type "Color".

While you can of course assign a Color to the variable temp of type Color, at least in java.awt.Color there is not constructor to create a Color from another Color.

lutzh