1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
package test;
import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.text.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
class Test extends JFrame implements ActionListener {
BorderLayout borderLayout1 = new BorderLayout();
JEditorPane jEditorPane1 = new JEditorPane();
JEditorPane jEditorPane2 = new JEditorPane();
JEditorPane jEditorPane3 = new JEditorPane();
JScrollPane jScrollPane1 = new JScrollPane(jEditorPane1);
JScrollPane jScrollPane2 = new JScrollPane(jEditorPane2);
JScrollPane jScrollPane3 = new JScrollPane(jEditorPane3);
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
public Test() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
setSize(320, 400);
}
private void jbInit() throws Exception {
String dir = System.getProperty("user.dir");
jEditorPane1.setPreferredSize(new Dimension(200, 50));
jEditorPane1.setPage("file:" + dir + "/doc/header.html");
jEditorPane2.setPage("file:" + dir + "/doc/center.html");
jEditorPane3.setPreferredSize(new Dimension(200, 50));
jEditorPane3.setPage("file:" + dir + "/doc/footer.html");
this.getContentPane().setLayout(borderLayout1);
jMenu1.setText("File");
jMenuItem1.setText("Print");
jMenuItem1.addActionListener(this);
this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.NORTH);
this.getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);
this.getContentPane().add(jScrollPane3, java.awt.BorderLayout.SOUTH);
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem1);
this.setJMenuBar(jMenuBar1);
}
public void doPrint() {
// Because I do not have a printer.
doSaveAsImage();
}
public void doSaveAsImage() {
Printable printable = new EditorPrintable(jEditorPane1, jEditorPane2, jEditorPane3);
Paper paper = new Paper();
paper.setImageableArea(0, 0, 300, 400);
PageFormat format = new PageFormat();
format.setPaper(paper);
try {
for (int i = 0; true; i++) {
BufferedImage image = new BufferedImage(300, 400, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
if (printable.print(graphics, format, i) == Printable.NO_SUCH_PAGE) {
break;
}
graphics.setColor(Color.BLUE);
graphics.setFont(new Font("Dialog", Font.PLAIN, 48));
graphics.drawString("Image #" + i, 100, 100);
graphics.dispose();
ImageIO.write(image, "png", new File("test" + i + ".png"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent event) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
doPrint();
}
});
}
public static void main(String[] args) {
Test t = new Test();
t.setDefaultCloseOperation(EXIT_ON_CLOSE);
t.setVisible(true);
}
} |
Partager