Why can't I create a color in Java with the "new" keyword?
I was trying to create a new color in java using
Color temp = new Color(foo.getBackground());
and he kept saying that I couldn't find the symbol.
But it works
Color temp = (foo.getbackground());
Why?
This is because it foo.getBackground()
returns an instance Color
and a no constructor Color
that takes an instance Color
as an argument.
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 a Color object, you have a 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());
Yes you can do that, the problem is maybe foo.getBackground does'nt return an integer or something similar.
Color c = new Color(23,32,43)
works great
There is no constructor for Color that only accepts color. In the second case, you are assigning to a variable that was returned from a function.
The Color class does not have a constructor that takes another Color instance as an argument, and that is what foo.getBackground () returns. IIRC, Java's Color class is immutable - so it just doesn't make sense to create a constructor that makes a copy of an existing Color object.
The type returned by foo.getBackground () appears to be of type "Color".
While you can of course assign a color to a temp variable of type Color, at least there is no constructor in java.awt.Color to create a color from another color.