jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Using PrinterJob to print graphic? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using PrinterJob to print graphic?" Watch "Using PrinterJob to print graphic?" New topic

Using PrinterJob to print graphic?

Janus Engstrøm
Ranch Hand

Joined: Nov 01, 2006
Posts: 44
Hello all!

I'm working on an application where I create a graph. The class that paints the graph is defined as following:

I override paintComponent(Graphics g) to do the painting. The actual graph consists of lines (g.drawLine(fromX, fromY, toX, toY)) and some homemade JComponents (myComponent.setBounds(x, y, a, b)) such as small squares and circles. I use these JComponents because I want Tool Tips on them.

On the screen all looks great!

Here's how I print:

But when I try to print it, the outcome on paper doesn't look that great:

All my homemade JComponents are stacked in upper left corner (coords 0, 0) instead of being placed correctly I'm aware that PrinterJob takes the Graphics object, does some transformations and then pipes it to the printer, but why are my components parintet but their coordinates ignored?

What to do .. what to do .. pretty annoying, actually

With kind regards!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I know zip about printing, but Tom Jacobs' StandardPrint class works well
for printing components. Might not suit, but worth a look


from memory, you have to comment out the references to SpecialPrint class
I agree. Here's the link: http://aspose.com/file-tools
subject: Using PrinterJob to print graphic?
It's not a secret anymore!