Printing the Combined-Java 1.2-and-1.4 Way : Print « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
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
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » 2D Graphics GUI » PrintScreenshots 
Printing the Combined-Java 1.2-and-1.4 Way
Printing the Combined-Java 1.2-and-1.4 Way

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.CubicCurve2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PrintingOneTwoFour {

  static class MyComponent extends JPanel implements Printable {

    public void paint(Graphics g) {
      Graphics2D g2d = (Graphics2Dg;
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          RenderingHints.VALUE_ANTIALIAS_ON);
      g2d.setPaint(Color.BLUE);
      g2d.setStroke(new BasicStroke(3));
      CubicCurve2D cubic = new CubicCurve2D.Float(1080603011013016080);
      g2d.draw(cubic);
    }

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
      if (pageIndex == 0) {
        paint(g);
        return Printable.PAGE_EXISTS;
      else {
        return Printable.NO_SUCH_PAGE;
      }
    }
  }

  public static void main(String args[]) throws Exception {

    final JFrame frame = new JFrame();

    Container contentPane = frame.getContentPane();

    final Component printIt = new MyComponent();
    contentPane.add(printIt, BorderLayout.CENTER);

    JButton button = new JButton("Print");
    contentPane.add(button, BorderLayout.SOUTH);

    ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        PrintService printService = PrintServiceLookup
            .lookupDefaultPrintService();
        DocPrintJob job = printService.createPrintJob();
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(printIt, flavor, das);
        try {
          job.print(doc, pras);
        catch (PrintException pe) {
          pe.printStackTrace();
        }
      }
    };
    button.addActionListener(listener);

    frame.setSize(350350);
    frame.show();
  }
}


           
       
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. 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 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
ww__w___.___j___a_v_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.