Solved

A quick question

Posted on 1998-08-31
6
208 Views
Last Modified: 2010-03-30
Greetings,

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

thanks,
0
Comment
Question by:pyang051597
  • 3
  • 2
6 Comments
 

Expert Comment

by:callapm
ID: 1223051
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
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1223052
Hmm... how about a long if, else if statement to compar the colors?
There are only about 13 of them.

0
 

Author Comment

by:pyang051597
ID: 1223053
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:)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Accepted Solution

by:
sailwind earned 20 total points
ID: 1223054
Hello,

     I thought about this issue for a while, and frankly I don't think there's a way to
do it. Given a string, you can get the class that is referenced by that name, but
you can't directly access the static variable with the string. The color.White
static variable simply cannot be referenced by color."white". Below is a small
method that I wrote which will give you the color that you specify with a string.
You can just call this method for every getparameter that you use, so you can do:

you can call it like:

Color myColor = convertColor(getParameter("colorChoice"));

----

public Color convertColor(String s) {
  if(s.equals("black"))
    return Color.white;
  else if(s.equals("blue"))
    return Color.blue;
  else if(s.equals("cyan"))
    return Color.cyan;
  else if(s.equals("darkGray"))
    return Color.darkGray;
  else if(s.equals("gray"))
    return Color.gray;
  else if(s.equals("green"))
    return Color.green;
  else if(s.equals("lightGray"))
    return Color.lightGray;
  else if(s.equals("magenta"))
    return Color.magenta;
  else if(s.equals("orange"))
    return Color.orange;
  else if(s.equals("pink"))
    return Color.pink;
  else if(s.equals("red"))
    return Color.red;
  else if(s.equals("white"))
    return Color.white;
  else if(s.equals("yellow"))
    return Color.yellow;
  else
    return null;
}
0
 

Author Comment

by:pyang051597
ID: 1223055
Thank you!
0
 

Author Comment

by:pyang051597
ID: 1223056
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!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 45
how to add new optional parameter to JSP 1 42
arguments to jar 5 26
SHA2 certs for IIS AND Java? 2 93
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question