Sei sulla pagina 1di 3

Using the Printer with Java

Java makes it fairly easy to utilize the printer with the use of the java.awt.print package.
There are two or three classes which are of primary importance for printing. These are
PrinterJob and Printable (found in java.awt.print).
The first thing you need in order to print a document is an instance of PrinterJob. This is
obtained by the static method getPrinterJob().
PrinterJob printerJob = PrinterJob.getPrinterJob();
The other thing you need in order to print is an instance of Printable. Printable is an
interface which contains the method print(Graphics g, PageFormat p, int index). In
this method you will draw on the Graphics obect whatever you want to get printed. This
is the same type of thing as overriding the paint method! however there are two important
differences.
". The method print returns an integer which reports on the status of the current page
to be printed. #ou must have a case which returns Printable.!"#$%&"P'G(
or else it will print in an infinite loop. This case should be based on the index
parameter! which is the inde$ of the current page being printed. Its range is % to
whatever. &or a single page print you should have a case like so'
if (inde$ ( %) return Printable.)*+,-./+P0123
*therwise! it should return Printable.P012+24I,T,
5. The graphics obect will allow you to draw things at the point (%!%)! etc! but note
that there is a specific area of the paper that will be printed to! and the rest will
not. The area which will print is a rectangle roughly defined by the points (65!65)
(785!9":). This is for an 9.; inch < "" inch sheet of paper. These dimensions can
also be obtained from the PageFormat parameter from get)mageable*()!
get)mageable+idth()! etc.
Therefore! if you draw a rectangle like this'
g.fill=ect(%!%!;%!;%)3
#ou will get a very blank sheet of paper.
*kay! now assume that you have your PrinterJob printerJob and your Printable
m,Printable. )ow you need to register m,Printable to printerJob like so'
printerJob.setPrintable(m,Printable);
If you want to configure how it is going to print! you can display a print dialog like so'
printerJob.print-ialog();
this returns a >oolean which reports on whether the user hit okay or cancel! so the
statement can be a condition inside an if statement.
The last thing you need to do is call print on printerJob. This throws a
Printer(xception! so you will have to deal with that.
tr,.
printerJob.print();
/catch (Printer(xception pex).
00 oh no1 the world is ending1
00 let2s stop ever,thing1
#,stem.exit(3);
/
/ere is an e$ample which prints a smiley face on one sheet of paper'
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.Printer(xception;
import java.awt.print.PrinterJob;
p4blic class Print-emo implements Printable.

p4blic static void main(#tring56 args).
PrinterJob printerJob = PrinterJob.getPrinterJob();
Print-emo demo = new Print-emo();
printerJob.setPrintable(demo);
i7(printerJob.print-ialog()).
tr, .
printerJob.print();
/ catch (Printer(xception ex) .
ex.print#tac89race();
/
/
/

p4blic int print(Graphics g, PageFormat p7, int index).
i7(index : 3).
ret4rn Printable.!"#$%&"P'G(;
/else.
draw#mile,(g, p7);
ret4rn Printable.P'G("(*)#9#;
/
/

private void draw#mile,(Graphics g, PageFormat p7).
int x =(int)(p7.get)mageable*();p7.get)mageable+idth()0<);
int , = (int)(p7.get)mageable=();p7.get)mageable&eight()0<);
g.draw!val(x>?3,,>?3,@33,@33);
g.7ill!val(x><?,,><3,@3,@3);
g.7ill!val(x;<3,,><3,@3,@3);
g.draw'rc(x><A,,;@3,??,B3,@C3,@C3);
/

/

Potrebbero piacerti anche