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
|
class Impression implements Printable
{
String phrase ;
String font ;
int mode ;
int taille;
public Impression(String phrase, String font, int mode, int taille)
{
this.phrase = phrase ;
this.font = font ;
this.mode = mode ;
this.taille = taille ;
}
public int print(Graphics g, PageFormat pf, int indexPage) throws PrinterException
{
if(indexPage > 0) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.black);
g2.setFont(new Font(font, mode, taille));
g2.drawString(phrase, 144, 144);
return PAGE_EXISTS;
}
//.........................................
JMenuItem imprimer = new JMenuItem("Imprimer") ;
fichier.add(imprimer) ;
imprimer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK)) ;
imprimer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PrinterJob tache = PrinterJob.getPrinterJob();
tache.setPrintable(new Impression(textArea.getText(), polices.getFontName(), mode, polices.getSize()));
if(! tache.printDialog()) return;
try {
tache.print();
} catch(Exception e) {
System.out.println("impossible dimprimer");}
}
}) ;
} |
Partager