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;
} |
Partager