|
NOTA: La traducción de esta documentación es un esfuerzo personal y voluntario, no es un documento oficial de Sun Microsystems
ni Oracle ni está patrocinado por ninguna de estas empresas. Los documentos originales (en inglés) están disponibles en:
http://java.sun.com/docs/books/tutorial/.
Dirija cualquier comentario, petición, felicitación, etc. a tutorialesjava_@RROBA_codexion.com. |
Si desea ayudar a mantener en funcionamiento esta web, colaborar con la traducción de estos documentos o necesita que se traduzca algĂșn capĂtulo en concreto puede realizar una donación directa mediante Paypal:
|
Another common printing task is to print the contents of a window or a frame, either in whole, or in part. The window may contain the following components: toolbars, buttons sliders, text labels, scrollable text areas, images, and other graphical content. All of these components are printed using the following methods of the Java 2D™ printing API:
java.awt.Component.print(Graphics g); java.awt.Component.printAll(Graphics g);The following figure represents a simple user interface.
The code to create this UI is located in the sample program
PrintUIWindow.java.To print this window, modify the code in the earlier examples which printed text or images. The resulting code should appear as follows:
public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); /* Print the entire visible contents of a java.awt.Frame */ frame.printAll(g); return PAGE_EXISTS; }
Note: The call to theprintAllmethod is the only difference between this example and examples to print text or image. Theprint(Graphics g)method mirrors thejava.awt.Component.paint(Graphics g)method used for on-screen rendering. Use theprint()method rather than thepaint()method as theComponentsclass may have overridden theprint()method to handle the printing case differently.The
printAll(Graphics g)method prints the component and all its subcomponents. This method is usually used to print object such as a complete window, rather than a single component.