• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting Paint to Color  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to convert a Paint object to a Color object and can't seem to find any thing specific. I kludged my own method to do it but it seems like the long way around the barn. Is there something more direct than this? TIA.
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, when I try this example:



I do not get any usefull outcome ("java.awt.GradientPaint@816f27d"). How did you test your solution?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no guarantee that a Paint object has a single colour, so this doesn't make much sense.

Here is an example of a custom Paint that produces a red/black output.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paint is an interface. When you call your getColor() method, what is the class of the object you pass as the argument? As Dave Tolls mentioned, it may have many color fields, or it might not have any color fields at all, and toString() might not contain anything useful.

Or it might have a single color field - note that Color implements Paint, so it is possible (though not necessarily likely) that you don't have to do anything; the object might already be a Color instance.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Paint object comes from JFreeChart 'getSeriesPaint(int n)' so I think in this case it is not a method but rather a single color. I couldn't cast Paint to Color so even though that is what it is, it seems like I have to do some kind of conversion. Does that make more sense?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:The Paint object comes from JFreeChart 'getSeriesPaint(int n)' so I think in this case it is not a method but rather a single color. I couldn't cast Paint to Color so even though that is what it is, it seems like I have to do some kind of conversion. Does that make more sense?

Show the line of code where you tried to cast the value returned by getSeriesPaint(0) to java.awt.Color.  Also post any compiler or runtime errors.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, damn! Now it let me. I must have done something wrong when I tried the first time. I'm good now, thanks.
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:The Paint object comes from JFreeChart 'getSeriesPaint(int n)' so I think in this case it is not a method but rather a single color. I couldn't cast Paint to Color so even though that is what it is, it seems like I have to do some kind of conversion. Does that make more sense?

hi Dennis,

I just had a look at JFreeChart, and if I understood it correctly, it is that you first set some Paint with "setSeriesPaint(int series, java.awt.Paint paint)" in one of the implementations of "XYItemRenderer". And the "getxxx" version then returns that Paint. So, in fact you know what Paint is used. If not, then I suggest to use the "instanceof" to find the Paint used. If it is an instance of a multicoloured Paint, decide which Color to use, and if it is a Color, then you're done. Otherwise, well, I have no idea.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!