Book demo : Print « 2D Graphics GUI « Java

Home
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 » 2D Graphics GUI » PrintScreenshots 
Book demo
Book demo
    

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
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 java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

public class BookDemo extends JFrame {
  DrawingCanvas canvas;

  JRadioButton portraitButton = new JRadioButton("Portrait"true);

  JRadioButton landscapeButton = new JRadioButton("Landscape");

  JRadioButton rLandscapeButton = new JRadioButton("Reverse Landscape");

  JButton addButton = new JButton("Add to Book");

  JButton printButton = new JButton("Print");

  JButton clearButton = new JButton("Clear");

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

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

    JPanel panel = new JPanel(new GridLayout(26));
    container.add(panel, BorderLayout.SOUTH);

    ButtonGroup gp = new ButtonGroup();
    gp.add(portraitButton);
    gp.add(landscapeButton);
    gp.add(rLandscapeButton);

    ActionListener buttonListener = new ButtonListener();
    portraitButton.addActionListener(buttonListener);
    landscapeButton.addActionListener(buttonListener);
    rLandscapeButton.addActionListener(buttonListener);

    panel.add(portraitButton);
    panel.add(landscapeButton);
    panel.add(rLandscapeButton);

    addButton.addActionListener(buttonListener);
    printButton.addActionListener(buttonListener);
    clearButton.addActionListener(buttonListener);

    panel.add(addButton);
    panel.add(printButton);
    panel.add(clearButton);

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

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

  class ButtonListener implements ActionListener {
    PrinterJob jobControl;

    PageFormat pageFormat;

    Book book;

    ButtonListener() {
      jobControl = PrinterJob.getPrinterJob();
      pageFormat = jobControl.defaultPage();
      book = new Book();
    }

    public void actionPerformed(ActionEvent e) {
      Object obj = e.getSource();

      if (obj instanceof JRadioButton) {
        JRadioButton tempButton = (JRadioButtonobj;

        if (tempButton.equals(portraitButton)) {
          pageFormat.setOrientation(PageFormat.PORTRAIT);
        }

        else if (tempButton.equals(landscapeButton)) {
          pageFormat.setOrientation(PageFormat.LANDSCAPE);
        else if (tempButton.equals(rLandscapeButton)) {
          pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
        }
      else if (obj instanceof JButton) {
        JButton tempButton = (JButtonobj;

        if (tempButton.equals(addButton)) {
          book.append(new PrintableCanvas(canvas.getShapesVector()),
              (PageFormatpageFormat.clone());
        else if (tempButton.equals(printButton)) {
          jobControl.setPageable(book);

          if (jobControl.printDialog()) {
            try {
              jobControl.print();
            catch (Exception pe) {
              System.out.println("Printing Exception Occured!");
              pe.printStackTrace();
            }
          }
        else if (tempButton.equals(clearButton)) {
          canvas.shapesVector.clear();
          canvas.clear = true;
          canvas.repaint();
        }
      }
    }
  }

  class DrawingCanvas extends Canvas {
    private Vector shapesVector;

    Point2D currPoint, newPoint;

    Line2D line;

    boolean clear = false;

    DrawingCanvas() {
      shapesVector = new Vector();

      addMouseListener(new MouseHandler());
      addMouseMotionListener(new MouseMotionHandler());

      setBackground(Color.white);
      setSize(450400)
    }

    public Vector getShapesVector() {
      return shapesVector;
    }

    public void update(Graphics g) {
      Graphics2D g2D = (Graphics2Dg;

      g2D.setColor(Color.black);

      if (currPoint != null && newPoint != null) {
        line = new Line2D.Float(currPoint, newPoint);
        g2D.draw(line);
        shapesVector.addElement(line);
      }
      currPoint = newPoint; 

      if (clear) {
        g2D.setColor(Color.white);
        g2D.fillRect(00, getWidth(), getHeight());
        clear = false;
      }
    }

    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2Dg;

      g2D.setColor(Color.black);

      for (int i = 0; i < shapesVector.size(); i++) {
        Line2D.Float line2D = (Line2D.FloatshapesVector.elementAt(i);
        g2D.draw(line2D);
      }
    }

    class MouseHandler extends MouseAdapter {
      public void mousePressed(MouseEvent e) {
        currPoint = newPoint = e.getPoint();
        repaint();
      }
    }

    class MouseMotionHandler extends MouseMotionAdapter {
      public void mouseDragged(MouseEvent e) {
        newPoint = e.getPoint();
        repaint();
      }
    }
  }

  class PrintableCanvas implements Printable {
    private Vector linesVector;

    public PrintableCanvas(Vector vector) {
      linesVector = (Vectorvector.clone();
    }

    public int print(Graphics pg, PageFormat pf, int pi)
        throws PrinterException {
      Graphics2D pg2D = (Graphics2Dpg;

      pg2D.translate(pf.getImageableX(), pf.getImageableY());

      pg2D.setPaint(Color.green);
      pg2D.drawString(" "100100);

      pg2D.setPaint(Color.black);
      for (int i = 0; i < linesVector.size(); i++) {
        Line2D.Float line2D = (Line2D.FloatlinesVector.elementAt(i);
        pg2D.draw(line2D);
      }

      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.Another print demoAnother print 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.