Demonstrates printing images : Print « SWT JFace Eclipse « 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. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
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 » SWT JFace Eclipse » PrintScreenshots 
Demonstrates printing images


//Send questions, comments, bug reports, etc. to the authors:

//Rob Warner (rwarner@interspatial.com)
//Robert Harris (rbrt_harris@yahoo.com)

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.printing.*;
import org.eclipse.swt.widgets.*;

/**
 * This class demonstrates printing images
 */
public class ImagePrinterExample {
  /**
   * The application entry point
   @param args the command line arguments
   */
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display, SWT.NONE);

    try {
      // Prompt the user for an image file
      FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
      String fileName = fileChooser.open();

      if (fileName == null) { return}

      // Load the image
      ImageLoader loader = new ImageLoader();
      ImageData[] imageData = loader.load(fileName);

      if (imageData.length > 0) {
        // Show the Choose Printer dialog
        PrintDialog dialog = new PrintDialog(shell, SWT.NULL);
        PrinterData printerData = dialog.open();

        if (printerData != null) {
          // Create the printer object
          Printer printer = new Printer(printerData);
  
          // Calculate the scale factor between the screen resolution and printer
          // resolution in order to correctly size the image for the printer
          Point screenDPI = display.getDPI();
          Point printerDPI = printer.getDPI();
          int scaleFactor = printerDPI.x / screenDPI.x;
  
          // Determine the bounds of the entire area of the printer
          Rectangle trim = printer.computeTrim(0000);

          // Start the print job
          if (printer.startJob(fileName)) {
            if (printer.startPage()) {
              GC gc = new GC(printer);
              Image printerImage = new Image(printer, imageData[0]);
              
              // Draw the image
              gc.drawImage(printerImage, 00, imageData[0].width,
                imageData[0].height, -trim.x, -trim.y, 
                scaleFactor * imageData[0].width, 
                scaleFactor * imageData[0].height);
  
              // Clean up
              printerImage.dispose();
              gc.dispose();
              printer.endPage();
            }
          }
          // End the job and dispose the printer
          printer.endJob();
          printer.dispose();
        }
      }
    catch (Exception e) {
      MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR);
      messageBox.setMessage("Error printing test image");
      messageBox.open();
    }
  }
}
           
       
Related examples in the same category
1. Print KTable (SWT Table)Example Print KTable (SWT Table)Example
2. Simple PrintSimple Print
3. Printing ExamplePrinting Example
4. ImageViewerImageViewer
5. Demonstrates printing text
6. SWT Print and 2D GraphicsSWT Print and 2D Graphics
7. Print text to printer, with word wrap and paginationPrint text to printer, with word wrap and pagination
8. Print Hello World in black, outlined in red, to default printerPrint Hello World in black, outlined in red, to default printer
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.