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 / First Aid / June 2007

Tip: Looking for answers? Try searching our database.

If with colors

Thread view: 
bjorn.wallstrom@gmail.com - 27 Jun 2007 11:35 GMT
I try to test what vale a color variable has with

  if (myColor==Color.green)

and even when I know it is true it doesn't work, any idea how I could
solve this?

Thankfull for every answer
Bjorn Abelli - 27 Jun 2007 11:51 GMT
<bjorn.wallstrom@gmail.com> wrote...

> I try to test what vale a color variable has with
>
>   if (myColor==Color.green)
>
> and even when I know it is true it doesn't work, any
> idea how I could solve this?

Just because the instances may have the same "colour", the statement might
still not be "true".

Twins might look the same, but they are still two different people.

Try this instead:

if ( myColor.equals(Color.green) )

That would be true if they contain the same values for rgb & alpha.

/// Bjorn A
Oliver Wong - 27 Jun 2007 17:08 GMT
> <bjorn.wallstrom@gmail.com> wrote...
>
[quoted text clipped - 15 lines]
>
> That would be true if they contain the same values for rgb & alpha.

   If you write it as...

 if (Color.green.equals(myColor))

... you additionally avoid the possibility of a Null Pointer Exception
(null is considered to not be equal to any color, and so the above would
evaluate to false).

   - Oliver
Roedy Green - 28 Jun 2007 14:44 GMT
>I try to test what vale a color variable has with
>
>   if (myColor==Color.green)
>
>and even when I know it is true it doesn't work, any idea how I could
>solve this?

Presuming myColour is of type Color, you should be using

if ( myColor.equals( Color.green ) )

UNLESS you are absolutely sure myColor was initialised with

myColor = Color.green;

rather than say:

myColor = new Color( 0x00ff00 );

You get into similar problems with equals vs == with Strings.
See http://mindprod.com/jgloss/gotchas.html#COMPARISON
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


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



©2008 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.