This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Color setting  RSS feed

 
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to use 1 value to set the color of a Color object in Java? I want to pass 1 value in to set the Color. What's the easiest way to do this? Thanks.
 
Sheriff
Posts: 23697
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the Color class does have a constructor with one parameter. (public Color(int rgb).) So if your goal is to pass in one object, that would be the easiest way to do it.

Easiest at least for the code you would have to write, anyway. It delegates the complexities of building that one object to somewhere else; I don't know if that's a concern for you.
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. You happen to have a good link how how to convert a value to the object expected? I'm guessing it isn't as easy as passing in a hexadecimal value? :-)
 
Marshal
Posts: 60098
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Color class constructor.

It is in fact as easy as passing a hex number.

Color red = new Color(0x00ff0000);
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool thanks.
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hexadecimal way is great but here's what I'm trying to do. I need to load a value from a textfile and use that value to set the color of objects on an applet. What's the best/easiest format to store within a text file and convert it (if need be) to something that can be used to set a color? Appreciate any help on this. Thanks again.
 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The nextInt() method of Scanner is probably the easiest way to extract an int from a text file.
 
Campbell Ritchie
Marshal
Posts: 60098
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use three ints in the range 0...255 (0...0xff) inclusive, or four floats in the range 0f...1.0f inclusive, and the three-arguments or four-arguments constructor of Color.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!