Bonjour
Je suis en train de dévelloper une appli qui imprime des panels,
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.swing.*;
 
public class BookTest extends JDialog
{
	public BookTest(Frame parent,JPanel[] PanelTb)
	{
             super(parent);
             enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		setTitle("Preview");
		setSize(300,75);
		 PanelTb1=PanelTb;
		Container contentPane=getContentPane();
 
 
		//attribut d'impression
		attributes=new HashPrintRequestAttributeSet();
 
		JPanel buttonPanel =new JPanel();
 
		JButton printButton=new JButton("Imprimer");
		buttonPanel.add(printButton);
		printButton.addActionListener(new
		ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				try
				{
                                    //creation d'un printjob pour imprimer
					PrinterJob job=PrinterJob.getPrinterJob();
                                        Book book=makeBook();
                                        System.out.println(book.getNumberOfPages());
                                       job.setPageable(book);
					if (job.printDialog(attributes))
					{
						job.print(attributes);
					}
				}
				catch(PrinterException exception)
				{
					JOptionPane.showMessageDialog(BookTest.this,exception);
				}
			}
		});
		JButton pageSetupButton=new JButton("Mise en page");
		buttonPanel.add(pageSetupButton);
		pageSetupButton.addActionListener(new
		ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
                            //creation d'un printjob pour mise en page
				PrinterJob job=PrinterJob.getPrinterJob();
				pageFormat=job.pageDialog(attributes);
			}
		});
		JButton printPreviewButton=new JButton("Apercu");
		buttonPanel.add(printPreviewButton);
		printPreviewButton.addActionListener(new
		ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
                            //creation d'un jdialog de preview
				PrintPreviewDialog dialog =new PrintPreviewDialog(makeBook());
 
    dialog.setVisible(true);
			}
		});
 
		contentPane.add(buttonPanel,BorderLayout.CENTER);
	}
 
        //creation du  book
	public Book makeBook()
	{
		if (pageFormat==null)
		{
			PrinterJob printJob=PrinterJob.getPrinterJob();
			pageFormat=printJob.defaultPage();
 
		}
		Book book=new Book();
 
                for(int i=0;i<PanelTb1.length;i++){
 
                   imprimer imp =new imprimer((FichePers)PanelTb1[i]);
 
                    book.append(imp, pageFormat);
 
                }
		return book;
	}
        JPanel[] PanelTb1;
 
	private PageFormat pageFormat;
	private PrintRequestAttributeSet attributes ;
 
 
 
}
 
 
class PrintPreviewDialog extends JDialog
{
	public PrintPreviewDialog(Printable p,PageFormat pf,int pages)
	{
 
 
		Book book=new Book();
		book.append(p,pf,pages);
		layoutUI(book);
	}
	public PrintPreviewDialog(Book b)
	{
 
             enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		setTitle("Preview");
		setSize(800,600);
 
		layoutUI(b);
	}
	public void layoutUI(Book book)
	{
		setSize(600,600);
 
		Container contentPane=getContentPane();
		canvas =new PrintPreviewCanvas(book);
		contentPane.add(canvas,BorderLayout.CENTER);
 
		JPanel buttonPanel=new JPanel();
		JButton nextButton=new JButton("Suivant");
		buttonPanel.add(nextButton);
		nextButton.addActionListener(new 
		ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				canvas.flipPage(1);
 
			}
		});
		JButton previousButton=new JButton("Précédent");
		buttonPanel.add(previousButton);
		previousButton.addActionListener(new 
		ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				canvas.flipPage(-1);
			}
		});
		JButton closeButton=new JButton("Fermer");
		buttonPanel.add(closeButton);
		closeButton.addActionListener(new
		ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				setVisible(false);
			}
		});
 
 
		contentPane.add(buttonPanel,BorderLayout.SOUTH);
		}
 
		private PrintPreviewCanvas canvas;
		private static final int WIDTH=300;
		private static final int HEIGHT=300;
}
class PrintPreviewCanvas extends JPanel
{
	public PrintPreviewCanvas(Book b)
	{
		book=b;
		currentPage=0;
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2=(Graphics2D)g;
		PageFormat pageFormat=book.getPageFormat(currentPage);
 
		double xoff;
		double yoff;
		double scale;
		double px=pageFormat.getWidth();
		double py=pageFormat.getHeight();
		double sx=getWidth()-1;
		double sy=getHeight()-1;
		if(px/py<sx/sy)
		{
			scale=sy/py;
			xoff=0.5*(sx-scale*px);
			yoff=0;
		}
		else 
		{
			scale=sx/px;
			xoff=0;
			yoff=0.5*(sy-scale*py);
		}
		g2.translate((float)xoff,(float)yoff);
		g2.scale((float)scale,(float)scale);
 
		Rectangle2D page=new Rectangle2D.Double(0,0,px,py);
		g2.setPaint(Color.white);
		g2.fill(page);
		g2.setPaint(Color.black);
		g2.draw(page);
 
		Printable printable=book.getPrintable(currentPage);
		try
		{
			printable.print(g2,pageFormat,0);
		}
		catch(PrinterException exception)
		{
			g2.draw(new Line2D.Double(0,0,px,py));
			g2.draw(new Line2D.Double(0,px,0,py));
		}
 
	}
 
