<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
>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