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
|
public Print2DPrinterJob()
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(this);
PrintService[] services = PrinterJob.lookupPrintServices();
PageFormat pf= pj.defaultPage();
Paper paper = pf.getPaper();
paper.setSize( 225, 850);
paper.setImageableArea(5, 5, 220, 812);
PrintRequestAttributeSet format = new HashPrintRequestAttributeSet();
format.add( new Copies(1) );
format.add( new JobName("My job", null ) );
if ( services.length > 0)
{
System.out.println("------------------- selected printer : ------------------");
System.out.println("selected printer " + services[0].getName() );
try {
pj.setPrintService( services[0] );
pj.print();
}
catch (PrinterException pe) {
System.err.println(pe);
}
}
public int print( Graphics g, PageFormat pf,int pageIndex)
{
if (pageIndex == 0)
{
double pageX = pf.getImageableX();
double pageY = pf.getImageableY();
Graphics2D g2d = ( Graphics2D ) g;
g2d.translate( pageX, pageY );
g2d.setFont( new Font("Serif", Font.PLAIN,12 ) );
g2d.setColor(Color.black);
g2d.drawString("La sottise, l'erreur, le péche, la lésine,\n",0, 0);
g2d.drawString("Occupent nos esprits et travaillent nos corps,\n",0, 10);
g2d.drawString("Et nous alimentons nos aimables remords,\n",0, 20);
g2d.drawString("Comme les mendiants nourrissent leur vermine.\n",0, 30);
return Printable.PAGE_EXISTS;
}
else
{
return Printable.NO_SUCH_PAGE;
}
} |
Partager