import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
/*
* Created on 14-set-2004
*
*/
/**
* @author Laudani
*
*/
public class ProvaPrint implements Printable {
/* (non-Javadoc)
* @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
*/
public int print(Graphics arg0, PageFormat arg1, int arg2)throws PrinterException {
System.out.println(arg2);
if(arg2 == 0) {
arg0.drawString("Prova1",(int)arg1.getImageableX()+10,(int)arg1.getImageableY() + 10);
return PAGE_EXISTS;
} else if(arg2 == 1) {
arg0.drawString("Prova2",(int)arg1.getImageableX()+10,(int)arg1.getImageableY() + 10);
return PAGE_EXISTS;
} else if(arg2 == 2) {
arg0.drawString("Prova3",(int)arg1.getImageableX()+10,(int)arg1.getImageableY() + 10);
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
public static void main(String[] args) {
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
pj.setPrintable(new ProvaPrint(), pf);
try {
if (pj.printDialog())
pj.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
when this class start the print method is called 6 times but it prints 3 pages
why?