The Printing code which implements Printable
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JComponent;
public class BasicPrint extends JComponent implements Printable {
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex > 0) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
drawGraphics(g2d, pf);
return Printable.PAGE_EXISTS;
}
public void drawGraphics(Graphics2D g2d,PageFormat pf){
}
public static void main(String[] args) {
PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat pf = pjob.defaultPage();
pjob.setPrintable(new BasicPrint(), pf);
try {
pjob.print();
} catch (PrinterException e) {
}
}
}
Related examples in the same category