打印在Java 2 : PrinterJob : 打印 « 图形用户界面 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 图形用户界面 » 打印屏幕截图 
打印在Java 2 : PrinterJob
  

/**
 * Class: Example2
 * <p>
 
 * Print in Java series for the JavaWorld magazine.
 
 @author Jean-Pierre Dube <jpdube@videotron.ca>
 @version 1.0
 @since 1.0
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;

public class JavaWorldPrintExample2 {
  public static void main(String[] args) {

    JavaWorldPrintExample2 example2 = new JavaWorldPrintExample2();
    System.exit(0);
  }

  //--- Private instances declarations
  private final static int POINTS_PER_INCH = 72;

  /**
   * Constructor: Example2
   * <p>
   *  
   */
  public JavaWorldPrintExample2() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
      try {
        printJob.print();
      catch (Exception PrintException) {
        PrintException.printStackTrace();
      }
    }
  }

  /**
   * Class: IntroPage
   * <p>
   
   * This class defines the painter for the cover page by implementing the
   * Printable interface.
   * <p>
   
   @author Jean-Pierre Dube <jpdube@videotron.ca>
   @version 1.0
   @since 1.0
   @see Printable
   */
  private class IntroPage implements Printable {

    /**
     * Method: print
     * <p>
     
     @param g
     *            a value of type Graphics
     @param pageFormat
     *            a value of type PageFormat
     @param page
     *            a value of type int
     @return a value of type int
     */
    public int print(Graphics g, PageFormat pageFormat, int page) {

      //--- Create the Graphics2D object
      Graphics2D g2d = (Graphics2Dg;

      //--- Translate the origin to 0,0 for the top left corner
      g2d.translate(pageFormat.getImageableX(), pageFormat
          .getImageableY());

      //--- Set the default drawing color to black
      g2d.setPaint(Color.black);

      //--- Draw a border arround the page
      Rectangle2D.Double border = new Rectangle2D.Double(00, pageFormat
          .getImageableWidth(), pageFormat.getImageableHeight());
      g2d.draw(border);

      //--- Print the title
      String titleText = "Printing in Java Part 2";
      Font titleFont = new Font("helvetica", Font.BOLD, 36);
      g2d.setFont(titleFont);

      //--- Compute the horizontal center of the page
      FontMetrics fontMetrics = g2d.getFontMetrics();
      double titleX = (pageFormat.getImageableWidth() 2)
          (fontMetrics.stringWidth(titleText2);
      double titleY = * POINTS_PER_INCH;
      g2d.drawString(titleText, (inttitleX, (inttitleY);

      return (PAGE_EXISTS);
    }
  }

  /**
   * Class: Document
   * <p>
   
   * This class is the painter for the document content.
   * <p>
   
   
   @author Jean-Pierre Dube <jpdube@videotron.ca>
   @version 1.0
   @since 1.0
   @see Printable
   */
  private class Document implements Printable {

    /**
     * Method: print
     * <p>
     
     @param g
     *            a value of type Graphics
     @param pageFormat
     *            a value of type PageFormat
     @param page
     *            a value of type int
     @return a value of type int
     */
    public int print(Graphics g, PageFormat pageFormat, int page) {

      //--- Create the Graphics2D object
      Graphics2D g2d = (Graphics2Dg;

      //--- Translate the origin to 0,0 for the top left corner
      g2d.translate(pageFormat.getImageableX(), pageFormat
          .getImageableY());

      //--- Set the drawing color to black
      g2d.setPaint(Color.black);

      //--- Draw a border arround the page using a 12 point border
      g2d.setStroke(new BasicStroke(12));
      Rectangle2D.Double border = new Rectangle2D.Double(00, pageFormat
          .getImageableWidth(), pageFormat.getImageableHeight());

      g2d.draw(border);

      //--- Print the text one inch from the top and laft margins
      g2d.drawString("This the content page", POINTS_PER_INCH,
          POINTS_PER_INCH);

      //--- Validate the page
      return (PAGE_EXISTS);

    }
  }

// Example2


           
         
    
  
Related examples in the same category
1. 印刷代码实现打印
2. 打印图像直接打印
3. 最简单的SWT打印范例最简单的SWT打印范例
4. 打印在Java :网页格式和文件
5. 打印在Java :多页
6. 打印在Java 5
7. 打印在Java 6
8. 简单的图书印刷简单的图书印刷
9. 形状打印形状打印
10. 显示打印对话框并打印
11. 打印打印区域概述打印打印区域概述
12. 打印的文本文件和打印预览打印的文本文件和打印预览
13. 可打印演示可打印演示
14. 打印图形界面组件打印图形界面组件
15. 书
16. 另一个打印演示另一个打印演示
17. 图书打印演示图书打印演示
18. 印刷组合Java 1.2和1.4之路印刷组合Java 1.2和1.4之路
19. 印刷方式Java 1.4
20. 提示打印机
21. 印刷方式Java 1.1印刷方式Java 1.1
22. 涂鸦涂鸦
23. 可打印文件
24. PrintFile -打印文件命名的命令行PrintFile -打印文件命名的命令行
25. 打印到标准输出
26. PrintPanel is the base for an open-ended series of classesPrintPanel is the base for an open-ended series of classes
27. 打印演示:图书打印演示:图书
28. Print Test Print Test
29. 分页文字分页文字
30. 可打印区域
31. 实际可打印区域
32. 打印页的不同格式
33. 设置定位的打印页面
34. 打印对话框:更改默认打印机设置(默认打印机,份数,范围页)
35. 打印到文件
36. 监听打印服务状态变化
37. 打印图片
38. 覆盖的默认操作的JTextComponent
39. 显示页面格式对话框:修改默认的页面格式,如方向和纸张大小。
40. 可打印组件
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.