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