Printed images in Swing get enlarged

Hi!

I'm currently developing an application in Java 6.0 which uses HTML, and I realised there were differences when printing a page contained in a JTextPane and in FireFox, for example. I experimented a bit and realised the problem was in general printing (not just HTML), and mostly worse with images. They get enlarged. Some fonts get enlarged too, but images are worse.

When I print a test image with 200x127 pixels in Paint, or any other image editor, or in Firefox/IE (I'm using Windows), it gets printed nice and clean, with 5cm width. When I print it in Java, it gets enlarged, quality goes down (because the image is being resized), and it is printed with a width of 7cm.

I tried with various physical and virtual printers with the same result. I tried with different image types and image sizes. What could be causing this?

I'm adding some sample code below for you to see how I'm printing an image. All three attempts give me the same result.

try{

PrinterJob printer = PrinterJob.getPrinterJob();

printer.setJobName("TESTING");

printer.printDialog();

final ImageIcon icon =new ImageIcon("c:/temp/accounting.gif");

Printable printable =new Printable(){

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

if (pageIndex == 0){

graphics.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());

graphics.drawImage(icon.getImage(), 0, 0, 200, 127,null);

graphics.drawImage(icon.getImage(), 0, 200, icon.getIconWidth(), icon.getIconHeight(),null);

icon.paintIcon(null, graphics, 0, 400);

return Printable.PAGE_EXISTS;

}

return Printable.NO_SUCH_PAGE;

}

};

printer.setPrintable(printable);

printer.print();

}catch (Exception e){

e.printStackTrace();

}

I thank for any help in this matter! I've already exhausted my efforts...

Best regards,

Marcos.

[2851 byte] By [Marcos118a] at [2007-11-27 11:39:18]
# 1

For me the problem is java DPI (72) which is less than screen DPI (96 got from default toolkit).

Try to add scale transformation to your graphics.

((Graphics 2D)graphics).scale(0.75, 0.75);

Regards,

Stas

StanislavLa at 2007-7-29 17:25:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks a lot for the suggestion, Stas!

I had already thought of using scale, and the main problem with that is that image quality becomes crappy. What happens is that Java scales the picture down, and the printer scales it up again (or something like that) and I get a lot of lines and other dirt in the printout. Quality is just infinitely better when printing with any other program.

Thanks, and best regards,

Marcos.

Marcos118a at 2007-7-29 17:25:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

The behaviour you describe won't happen. Java doesn't change the image and you don't change the image by the scale. The scale is applied to graphics. But printer DPI is much bigger than screen DPI.

Just try it.

For me it should work.

Regards,

Stas

StanislavLa at 2007-7-29 17:25:19 > top of Java-index,Desktop,Core GUI APIs...
# 4

Sorry, I guess I didn't express myself right.

I did try, and after I read your firt post I did try it again. The size is right, but the quality is not. But perhaps you are right, perhaps the bad quality has nothing to do with the scaling...

Thanks a lot, and best regards,

Marcos.

Marcos118a at 2007-7-29 17:25:19 > top of Java-index,Desktop,Core GUI APIs...
# 5

Additionaly try to play with rendering hints of the graphics.

Set th e KEY_RENDERING to VALUE_RENDER_QUALITY

Regards,

Stas

StanislavLa at 2007-7-29 17:25:19 > top of Java-index,Desktop,Core GUI APIs...
# 6

Hi Stas,

thanks again for the answer. The rendering hints were a very good tip! They didn't do the trick, though... :(

Images are still printed with poor quality. I tried setting rendering quality, played around with anti-alising, interpolation, and basically all I could find in the RenderingHint class. I still get lines and dirt everywhere. Perhaps the java rendering algorithms are just not as good as in other softwares...

Thanks, and best regards,

Marcos.

Marcos118a at 2007-7-29 17:25:19 > top of Java-index,Desktop,Core GUI APIs...