Java Tutorial/2D Graphics/PrinterJob — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 15:25, 31 мая 2010

Print Book

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





Printing setting

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PrinterSettingUpDialogPrint extends JFrame {
  DrawingCanvas canvas= new DrawingCanvas();
  JButton setUpButton= new JButton("Page Setup"), printButton= new JButton("Print"), cancelButton= new JButton("Cancel");
  PrinterJob printJob = PrinterJob.getPrinterJob();
  PageFormat pageFormat = printJob.defaultPage();
  PrintableCanvas printableCanvas;
  public PrinterSettingUpDialogPrint() {
    getContentPane().add(canvas);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 3));
    setUpButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent a){
        setup();
      }
    });
    panel.add(setUpButton);
    printButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent a){
        print();
      }
    });
    panel.add(printButton);
    cancelButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent a){
        cancel();
      }
    });
    panel.add(cancelButton);
    getContentPane().add(BorderLayout.SOUTH, panel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 275);
    setVisible(true);
  }
  private void setup() {
    pageFormat = printJob.pageDialog(pageFormat);
    printJob.validatePage(pageFormat);
  }
  private void print() {
    printableCanvas = new PrintableCanvas(pageFormat);
    printJob.setPrintable(printableCanvas);
    boolean ok = printJob.printDialog();
    if (ok) {
      try {
        printJob.print();
      } catch (Exception pe) {
        System.out.println("Printing Exception Occured!");
        pe.printStackTrace();
      }
    }
  }
  private void cancel() {
    printJob.cancel();
  }
  public static void main(String arg[]) {
    new PrinterSettingUpDialogPrint();
  }
}
class DrawingCanvas extends JPanel {
  Font font = new Font("Dialog", Font.BOLD, 50);
  FontMetrics fontMetrics;
  int w = 400, h = 275;
  DrawingCanvas() {
    setBackground(Color.white);
    setSize(400, 275);
    fontMetrics = getFontMetrics(font);
  }
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2D) g;
    paintContent(g2D, w, h);
  }
  public void paintContent(Graphics2D g2D, int w, int h) {
    g2D.setFont(font);
    g2D.drawString("Java 2D", (float) (0.5 * (w - fontMetrics
        .stringWidth("Java 2D"))), (float) (0.5 * h - 1.25 * fontMetrics
        .getHeight()));
  }
}
class PrintableCanvas implements Printable {
  DrawingCanvas canvas;
  PageFormat pageFormat;
  public PrintableCanvas(PageFormat pf) {
    pageFormat = pf;
  }
  public int print(Graphics g, PageFormat pageFormat, int pageIndex)
      throws PrinterException {
    if (pageIndex >= 1) {
      return Printable.NO_SUCH_PAGE;
    }
    Graphics2D g2D = (Graphics2D) g;
    canvas = new DrawingCanvas();
    canvas.paintContent(g2D, (int) pageFormat.getImageableWidth(),
        (int) pageFormat.getImageableHeight());
    return Printable.PAGE_EXISTS;
  }
}