salut tout le monde , je voudrais imprimer un JPanel contenant jfreeChart mais le malheur une exception est générée :(
la voila
comment résoudre ce probleme??Code:
1
2 sun.print.ProxyPrintGraphics cannot be cast to java.awt.Graphics2D
Version imprimable
salut tout le monde , je voudrais imprimer un JPanel contenant jfreeChart mais le malheur une exception est générée :(
la voila
comment résoudre ce probleme??Code:
1
2 sun.print.ProxyPrintGraphics cannot be cast to java.awt.Graphics2D
Attends, ne bouge pas, je sort mes poulets pour faire de la divination.
Ou alors tu peux nous donner ton code et la stack complète de ton exception?
code d'impression cclassique
impression:Code:
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 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package audiogramme; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.PrintJob; import java.awt.Toolkit; import javax.swing.*; import sun.print.ProxyPrintGraphics; /** * * @author BIBICHA */ public class Imprimer { public void imprimer(JPanel relief, JFrame frame) { try { System.out.println("je suis la 1"); PrintJob tache = Toolkit.getDefaultToolkit().getPrintJob(frame, "Imprimer relief", null); if (tache != null) { System.out.println("je suis la 2"); ProxyPrintGraphics gg=(ProxyPrintGraphics) tache.getGraphics();; Graphics g = tache.getGraphics(); //Graphics g=gg.getGraphics(); System.out.println("je suis la 3"); if (g != null) System.out.println(g.getClass().getName()); //sun.awt.windows.WPathGraphics { System.out.println("je suis la 4"); relief.print(g); relief.printAll(g); System.out.println("je suis la 5"); g.dispose(); } System.out.println("je suis la 6"); tache.end(); System.out.println("je suis la 7"); } } catch(Exception e) { System.out.println("System d'impression icompatible"+e.getMessage()); } } }
this contient jfreeChartCode:
1
2
3
4 Imprimer i=new Imprimer(); i.imprimer(OD, this);
il s'arrete au niveau de l'affichage 4 et il bloque pour donner l'exception que j'ai donné
Code d'impression classique ? Où est-ce que tu es allé pêcher ça ?
Avec dialogue pour configurer l'impression :Code:
1
2
3
4
5
6
7
8
9 java.awt.print.Printable printable = ...; PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(printable); job.print();
Code:
1
2
3
4
5
6
7
8
9
10
11 Printable printable = ...; PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(printable); if (job.printDialog()) { job.print(); }
Un ChartPanel JFreeChart étant Printable, tu peux l'imprimer directement. Pour imprimer un JPanel qui contient un ChartPanel, s'il n'y a rien d'autre dans le JPanel, imprimer le ChartPanel, sinon implémenter l'interface Printable. Il peut être intéressant d'implémenter Printable (ou java.awt.print.Pageable, à utiliser avec la méthode Job.setPageable() au lieu de Job.setPrintable()) si tu as besoin d'imprimer des mentions supplémentaires qui ne seraient pas déjà gérées par JFreeChart (titre, numéro de page, etc.).