Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Compare color values

Thread view: 
kmextra@gmail.com - 25 Feb 2006 03:57 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.