Solved

A quick question

Posted on 1998-08-31
6
198 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 57
groupSumClump challenge 9 102
starter POM and spring-boot-starter,  spring-boot-web 2 43
factorial example 4 35
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now