Simple Book for printing : Print « 2D Graphics GUI « Java






Simple Book for printing

Simple Book for printing
    

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.RoundRectangle2D;
import java.awt.print.Book;
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 SimpleBook extends JPanel implements ActionListener {
  final static JButton button = new JButton("Print");

  public SimpleBook() {
    button.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat landscape = job.defaultPage();
    landscape.setOrientation(PageFormat.LANDSCAPE);
    Book bk = new Book();
    bk.append(new DefaultPage(), job.defaultPage());
    bk.append(new PaintContent(), landscape);
    job.setPageable(bk);
    if (job.printDialog()) {
      try {
        job.print();
      } catch (Exception exc) {
      }
    }
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    drawShapes(g2);
  }

  static void drawShapes(Graphics2D g2) {
    g2.fill(new RoundRectangle2D.Double(10, 10, 200, 200, 10, 10));
  }

  public static void main(String[] args) {
    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }

      public void windowClosed(WindowEvent e) {
        System.exit(0);
      }
    };
    JFrame f = new JFrame();
    f.addWindowListener(l);
    JPanel panel = new JPanel();
    panel.add(button);
    f.getContentPane().add(BorderLayout.SOUTH, panel);
    f.getContentPane().add(BorderLayout.CENTER, new SimpleBook());
    f.setSize(775, 450);
    f.show();
  }

}

class DefaultPage implements Printable {
  Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 48);

  public int print(Graphics g, PageFormat pf, int pageIndex)
      throws PrinterException {
    g.setFont(fnt);
    g.setColor(Color.black);
    g.drawString("Sample Shapes", 100, 200);
    return Printable.PAGE_EXISTS;
  }
}

class PaintContent implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex)
      throws PrinterException {
    SimpleBook.drawShapes((Graphics2D) g);
    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.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