Printing to fit one page

Found an example in some of the code but it ended up killing about 10 trees… ended up figuring out that they were not taking the resolutions of the devices into account.

Here is the solution I came up with:

public double getPrintScale(Graphics2D g2, Printer pf ) {
Rectangle pageRect = getPrintPageRect(g2, pf);
Dimension docSize = getPrintDocumentSize();

     Point printerDPI = pf.getDPI();
     Point screenDPI = getDisplay().getDPI();

     // make sure it doesn't get scaled too much! (especially if no objects in document)
     docSize.width = Math.max(docSize.width, 50);
     docSize.height = Math.max(docSize.height, 50);
     double hratio = (pageRect.width/printerDPI.x) / ((double)docSize.width/screenDPI.x);
     double vratio = (pageRect.height/printerDPI.y) / ((double)docSize.height/screenDPI.y);
     System.err.println( "  ratio: " + Math.min(hratio, vratio) );
     return Math.min(hratio, vratio);


Thanks for noticing that problem with the code in the sample.

Alas, I just created the 5.2 kits, so that fix won't make it into the kit. I suppose it doesn't matter too much since that code was commented out anyway.