The Printing Program - Java 2D Graphics

Java examples for 2D Graphics:Print

Description

The Printing Program

Demo Code

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;

class BasicPrint extends JComponent implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex > 0) {
      return Printable.NO_SUCH_PAGE;
    }//from   w w  w.  j  a v a2s. c  o  m
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    //drawGraphics(g2d, pf);
    return Printable.PAGE_EXISTS;
  }

  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 Tutorials