Bonjour à tous,

Tout d'abord j'espère être dans la bonne rubrique, si je ne le suis pas, n'hésitez pas à me le signaler.

Voilà mon problème, j'ai une petite application représentant des automates, je souhaite après réalisation d'un automate l'imprimer j'ai donc implanter la classe jointe.

Mon problème est que je n'arrive pas à afficher mon image à l'aide de la méthode g.drawImage().

Pour information ma requête getAutomateImage() fonctionne bien étant donné que l'image est bien créé sur mon disque dur.

Merci d'avance pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 d’impression
            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;
    }
}