Solved

A quick question

Posted on 1998-08-31
6
219 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 236
check java version using powershell 13 291
Running JavaFX on JDeveloper 12C 1 76
jmss example java 2 31
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

761 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