• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Color Parameter

 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to ask you following question:
I have one HTML file in which I have used Param Value = "blue" .
Now in Applet, I will store the Param Value into one string called String s.
In Applet,I have color object called Color c and Label Object label. Now I want to set the Background color of the label object.
So can I used:
label.setBackground(Color.s) where s is the string which has param value of html. OR Is there any other alternate way through with I can get the color value from HTML and set it to color object.
Please let me know,
Thanks
Angela
 
Arun, Shanmugham
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think only way is to have a switch for the color and set the background color of the label to Color.<color name>. Something like
switch(s.substring(0,1))
{
s='b': l.setBackground(Color.blue);
}

Arun
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun,
Unfortunately, your solution would ignore Color.black ( One of my favourite colors for web backgrounds! )... This is due to the fact the switch() statements can only take arguments that are comparable to int... so you have to use chars instead of Strings... Strings could be used if you used cascading if..else statements... but that may get a little messy... my solution would be to pass the color into the applet as RGB values and then use these to set the color... This requires you to have parameters going into your applet like BackgroundR, BackgroundG, and BackgroundB for each color you want to use, but I think it is an easier solution to code and makes the applet more modifiable...

HTH,
-Nate
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use reflection to set up a Map which maps color names to Color objects?

As you see in the main() method, a Color may be accessed via colorMap.get(colorName). Of course you'll want to check if this is null, indicating the color name does not match an existing color.
[This message has been edited by Jim Yingst (edited April 18, 2001).]
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch!
Angela
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic