Solved

A quick question

Posted on 1998-08-31
6
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

623 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