MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JPanel;

public class MainClass extends JPanel {

    public static void main(String[] args) {
        try {
            PrinterJob pjob = PrinterJob.getPrinterJob();
            pjob.setJobName("Graphics Demo Printout");
            pjob.setCopies(1);
            pjob.setPrintable(new Printable() {
                public int print(Graphics pg, PageFormat pf, int pageNum) {
                    if (pageNum > 0) // we only print one page
                        return Printable.NO_SUCH_PAGE; // ie., end of job

                    pg.drawString("www.java2s.com", 10, 10);

                    return Printable.PAGE_EXISTS;
                }
            });

            if (pjob.printDialog() == false) // choose printer
                return;
            pjob.print();
        } catch (PrinterException pe) {
            pe.printStackTrace();
        }
    }

}