Solved

A quick question

Posted on 1998-08-31
6
185 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 57
array220 challenge 8 47
nestparen challenge 4 56
backtracking recursion  code 19 40
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 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

22 Experts available now in Live!

Get 1:1 Help Now