Book demo : Print « 2D Graphics GUI « Java






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(2, 6));
    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(450, 425);
    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 = (JRadioButton) obj;

        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 = (JButton) obj;

        if (tempButton.equals(addButton)) {
          book.append(new PrintableCanvas(canvas.getShapesVector()),
              (PageFormat) pageFormat.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(450, 400); 
    }

    public Vector getShapesVector() {
      return shapesVector;
    }

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

      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(0, 0, getWidth(), getHeight());
        clear = false;
      }
    }

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

      g2D.setColor(Color.black);

      for (int i = 0; i < shapesVector.size(); i++) {
        Line2D.Float line2D = (Line2D.Float) shapesVector.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 = (Vector) vector.clone();
    }

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

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

      pg2D.setPaint(Color.green);
      pg2D.drawString(" ", 100, 100);

      pg2D.setPaint(Color.black);
      for (int i = 0; i < linesVector.size(); i++) {
        Line2D.Float line2D = (Line2D.Float) linesVector.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