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
|
import java.awt.print.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JOptionPane;
import java.awt.image.BufferedImage;
/**
*
* @author GLDavid
*/
public class Imprimer extends javax.swing.JPanel implements Printable {
private BufferedImage bImage;
//Creates a new instance of Imprimer
public Imprimer() {
bImage = new BufferedImage(ProjectWindow.jTextArea1.getWidth(), ProjectWindow.jTextArea1.getHeight(), BufferedImage.TYPE_INT_RGB);
java.awt.Graphics g = bImage.getGraphics();
ProjectWindow.jTextArea1.printAll(g);
setSize(ProjectWindow.jTextArea1.getWidth(), ProjectWindow.jTextArea1.getHeight());
repaint();
printme();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (bImage != null)
g.drawImage(bImage, 0, 0, null);
else {
JOptionPane.showMessageDialog(null,
"Problem to print the canvas",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
public void printme() {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(this);
try {
pj.print();
} catch ( Exception e ) {
JOptionPane.showMessageDialog(null, ""+e, "Error", JOptionPane.ERROR_MESSAGE);;
}
}
public int print(Graphics g, PageFormat pf, int index) throws PrinterException {
if (index > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
this.paint(g2d);
return(PAGE_EXISTS);
}
}
} |
Partager