Simplest SWT Print Example : Print « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » PrintScreenshots 
Simplest SWT Print Example
Simplest SWT Print Example
    
/**
 * Class: Example1
 * <p>
 
 @author Jean-Pierre Dube <jpdube@videotron.ca>
 @version 1.0
 @since 1.0
 @see Printable
 */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;

public class JavaWorldPrintExample1 implements Printable {
  public static void main(String[] args) {

    JavaWorldPrintExample1 example1 = new JavaWorldPrintExample1();
    System.exit(0);
  }

  //--- Private instances declarations
  private final double INCH = 72;

  /**
   * Constructor: Example1
   * <p>
   *  
   */
  public JavaWorldPrintExample1() {

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

    //--- Set the printable class to this one since we
    //--- are implementing the Printable interface
    printJob.setPrintable(this);

    //--- Show a print dialog to the user. If the user
    //--- click the print button, then print otherwise
    //--- cancel the print job
    if (printJob.printDialog()) {
      try {
        printJob.print();
      catch (Exception PrintException) {
        PrintException.printStackTrace();
      }
    }

  }

  /**
   * Method: print
   * <p>
   
   * This class is responsible for rendering a page using the provided
   * parameters. The result will be a grid where each cell will be half an
   * inch by half an inch.
   
   @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) {

    int i;
    Graphics2D g2d;
    Line2D.Double line = new Line2D.Double();

    //--- Validate the page number, we only print the first page
    if (page == 0) {

      //--- Create a graphic2D object a set the default parameters
      g2d = (Graphics2Dg;
      g2d.setColor(Color.black);

      //--- Translate the origin to be (0,0)
      g2d.translate(pageFormat.getImageableX(), pageFormat
          .getImageableY());

      //--- Print the vertical lines
      for (i = 0; i < pageFormat.getWidth(); i += INCH / 2) {
        line.setLine(i, 0, i, pageFormat.getHeight());
        g2d.draw(line);
      }

      //--- Print the horizontal lines
      for (i = 0; i < pageFormat.getHeight(); i += INCH / 2) {
        line.setLine(0, i, pageFormat.getWidth(), i);
        g2d.draw(line);
      }

      return (PAGE_EXISTS);
    else
      return (NO_SUCH_PAGE);
  }

//Example1




           
         
    
    
    
  
Related examples in the same category
1.The Printing code which implements Printable
2.Print an Image to print directly
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.Print in Java 6
8.Simple Book for printingSimple Book for printing
9.Shapes PrintShapes Print
10.Display the print dialog and print
11.Print the printable area outlinePrint the printable area outline
12.Print the text file and print preview themPrint the text file and print preview them
13.Printable demoPrintable demo
14.Print Swing componentsPrint Swing components
15.BookBook
16.Another print demoAnother print demo
17.Book demoBook demo
18.Printing the Combined-Java 1.2-and-1.4 WayPrinting the Combined-Java 1.2-and-1.4 Way
19.Printing the Java 1.4 Way
20.Prompting for a Printer
21.Printing the Java 1.1 WayPrinting the Java 1.1 Way
22.ScribbleScribble
23.Printable Document
24.PrintFile -- Print a file named on the command linePrintFile -- Print a file named on the command line
25.Print to the standard output
26.PrintPanel is the base for an open-ended series of classesPrintPanel is the base for an open-ended series of classes
27.Pageable TextPageable Text
28.The area of the printable area
29.The area of the actual page
30.Printing Pages with Different Formats
31.Setting the Orientation of a Printed Page
32.Print Dialog: change the default printer settings(default printer, number of copies, range of pages)
33.Printing to a File
34.Listening for Print Service Status Changes
35.Print Image
36.Overriding the Default Action of a JTextComponent
37.Displaying the Page Format Dialog: changes the default page format such as orientation and paper size.
38.Printable Component
39.Create PageFormats on a higher level
40.Printing of a multi-page bookPrinting of a multi-page book
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.