Implement Printable to create printable object in Java

Description

The following code shows how to implement Printable to create printable object.

Example


  //from  w  ww. j a v  a 2s  .  c o  m

import java.awt.Color;
import java.awt.Font;
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;

public class Main implements Printable {
  private static Font sFont = new Font("Serif", Font.PLAIN, 64);

  public int print(Graphics g, PageFormat Pf, int pageIndex) throws PrinterException {
    if (pageIndex > 0)
      return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(sFont);
    g2.setPaint(Color.black);
    g2.drawString("Save a tree!", 96, 144);
    return PAGE_EXISTS;
  }

  public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new Main());
    if (job.printDialog()) {
      try {
        job.print();
      } catch (PrinterException e) {
      }
    }
    System.exit(0);
  }
}

The code above generates the following result.

Implement Printable to create printable object in Java




















Home »
  Java Tutorial »
    Graphics »




Animation
BufferedImage
Color
Font
Gradient
Graphics Settings
Image
Mouse Draw
Print
Shape
Text
Transform