	public void flipPage(int by)
	{
		int newPage=currentPage+by;
		if(0<=newPage && newPage<book.getNumberOfPages())
		{
			currentPage=newPage;
 
			repaint();
		}
	}
	private Book book;
	private int currentPage;
}
et voila ma classe imprime
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
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.Vector;
 
import javax.swing.JPanel;
public class imprimer implements Printable {
  public imprimer(JPanel panel)
    {
 
        this.panel = panel;
        initPrintablePanel();
    }
 
    public void initPrintablePanel()
    {
 
        printJob = PrinterJob.getPrinterJob();
        pageFormat = printJob.defaultPage();
        pageFormat.setOrientation(1);
    }
 
 
 
//renvoie la largeur de la page
    public int getPageWidth()
    {
        return (int)pageFormat.getImageableWidth();
    }
// renvoi la marge du haut
    public double getMarginTop ()
    {
        return pageFormat.getImageableY();
    }
//renvoi la marge de gauche
    public double getMarginLeft ()
    {
        return pageFormat.getImageableX();
    }
 
 
 
//preparation de l'impression
 
    public int print(Graphics g, PageFormat pf, int pageIndex)
        throws PrinterException
    {
        //dimensin du panel a imprimer
        Dimension tailleDoc = panel.getSize();
        //hauteur du panel
        double hauteurDocu = tailleDoc.getHeight();
        //hauteur de la page
        double hauteurPage = pf.getImageableHeight();
        //largeur du panel
        double largeurDocu = tailleDoc.getWidth();
        //largeur de la page
        double largeurPage = pf.getImageableWidth();
        //total nombre de page
        int totalNumPages = (int)Math.ceil(hauteurDocu / hauteurPage);
 
            totalNumPages = 1;
 
 
        //mise a l'echelle x et y
        double scaleX = largeurPage / (largeurDocu);
        double scaleY = hauteurPage / hauteurDocu;
 
      /*  if(pageIndex >= totalNumPages)
        {
            return Printable.NO_SUCH_PAGE;
        }*/
        //creation du graphique
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //translation du graphique en fonction des marges
        g2d.translate(pf.getImageableX(), pf.getImageableY());
 
            double ratio = Math.min(scaleX, scaleY);
            g2d.scale(ratio, ratio);
        panel.printAll(g2d);
 
         return Printable.PAGE_EXISTS;
    }
 
 
 
 
    private JPanel panel;
 
    private PageFormat pageFormat;
    private PrinterJob printJob;
    public static final int PORTRAIT = 1;
    public static final int LANDSCAPE = 0;
}
mon probleme est que lorsque je lance l'impression seule la premiere page de mon book s'imprime alors que la previsualisation marche .
Si quelq'un a une idée merci d'avance