Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I create a Color in Java with "new" keyword?

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?

like image 739
kokokok Avatar asked Dec 18 '25 02:12

kokokok


2 Answers

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

like image 89
Vinay Sajip Avatar answered Dec 20 '25 18:12

Vinay Sajip


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());
like image 21
Bruno Simões Avatar answered Dec 20 '25 18:12

Bruno Simões



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!