This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Copying JComponent into an Image Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Copying JComponent into an Image" Watch "Copying JComponent into an Image" New topic
Author

Copying JComponent into an Image

Tom McC
Greenhorn

Joined: Jul 28, 2005
Posts: 21
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jul 28, 2005
Posts: 21
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!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Copying JComponent into an Image
 
Similar Threads
ConvolvedOp with / without alpha premultiplication
Images
Save JScrollPane as Image
BufferedImage questions.
Drawing a triangle