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;
}
}
|