Another print demo : 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 
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.The Printing code which implements Printable
2.Print an Image to print directly
3.Simplest SWT Print ExampleSimplest SWT Print Example
4.Print in Java 2: PrinterJob
5.Print in Java: page format and document
6.Print in Java: Multi page
7.Print in Java 5
8.Print in Java 6
9.Simple Book for printingSimple Book for printing
10.Shapes PrintShapes Print
11.Display the print dialog and print
12.Print the printable area outlinePrint the printable area outline
13.Print the text file and print preview themPrint the text file and print preview them
14.Printable demoPrintable demo
15.Print Swing componentsPrint Swing components
16.BookBook
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.