• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting Paint to Color

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic