Hi,
I was wondering, if there was any way to identify whether two color
values are same.?
Example:
I have a color object 'val'
System.out.println(val) ----> java.awt.Color[r=174,g=36,b=25]
If I need to compare it with another color object... I do the following
Color val1 = new Color(240, 74, 36);
if (val1 == val) {
System.out.println("Same color");
}
This doesnot seem to work ... I am an amateur programmer, Any help
would be appreciated
Thanks
km
kmextra@gmail.com - 25 Feb 2006 04:03 GMT
Sorry .. by mistake I typed in different color values in the earlier
msg
Color val1 = new Color (174, 36, 25);
Then I should get the output as 'Same Color'... but no it doesnot enter
the 'if' condition..shown above.
opalpa@gmail.com opalinski from opalpaweb - 25 Feb 2006 04:12 GMT
val1.equals(val)
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
James McGill - 25 Feb 2006 06:06 GMT
> Sorry .. by mistake I typed in different color values in the earlier
> msg
[quoted text clipped - 3 lines]
> Then I should get the output as 'Same Color'... but no it doesnot enter
> the 'if' condition..shown above.
What you need to do next is look into the difference between overriding
the "equals()" method to compare object values, versus using "==" to
compare object identities. The "==" operator doesn't do what you seem
to think it does in your example.
You might also look at the Comparable interface, if your model has the
concept of ordering colors (e.g., red < yellow < blue)
Luc The Perverse - 25 Feb 2006 05:48 GMT
> Hi,
>
[quoted text clipped - 14 lines]
> This doesnot seem to work ... I am an amateur programmer, Any help
> would be appreciated
Remember Java has no operator overloading.
The onlything val1==val can tell you is if the two items are the same
object.
opalpa . . . replied and told you how to compare them correctly.
--
LTP
:)
Roedy Green - 25 Feb 2006 09:46 GMT
>if (val1 == val) {
> System.out.println("Same color");
the first thing to do is look at the equals method to see if it does
what you want already.
Turns out you are in luck. Here is how it is defined.
public boolean equals(Object obj) {
return obj instanceof Color && ((Color)obj).value ==
this.value;
so you can say if ( c1.equals( c2 ) )
If Sun had not provide that method, you could have done:
if ( c1.getRGB() == c2.getRGB() )
getRGB is a mismomer. I actually returns argb with 8-bits of alpha
channel.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Luc The Perverse - 25 Feb 2006 18:25 GMT
> if ( c1.getRGB() == c2.getRGB() )
>
> getRGB is a mismomer. I actually returns argb with 8-bits of alpha
> channel.
The real secret is that getRGB is returning a primitive, not an object :)
--
LRP
:)
km - 25 Feb 2006 22:22 GMT
It worked .. :)
Thanks a bunch for the quick feedback
km