Pagination during print : Print « 2D Graphics « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » 2D Graphics » Print 
16. 50. 10. Pagination during print
/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.UIManager;

public class PaginationExample implements Printable, ActionListener {

  int[] pageBreaks; // array of page break line positions.

  /* Synthesise some sample lines of text */
  String[] textLines;

  private void initTextLines() {
    if (textLines == null) {
      int numLines = 200;
      textLines = new String[numLines];
      for (int i = 0; i < numLines; i++) {
        textLines[i"This is line number " + i;
      }
    }
  }

  public int print(Graphics g, PageFormat pf, int pageIndex)
      throws PrinterException {

    Font font = new Font("Serif", Font.PLAIN, 10);
    FontMetrics metrics = g.getFontMetrics(font);
    int lineHeight = metrics.getHeight();

    if (pageBreaks == null) {
      initTextLines();
      int linesPerPage = (int) (pf.getImageableHeight() / lineHeight);
      int numBreaks = (textLines.length - 1/ linesPerPage;
      pageBreaks = new int[numBreaks];
      for (int b = 0; b < numBreaks; b++) {
        pageBreaks[b(b + 1* linesPerPage;
      }
    }

    if (pageIndex > pageBreaks.length) {
      return NO_SUCH_PAGE;
    }

    /*
     * User (0,0) is typically outside the imageable area, so we must translate
     * by the X and Y values in the PageFormat to avoid clipping Since we are
     * drawing text we
     */
    Graphics2D g2d = (Graphics2Dg;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /*
     * Draw each line that is on this page. Increment 'y' position by lineHeight
     * for each line.
     */
    int y = 0;
    int start = (pageIndex == 0: pageBreaks[pageIndex - 1];
    int end (pageIndex == pageBreaks.length? textLines.length
        : pageBreaks[pageIndex];
    for (int line = start; line < end; line++) {
      y += lineHeight;
      g.drawString(textLines[line]0, y);
    }

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
  }

  public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {
      try {
        job.print();
      catch (PrinterException ex) {
        /* The job did not successfully complete */
      }
    }
  }

  public static void main(String args[]) {

    try {
      String cn = UIManager.getSystemLookAndFeelClassName();
      UIManager.setLookAndFeel(cn)// Use the native L&F
    catch (Exception cnf) {
    }
    JFrame f = new JFrame("Printing Pagination Example");
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    JButton printButton = new JButton("Print Pages");
    printButton.addActionListener(new PaginationExample());
    f.add("Center", printButton);
    f.pack();
    f.setVisible(true);
  }
}
16. 50. Print
16. 50. 1. implements Printable
16. 50. 2. Print an image out
16. 50. 3. javax.print API and allows you to list available printers, query a named printer, print text and image files to a printer, and print to postscript files
16. 50. 4. extends JuliaSet1 and overrides the print() method to demonstrate the Java 1.2 printing API
16. 50. 5. Work with PrintRequestAttributeSet
16. 50. 6. PrinterJob and Printable
16. 50. 7. Print out a Swing component
16. 50. 8. Print Image
16. 50. 9. Printing to a File
16. 50. 10. Pagination during print
16. 50. 11. Printing Pages with Different Formats
16. 50. 12. The area of the printable area
16. 50. 13. The area of the actual page
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.