MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.Font;
import java.awt.Graphics;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.util.Enumeration;
import java.util.Vector;

public class MainClass {
    public static void main(String args[]) throws Exception {
        PrinterJob pj = PrinterJob.getPrinterJob();
        Book book = new Book();
        PageFormat defaultFormat = new PageFormat();
        defaultFormat = pj.defaultPage(defaultFormat);
        PageFormat landscapeFormat = new PageFormat();
        landscapeFormat.setOrientation(PageFormat.LANDSCAPE);
        PagePrinter[] page = new PagePrinter[2];
        int pageWidth = (int) defaultFormat.getImageableWidth();
        int pageHeight = (int) defaultFormat.getImageableHeight();
        Font font = new Font("Helvetica", Font.BOLD, 18);
        page[0] = new PagePrinter();
        page[0].addPrintElement(new MyItem("AAA", font, 100, pageHeight / 2));
        page[0].addPrintElement(new MyItem("line", 0, pageHeight, pageWidth, pageHeight));

        page[1] = new PagePrinter();
        page[1].addPrintElement(new MyItem("rectangle", 100, 100, pageWidth - 200, pageHeight - 200));
        page[1].addPrintElement(new MyItem("oval", 120, 120, pageWidth - 240, pageHeight - 240));

        book.append(page[0], defaultFormat);
        book.append(page[1], landscapeFormat);

        pj.setPageable(book);
        pj.print();
    }
}

class PagePrinter implements Printable {
    Vector pageContents;

    public PagePrinter() {
        pageContents = new Vector();
    }

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
        Enumeration printElements = pageContents.elements();
        while (printElements.hasMoreElements()) {
            MyItem pe = (MyItem) printElements.nextElement();
            pe.print(g);
        }
        return Printable.PAGE_EXISTS;
    }

    public void addPrintElement(MyItem pe) {
        pageContents.addElement(pe);
    }
}

class MyItem {
    static final int TEXT = 1;

    static final int GRAPHICS = 2;

    int type;

    String text;

    Font font;

    String shape;

    int x, y, width, height;

    public MyItem(String text, Font font, int x, int y) {
        type = TEXT;
        this.text = text;
        this.font = font;
        this.x = x;
        this.y = y;
    }

    public MyItem(String shape, int x, int y, int width, int height) {
        type = GRAPHICS;
        this.shape = shape.toUpperCase();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public void print(Graphics g) {
        Font oldFont = g.getFont();
        if (type == TEXT) {
            g.setFont(font);
            g.drawString(text, x, y);
        } else if (type == GRAPHICS) {
            if (shape.equals("LINE")) {
                g.drawLine(x, y, width, height);
            } else if (shape.equals("OVAL")) {
                g.drawOval(x, y, width, height);
            } else if (shape.equals("RECTANGLE")) {
                g.drawRect(x, y, width, height);
            }
        }
        g.setFont(oldFont);
    }
}