Another print demo : 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 
Another print demo
Another print demo


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
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.JPanel;

public class AnotherPrintDemo extends JFrame {
  DrawingCanvas canvas;

  JButton setUpButton = new JButton("Page Setup");

  JButton printButton = new JButton("Print");

  JButton cancelButton = new JButton("Cancel");

  public AnotherPrintDemo() {
    super();
    Container container = getContentPane();

    canvas = new DrawingCanvas();
    container.add(canvas);

    JPanel panel = new JPanel(new GridLayout(13));

    ButtonListener buttonListener = new ButtonListener();
    setUpButton.addActionListener(buttonListener);
    panel.add(setUpButton);

    printButton.addActionListener(buttonListener);
    panel.add(printButton);

    cancelButton.addActionListener(buttonListener);
    panel.add(cancelButton);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    setSize(650275);
    setVisible(true);
  }

  class ButtonListener implements ActionListener {
    PrinterJob printJob;

    PageFormat pageFormat;

    PrintableCanvas printableCanvas;

    ButtonListener() {
      printJob = PrinterJob.getPrinterJob();
      pageFormat = printJob.defaultPage();
    }

    public void actionPerformed(ActionEvent e) {
      JButton tempButton = (JButtone.getSource();

      if (tempButton.equals(setUpButton)) {
        pageFormat = printJob.pageDialog(pageFormat);
        printJob.validatePage(pageFormat);
      else if (tempButton.equals(printButton)) {
        printableCanvas = new PrintableCanvas(pageFormat);
        printJob.setPrintable(printableCanvas);

        boolean ok = printJob.printDialog();
        if (ok) {
          try {
            printJob.print();
          catch (Exception pe) {
            System.out.println("Printing Exception Occured!");
            pe.printStackTrace();
          }
        }
      else if (tempButton.equals(cancelButton)) {
        printJob.cancel();
      }
    }
  }

  public static void main(String arg[]) {
    new AnotherPrintDemo();
  }
}

class DrawingCanvas extends JPanel {
  Font font;

  FontMetrics fontMetrics;

  int w, h;

  DrawingCanvas() {
    setBackground(Color.white);
    setSize(400275);

    w = this.getWidth();
    h = this.getHeight();

    font = new Font("Dialog", Font.BOLD, 50);
    fontMetrics = getFontMetrics(font);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g)
    Graphics2D g2D = (Graphics2Dg;

    paintContent(g2D, w, h);
  }

  public void paintContent(Graphics2D g2D, int w, int h) {
    g2D.setFont(font);

      g2D.drawString("Java Source and Support"0,
        (float) (0.5 * h - 1.25 * fontMetrics.getHeight()));

  }
}

class PrintableCanvas implements Printable {
  DrawingCanvas canvas;

  PageFormat pageFormat;

  public PrintableCanvas(PageFormat pf) {
    pageFormat = pf;
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex)
      throws PrinterException {
    if (pageIndex >= 1) {
      return Printable.NO_SUCH_PAGE;
    }

    Graphics2D g2D = (Graphics2Dg;

    canvas = new DrawingCanvas();

    canvas.paintContent(g2D, (intpageFormat.getImageableWidth(),
        (intpageFormat.getImageableHeight());

    // successful printing of the page
    return Printable.PAGE_EXISTS;
  }
}



           
       
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. 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_.j_a___v__a___2___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.