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.

