Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Copying JComponent into an Image  RSS feed

 
Tom McC
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have a couple of charting classes that I want to be able to include in PDF reports created by my program. The classes all extend JComponent. My problem is that when I convert the Components to images, I always get a black image.

I'm using this code that I pieced together from various places:


Now, I've tried adding the charting component to a JPanel and then trying to print that component onto the image, and I am able to see the JPanel's default background color, but no sign of the charting component.

I've also tried using a few different variations of Buffered Image types (other than TYPE_INT_RGB), but felt I was getting nowhere. Is there something here I'm missing?

(And PS: What's the difference between print() and paint() for a Component?)

Thanks!
Tom
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you'll find a couple of links here (code that does what you describe)
the link of camickr's is the better, but the other one is worth a look as well

http://forum.java.sun.com/thread.jspa?threadID=604040
 
Tom McC
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael, the code on that page (ScreenImage.java) is very handy; I'm saving that one.

Unforunately, it still didn't work for these charting components. I ended up browsing through the code (which has been put through an obfuscator) and finding a render(Graphics) method which did the trick. How odd that even though the charts extend JComponent and show up in a program fine, they refuse to draw onto an image, something other JComponents did fine.

Thanks so much for the prompt reply and useful link, however!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!