Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Mac OS X - Printing - PrinterGraphicsConfig.getDefaultTransform() throws a NullPointerException

shrudaJul 15 2014 — edited Jul 22 2014

Hello guys, I currently try to get our Swing application working on Mac OS X and I found one problem which blocks me. Our application usese the java.awt.print package to generate print-outs.

One very important information for us is the printers used DPI/PPI setting. We've found a long time ago a tutorial which uses the given Graphics object of the java.awt.Printable.print(Graphics, PageFormat, int) method to get this neseccary information.

The implementation of our looks like the following:

/**

* @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)

*/

@Override

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException

{

  if (pageIndex != 0)

  return Printable.NO_SUCH_PAGE;

  Graphics2D graphics2D = (Graphics2D) graphics;

  // get the current scale

  AffineTransform defaultDeviceTransform = graphics2D.getDeviceConfiguration().getDefaultTransform();

  double deviceScaleX = defaultDeviceTransform.getScaleX();

  double deviceScaleY = defaultDeviceTransform.getScaleY();

  /*

  * this method gets an graphics from the printer which is already scaled to the desired resolution and therefore

  * the dpi can be calculated by the scale of the transformation.

  * -> no other way to determine the dpi were found

  */

  double dpi = deviceScaleX * 72;

  System.out.println("The Printer uses the following DPI setting: " + dpi);

  myPrintingMethod(graphics2D, dpi);

  return Printable.PAGE_EXISTS;

}

This way to get the DPI/PPI setting of the printer works perfect if the operating system is Microsoft Windows. But in case of the Mac OS X throws the Line 13. the following NullPointerException:

java.lang.NullPointerException

at java.awt.geom.AffineTransform.<init>(AffineTransform.java:488)

at sun.print.PrinterGraphicsConfig.getDefaultTransform(PrinterGraphicsConfig.java:101)

at com.intergraph.web.plugin.printing.PrintableDocument.print(PrintableDocument.java:122)

at sun.lwawt.macosx.CPrinterJob$6.run(CPrinterJob.java:697)

at sun.lwawt.macosx.CPrinterJob.printAndGetPageFormatArea(CPrinterJob.java:707)

at sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)

at sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:299)

at com.intergraph.web.plugin.printing.PrintEngine.print(PrintEngine.java:177)

at com.intergraph.web.plugin.printing.controller.PrintDocumentJob.run(PrintDocumentJob.java:41)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:745)

I tried to find a workaround but I wasn't sucessful and I only found the following thread from 2008 Simple Printing example that raises many questions .... the result of this thread is not the same but it describes the same problem...

Does anybody of you know a way to figure out the DPI/PPI setting of a printer?

I'm tested it with:

JDK 8u5

OS X 10.9.4

Thanks in advance!

Best Regards,

Steve

Comments

Peter K
Hi Dude,

Thanks for the info. I'm running 10.10, but have held back on upgrading to 11.04.
have you ever applied patchset 11.202 on Ubuntu (either 10.10 or 11.04)?
Dude!
Oracle 11g 11.2.0.2 is a full installation and not a patch set anymore.

I have tested an Out-of-Place Upgrade of the existing 11.2.0.1 database to 11.2.0.2 on the above system without problems. Just select the "ignore all" button at the prerequisites check. It worked fine, including the Network Configuration and Database Upgrade Assistant. If you go straight to 11.2.0.2 you may not need to install any 32-bit libraries, but I didn't try it.

I have updated the instructions.
Peter K
thanks, will try it out on 10.10
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 19 2014
Added on Jul 15 2014
2 comments
1,601 views