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?
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?
That's because foo.getBackground()
returns a Color
instance, and there's no Color
constructor which takes a Color
instance as an argument.
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
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.
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.
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());
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.