The Artima Developer Community
Sponsored Link

Java Answers Forum
Convert String to Color

5 replies on 1 page. Most recent reply: Mar 1, 2002 2:38 PM by Roger

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Roger

Posts: 3
Nickname: roger
Registered: Mar, 2002

Convert String to Color Posted: Mar 1, 2002 1:18 PM
Reply to this message Reply
Advertisement
I am using a command named stringToColor which I believe is created in the
import java.awt.*; package

My code is a follows but I get an error
Any Ideas would be greatly appriciated
class PieItem {
public double frac; // each one has a number
public String label; // a label
public Color wedgeColor; // and a Color

PieItem (String s) { // constructor
StringTokenizer t = new StringTokenizer(s, ",");
frac = Double.valueOf(t.nextToken()).doubleValue();
label = t.nextToken();
wedgeColor=toColor(t.nextToken());
System.out.println(frac);
System.out.println(label);
System.out.println(wedgeColor);

} // constructor

} // PieItem

and the test line is like this

<param name=arg0 value="2,Mice,Red">

The values are read in as strings but the error I get is

C:\webJava\Pie.java:26: cannot resolve symbol
symbol : method toColor (java.lang.String)
location: class PieItem
wedgeColor=toColor(t.nextToken());

Thanks in advance.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Convert String to Color Posted: Mar 1, 2002 1:29 PM
Reply to this message Reply
Looks like stringToColor() is a method of
javax.swing.text.html.StyleSheet
according to my documentation. However, you are calling toColor(), which doesn't appear in the Java API, as far as I can see and since you are calling it in your own class which doesn't define it, that is why the compiler is displeased.

By the way, don't forget to use the [java] and [/java] tags in your post to get nicely-formatted code (look under the Formatting Your Post heading at the bottom of the page, when you are posting, or go to the "How to Post in the Artima Forums" page at http://www.artima.com/forums/howtopost.html).

Roger

Posts: 3
Nickname: roger
Registered: Mar, 2002

Re: Convert String to Color Posted: Mar 1, 2002 1:36 PM
Reply to this message Reply
My apologies about the ugly posting. I have changed the piece of the code from stringToColor and back several times. I am looking for a solution to take a parameter in as a string and set it to a color object so the pie chart can draw correctly. Looks like I have to come up with another method to acomplish this

If you have any, It would be greatly appriciated

Roger

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Convert String to Color Posted: Mar 1, 2002 2:26 PM
Reply to this message Reply
Well, as I said, the class javax.swing.text.html.StyleSheet has that method. I haven't used it, but it appears to do what you want (returns a Color object, given a string like "RED"). I think it should do the trick and it would be pretty easy to write. Here is a simple test which shows it working (try it with basic colors for which you know the rgb values):
import javax.swing.text.html.StyleSheet;
import java.awt.Color;
 
public class ColorTest
{
   public static void main( String[] args )
   {
      StyleSheet s = new StyleSheet();
 
      String str = "red";
      if( args.length > 0 )
         str = args[0];
 
      Color c = s.stringToColor(str);
 
      System.out.println( "The color '" + str + "' is: " + c.toString() );
   }
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Convert String to Color Posted: Mar 1, 2002 2:35 PM
Reply to this message Reply
A couple more notes:
- The StyleSheet's stringToColor() method will return null if it doesn't know the color (look at the documentation and you'll see it only supports colornames specified in HTML3.2, which is a small subset of what the modern browsers support).

- Where I said "pretty easy to write" I should have said "pretty easy to use" (I was originally writing that it would also be pretty easy to write your own class that would convert names to Colors -- if StyleSheet doesn't fill the bill, you could do this (I'd first call the strinToColor() method of StyleSheet and if didn't yield any result, look up my own table of names/RGB from a properties file or database)).

Roger

Posts: 3
Nickname: roger
Registered: Mar, 2002

Re: Convert String to Color Posted: Mar 1, 2002 2:38 PM
Reply to this message Reply
Thanks,
That seems to work well in my application. I have seen it all over and did not realize it was a style sheet. Seems that everywhere I was looking had too little information on it.

Roger

Flat View: This topic has 5 replies on 1 page
Topic: How To Post Java Code in the Forum Previous Topic   Next Topic Topic: Discrete Event Simulation Code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use