aspose file tools*
The moose likes Beginning Java and the fly likes Hexadecimal value for new Color() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hexadecimal value for new Color()" Watch "Hexadecimal value for new Color()" New topic
Author

Hexadecimal value for new Color()

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
I was wondering how I could pass a hexadecimal value in a variable into the Color method as a parameter. I tried this:
.... = new Color(request.getParameter("ColorPicked"));
This obviously doesn't work (hence the post). Do I need to cast the value returned from the parameter? The value right now is ffff00. In the case where it's used I'd rather use hexadecimal values rather than specifying 3 integers for red, green, and blue. Help much apprecaited.

Rob
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look through the Integer javadoc for methods to parse a String into an int. I think one of them has just what you need. Let us know if the result works ok with Color.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
Hi Stan,
You mean to convert a hexadecimal such as FFFF00 into an integer and then use that? Is there a way to just pass FFFF00 into the Color method? I looked at some docs for Integer and there is a toHexString but that converts an integer into a hexadecimal string. Even if I can't use the FFFF00 for example in whole I could break it into 3 components (little bit more code) but there doesn't appear to be an invert of the toHexString. Thanks for the help.

Rob
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yes, there's a Color constructor that takes an int and an Integer.parseInt method that lets you specify radix (or base) 16. Integer will parse "FFFF00" into an int 16776960. See if Color comes up yellow-green.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
I'll give it a try I thought there were only 2 constructors 1) hexadecimal and 2) 3 integer acceptance. Thanks.

Rob
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Did you look at the API documentation for java.awt.Color? It has 7 different constructors, not just one or two.

What you need to do is parse the string that contains the hexadecimal number into an int, and then use the constructor Color(int rgb) to create the Color object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hexadecimal value for new Color()
 
Similar Threads
Color setting
Language doubt
wrapper class doubt
Color to hexa
how to convert hexadecimal value in int value