Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Converting String to Color

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am working on applets,
i am passing a color parameter as shown below

/*
<applet code = "ZoominApplet3.class" width = 300 height = 200>

<param name = "color" value = "Font.RED">
</applet>
*/




fontColor =getParameter("color");


now fontColor is of String type,
i want to convert it to Color type, so that
i can pass it as shown below.

txtA.setBackground(fontColor);


It would be kindful if anyone could tell me how to convert a variable
from String to Color

Regards
Sanam
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The Color class has a static method "decode" that takes a string and returns a Color.

Something like:
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Color does indeed have a "decode" method, and it's what most people would use in this situation. But note that it won't take an argument like "Font.RED" (Did you mean "Color.RED"?) It take hexadecimal color values like "#FFFFFF" (white) and "#FF0000" (red).

If your applet parameters must be symbolic color names like "RED", then your applet will need to contain a table mapping between these and the hexadecimal equivalents.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic