BasicPrint.java Source code

Java tutorial

Introduction

Here is the source code for BasicPrint.java

Source

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) {
        }
    }
}