Link to home
Start Free TrialLog in
Avatar of pyang051597
pyang051597

asked on

A quick question

Greetings,

How do I convert a string getParameter("white") to a class Color instance?

thanks,
Avatar of callapm
callapm

try identifying the RGB value of a color parameter with the 1.1 method:

public static Color decode(String nm) throws NumberFormatException;

the Color constructor takes an RGB value:

public Color(int r, int g, int b);    or    public Color(int rgb);

-hope this helps
Hmm... how about a long if, else if statement to compar the colors?
There are only about 13 of them.

Avatar of pyang051597

ASKER

Can you give me an example how to use decode(string)  please?

Thanks,

btw, sailwind, I am not going to do a loop 13 times, sorry:)
ASKER CERTIFIED SOLUTION
Avatar of sailwind
sailwind

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!
I found a way to do this:

public static Color convertStringToColor(String aString) {
   try {
      return (Color)Class.forName("java.awt.Color").getField(aString).get(null);
   } catch(Throwable e) {
      System.err.println(e);
      return null;
}

no loop needed!