Print in Java 6 : Print « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
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
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » PrintScreenshots 
Print in Java 6

/**
 * Class: Example6
 * <p>
 
 * Example on how to print an image.
 * <p>
 
 @author Jean-Pierre Dube <jpdube@videotron.ca>
 @version 1.0
 @since 1.0
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
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;
import java.net.MalformedURLException;
import java.net.URL;

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

    JavaWorldPrintExample6 example = new JavaWorldPrintExample6();
    System.exit(0);
  }

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

  /**
   * Constructor: Example6
   * <p>
   *  
   */
  public JavaWorldPrintExample6() {

    //--- 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, Example 6, Printing an image";
      Font titleFont = new Font("helvetica", Font.BOLD, 18);
      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. In this example, it
   * will print an image of the Nasa Space Station with a border arround it.
   * <p>
   
   
   @author Jean-Pierre Dube <jpdube@videotron.ca>
   @version 1.0
   @since 1.0
   @see Printable
   */
  private class Document extends Component 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(4));
      Rectangle2D.Double border = new Rectangle2D.Double(00, pageFormat
          .getImageableWidth(), pageFormat.getImageableHeight());

      g2d.draw(border);

      //--- Create a media tracker and a URL object
      MediaTracker mt = new MediaTracker(this);
      URL imageURL = null;

      //--- Set the URL to the image that we want to load.
      //--- NOTE: Change the path to reflect your location of the image
      //--- NOTE: Only the following image type are supported JPEG, GIF
      // and PNG.
      try {

        imageURL = new URL(
            "file:///c:/softdev/java/articles/javaworld/printing/part_2/ss2.jpg");
      catch (MalformedURLException me) {
        me.printStackTrace();
      }

      //--- Load the image and wait for it to load
      Image image = Toolkit.getDefaultToolkit().getImage(imageURL);
      mt.addImage(image, 0);
      try {
        mt.waitForID(0);
      catch (InterruptedException e) {
      }

      //--- Render the image on the sheet
      g2d.drawImage(image, (int) (0.25 * POINTS_PER_INCH),
          (int) (0.25 * POINTS_PER_INCH),
          (int) (8.5 * POINTS_PER_INCH)(int) (* POINTS_PER_INCH),
          this);

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

// Example6



           
       
Related examples in the same category
1. Print an Image to print directly
2. Simplest SWT Print ExampleSimplest SWT Print Example
3. Print in Java 2: PrinterJob
4. Print in Java: page format and document
5. Print in Java: Multi page
6. Print in Java 5
7. Simple Book for printingSimple Book for printing
8. Shapes PrintShapes Print
9. Display the print dialog and print
10. Print the printable area outlinePrint the printable area outline
11. Print the text file and print preview themPrint the text file and print preview them
12. Printable demoPrintable demo
13. Print Swing componentsPrint Swing components
14. BookBook
15. Another print demoAnother print demo
16. Book demoBook demo
17. Printing the Combined-Java 1.2-and-1.4 WayPrinting the Combined-Java 1.2-and-1.4 Way
18. Printing the Java 1.4 Way
19. Prompting for a Printer
20. Printing the Java 1.1 WayPrinting the Java 1.1 Way
21. ScribbleScribble
22. Printable Document
23. PrintFile -- Print a file named on the command linePrintFile -- Print a file named on the command line
24. Print to the standard output
25. PrintPanel is the base for an open-ended series of classesPrintPanel is the base for an open-ended series of classes
26. Print Demo: BookPrint Demo: Book
27. Print Test Print Test
28. Pageable TextPageable Text
29. Printable Component
w___w_w.jav___a2_s.___c__o_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.