package util;

import java.awt.*;
import javax.swing.*;

import java.awt.image.BufferedImage;
import java.awt.print.*;

public class Printer implements Printable {
	
	private BufferedImage image;
	
	public Printer(BufferedImage image) {
		
		this.image = image;
		
		this.print();
	}
	
	public void print() {
		
		PrinterJob printJob = PrinterJob.getPrinterJob();
		printJob.setPrintable(this);
		
		if (printJob.printDialog())
			
			try {
				printJob.print();
			}
			
			catch (PrinterException pe) {
				JOptionPane.showMessageDialog(null, "Error in printing !!! " + pe);
			}
	}
	
	public int print(Graphics g, PageFormat pf, int pageIndex) {
		
		if (pageIndex > 0) {
			return (NO_SUCH_PAGE);
		}
		
		else {
			
			Graphics2D g2d = (Graphics2D) image.getGraphics();
			g2d.translate(pf.getImageableX(), pf.getImageableY());
			
			return (PAGE_EXISTS);
		}
	}
}