• Post Reply Bookmark Topic Watch Topic
  • New Topic

Text description of a color  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that toString can be used to get text descriptions of objects, but don't understand how this works in all situations. For instance, if I define a class that has a Color data element, and instantiate it with a value such as Color.blue, can I reasonably create a print statement that will describe the object as "blue", or "Color.blue", or even (0, 0, 255)?

One thing I've done is the following:



That gives me the following:

A blue shape Color = -16776961

so that color has some sort of integer representation.

I can instantiate colors with a statement like:

Color colorBlue = new Color(0, 0, 255);

but will this help in getting a text description of a color?

I've looked for a solution to this, or examples, but haven't been able to find something yet.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dennis,

any object in Java has a toString() method. If you do something like

this is converted to

If you need a color class with a different string representation than the provided one, you have to create your own MyColor class and implement your own to string method.

Jan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!