🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.java
Article #29588

Re: Imprimir objetos (texto y gr�ficos) desde un Applet

#29588
From: Chano G�mez
Date: Tue, 25 Nov 1997 00:00
97 lines
4569 bytes
>     El problema es que la presentaci�n de lo que voy a imprimir es importante. Forma parte de un PFC, y como te podr�s
> imaginar, la chapuza (al menos, la visual), no punt�a. Tengo que sacar por pantalla unas tablas con n�meros, y si al condenao
> usuario le da por imprimir, pues eso, que tenga lo mismo en papel.



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
T I P S ,  T E CH N I Q U E S ,  A N D  S A M P L E  C O D E

FOR PRINTING IN JAVA, TRY THIS. JDK(tm) 1.1 provides a standard way to
print and improves on JDK 1.0 printing.

You print via the Abstract Window Toolkit (AWT). A simple example for
printing a filled-in oval looks like this:

         import java.awt.*;

         public class print {

                public static void main(String args[])
                {
                        Frame f = new Frame("test");
                        f.pack();

                        PrintJob pj =
                            f.getToolkit().getPrintJob(f, "print1", null);

                        if (pj != null) {
                                Graphics g = pj.getGraphics();

                                g.fillOval(5, 5, 150, 100);

                                g.dispose();

                                pj.end();
                        }

                        System.exit(0);
                }

        }


This example is a standalone Java program that creates an AWT frame, a
top-level GUI component for running an application. The program next
retrieves the toolkit for the frame. The toolkit for the AWT is the
interface between the abstract window layer and an actual windowing
implementation such as Windows 95 or X Windows.

Once you create the toolkit, you can initiate a print job via getPrintJob,
which causes a window to pop up on the screen asking the user for
information about which printer is being used, the number of copies to
print, and so on. This process is similar to printing in conventional
Windows applications.

Given a PrintJob object, you can obtain a graphics context and draw to it.
Such drawing, using normal graphics primitives such as fillOval, goes to
the printer instead of the screen. Calling dispose on the graphics context
object sends the page to the printer, and the print job is terminated by
calling end.

If you're using your own custom AWT components, you can use the paint
methods you define for printing without change--by passing them the
graphics context returned by getGraphics. But if you want different
behavior when you print, specialized methods can also be defined for
components as necessary.


-- Printing from applets

The example given above is a standalone Java program. But what about
printing from applets? The Java security system contains a feature that may
lock out an applet from initiating its own print job, requiring that the
initiation be done via a Web browser or applet viewer. The security area is
in a state of flux at present, and your experiences with this process may
be different.

Finally, another less portable approach to printing text is to open the
special device file that represents the printer, and do conventional file
I/O to that file. For example, "lpt1" can be used for this purpose with
Windows systems, and "/dev/lp" can be used with UNIX.

--  COPYRIGHT --

Copyright Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 U.S.A.

This document is protected by copyright. You may make a reasonable number
of copies of this document for your own use, and you may distribute this
document, subject to the following conditions: (i) all text (including this
notice) must be copied without modification and all pages must be included;
and (ii) this document may not be distributed for commercial purposes.

Sun, the Sun logo, Sun Microsystems, Java, and all Java-related trademarks
are trademarks or registered trademarks of Sun Microsystems, Inc. in the
U.S. and other countries.
Technical Details
Message-ID: <347A17B6.71626AEF@ran.es>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <347421B3.10BE20D5@apolo.lcc.uma.es> <3474B005.99FCEA2F@caymasa.es> <34793AD0.31BD3C49@apolo.lcc.uma.es>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 1116f3,8ef4e93cdee38e2a
X-Google-Attributes: gid1116f3,public
From: "Chano G�mez" <chano@ran.es>
Subject: Re: Imprimir objetos (texto y gr�ficos) desde un Applet
Date: 1997/11/25
Message-ID: <347A17B6.71626AEF@ran.es>
X-Deja-AN: 292526882
References: <347421B3.10BE20D5@apolo.lcc.uma.es> <3474B005.99FCEA2F@caymasa.es> <34793AD0.31BD3C49@apolo.lcc.uma.es>
Organization: Unisource Espana NEWS SERVER
Newsgroups: es.comp.lenguajes.java