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
|
public class Impression implements Printable {
// ATTRIBUTS
PageFormat pageFormat;
Image image;
public Impression(GraphicAutomat gr) {
image = getAutomateImage(gr);
imprimer();
}
public int print(Graphics g, PageFormat pf, int pi) {
// Imprime au maximum 2 pages
if (pi >= 2) {
return NO_SUCH_PAGE;
}
g.drawImage(image, 100, 100,null);
return PAGE_EXISTS;
}
public void imprimer() {
PrinterJob printJob = PrinterJob.getPrinterJob();
if (pageFormat == null) {
pageFormat = printJob.defaultPage();
}
printJob.setPrintable(this, pageFormat);
if (printJob.printDialog()) { // le dialogue dimpression
try {
printJob.print();
} catch (PrinterException ex) {
JOptionPane.showMessageDialog(
null, "Erreur d'impression",
"Erreur d'impression",
JOptionPane.ERROR_MESSAGE);
}
}
}
public static Image getAutomateImage(GraphicAutomat gr) {
Image result = null;
Point p = gr.getLocationOnScreen();
Dimension d = gr.getSize();
Rectangle rect = new Rectangle(p.x, p.y, d.width, d.height);
try {
Robot r = new Robot();
BufferedImage img = r.createScreenCapture(rect);
File file = new File("text.jpg");
if (!file.exists()) {
file.createNewFile();
}
ImageIO.write(img, "JPG", file);
} catch (IOException ex) {
throw new IllegalStateException("Erreur de fichier");
} catch (AWTException ex) {
throw new IllegalStateException("Erreur de Robot");
}
return result;
}
} |
Partager