|
import java.awt.*;
import java.awt.print.*;
public class ExempleImpression implements Printable
{
String phrase;
public ExempleImpression(String phrase)
{
this.phrase = phrase;
}
public int print(Graphics g, PageFormat pf, int indexPage)
{
if(indexPage > 0) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.blue);
g2.setFont(new Font("Serif", Font.PLAIN, 64));
g2.drawString(phrase, 96, 144);
return PAGE_EXISTS;
}
public static void main(String args[])
{
PrinterJob tache = PrinterJob.getPrinterJob();
tache.setPrintable(new ExempleImpression("Ceci est un teste d’impression en java!"));
//printDialog affiche la fenetre de sélection de l’imprimante, etc...
//il retourne true si on clique sur "Ok", false si on clique sur "Annuler"
if(! tache.printDialog()) return;
try {
tache.print();
} catch(Exception e) {
System.out.println("impossible d’imprimer");
}
}
}
|