Re: If with colors




"Bjorn Abelli" <bjorn_abelli@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:f5tffh$d03$1@xxxxxxxxxxx

<bjorn.wallstrom@xxxxxxxxx> 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.

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


.



Relevant Pages