Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printer Resolution Independance

115 views
Skip to first unread message

Colin Stamp

unread,
Oct 25, 2001, 3:13:11 AM10/25/01
to
Hi all,

I find myself wanting to print an accurately sized graphic using
java.awt.print, but I'm having trouble getting the size to be
independent of the printer resolution.
I can get the size of the page in various ways, but it always comes
back to me in units of 1/72nd of an inch. When I render the graphic,
it seems to be getting done in printer pixels instead. The resulting
graphic then doubles in size if I set the printer to half the
resolution etc.
What I'm looking for is a way of getting either the printer resolution
or the size of the printable area in pixels. Either one of those would
allow me to set up an AffineTransform to compensate for the
resolution. Using the debugger, I can see the variables I want to
access (They're all in the PrinterJob object - mPrintXres and
mPrintYres for instance) but I can't find methods to access them.
I can't help thinking that I'm doing something fundamentally wrong
here, and I shouldn't need to know the printer resolution, but I can't
work out how the system can compensate automatically...

Cheers,

Colin.

Colin Stamp

unread,
Oct 26, 2001, 1:32:47 PM10/26/01
to

I've finally sorted it out :o)

Just in case anyone's interested, the Print method from the Printable
interface seems to be getting called twice. The first one always has
the AffineTransform in the graphics object set to identity regardless
of the resolution, but the second call is made with the transform
correctly set to give 72 user units per inch on the printer.
I only saw the transform from the first call, since I stopped the
debugger as soon as I'd traced through my code once. I thought an
identity transform wasn't worth keeping, and overwrote it with one of
my own - d'oh.

Cheers,

Colin.

Paul Murray

unread,
Oct 26, 2001, 10:48:10 AM10/26/01
to
Ok. Open up the java docs. Search the index for "resolution". Have a look
under the letter G forst, because that's hwerre all the get methods are.

Say there:

javax.print.attribute.standard.PrinterResolution:
Class PrinterResolution is a printing attribute class that specifies an
exact resolution supported by a printer or to be used for a print job. This
attribute assumes that printers have a small set of device resolutions at
which they can operate rather than a continuum.

Aha! So you have to fetch a "Printer Resolution" object somehow. According
to this, you can get one off a "printer" or off a "print job". Probably via
some sort of "fetch attribute" method.

javax.print.PrintServiceLookup
Implementations of this class provide lookup services for print services
(typically equivalent to printers) of a particular type.

Uhh huh. So a "printer" in javaspeak is called a "PrintService"

Bingo!
public PrintServiceAttribute getAttribute(Class category)

Ooops. Not quite bingo yet. Resolution is not a print service attrivute.
This makes sense, becuse a printer might be capable of several resolution.
Resolutio is an attribute of a print request, so you have to have a request
first ...

public interface DocPrintJob
This interface represents a print job that can print a specified document
with a set of job attributes. An object implementing this interface is
obtained from a print service.

====================

Ok. So, you have a Doc. You have a PrintService. You create a DocPrintJob.
You ask it for its Attribute set. You ask the attribute set for an attribute
of class "javax.print.attribute.standard.PrinterResolution". This attribute
will tell you the printer resolution. You then inform your Doc about the
resolution of the printer it is about to be printed on, so that it knowas
how to size itself.

Hmmm. Bit of a circular dependency here .... let's hope it doesn't matter.

__________________________________________________
Spot the difference!
Jer 48:10 A curse on him who is lax in doing the Lord's work! A curse on him
who keeps his sword from bloodshed!
Mk 16:16 He that believeth and is baptized shall be saved; but he that
believeth not shall be damned.
Quran 2 And kill them wherever you find them, ... if they do fight you, then
slay them; such is the recompense of the unbelievers.
http://www.users.bigpond.com/pmurray .


Colin Stamp

unread,
Oct 27, 2001, 5:30:58 AM10/27/01
to

Thanks for the reply, I thought I was going to have to go down this
kind of route too, but a little voice kept saying "It shouldn't be
this complicated. You shouldn't even need to know the resolution".
The voice turned out to be right. The graphics context passed to the
print function in the printable interface has it's AffineTransform
pre-set to compensate for the resolution and give 72 user units per
inch on the printer. I was missing that before because of my
incompetent use of the debugger. Ho-hum, I know a bit better now.

Cheers,

Colin.

0 new messages