📄 print.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Java Applications, Chapter 6: Printing</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)print.src 1.21 08/10/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ROWSPAN="2" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A><BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<!-- need to restore in MAPA URL -->
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT"><IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR><TD BGCOLOR="#CC9966" WIDTH="100%"><IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD></TR>
<TR><TD><IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD></TR>
<TR><TD BGCOLOR="#CC9966"><IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD></TR>
<TR><TD><IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD></TR>
<TR><TD BGCOLOR="#CC9966"><IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD></TR>
<TR><TD><IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD></TR>
<TR><TD BGCOLOR="#CC9966"><IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD></TR>
</TABLE></TD>
<TD ALIGN="LEFT"><IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR><TD VALIGN="CENTER"><A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER"><FONT FACE="Helvetica" SIZE="1"><INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER"><INPUT TYPE="image" SRC="/images/search.button.gif" value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD></TR></TABLE></TD>
<!-- TD><IMG SRC="/images/striperightrt.gif"></TD --></TR>
</TABLE></FORM><P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55" HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" ALT="Java Developer Connection(SM)"></A></TD></TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
<TD ALIGN="RIGHT"></TD></TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="4" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- the following spacer ensures that the -->
<!-- table is at least 157 pixels wide -->
<TR><TD></TD></TR>
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/">
<IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130"
BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html">
<IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130"
BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130"
BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130"
BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html">
<IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/">
<IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130"
BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions">
<IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130"
BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies">
<IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130"
BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
</TABLE>
<!-- End NavBar Insert -->
</TABLE>
<TABLE>
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 6 Continued: Printing</FONT></H2>
<FONT SIZE="-1">[<A HREF="aucserv.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="advprint.html">NEXT>></A>]</FONT></DIV>
<P>
The new <code>java.awt.print</code> package is part of the
Java™ 2 platform software and provides
printing capabilities that give you the power and flexibility you
need to meet your application printing requirements.
<P>
The new printing package provides enhanced functionality over what is
available in the pre-existing Abstract Window Toolkit (AWT)
printing API for printing anything that
can be rendered to a graphics context — including AWT components, Swing
components, and 2D graphics. Currently, both APIs are available in the
Java 2 platform software.
<HR>
<H3>About the New Printing Model</H3>
The <code>java.awt.print</code> API is easy to use. Your application
tells the printing system what to print, and the printing system
determines when each page is rendered. This <EM>callback
printing model</EM> enables printing support on a wide range
of printers and systems. The callback model also lets users print to a
bitmap printer from a computer that does not have enough memory
or disk space to hold the bitmap for an entire page.
<P>
The <code>java.awt.print</code> API is designed so anything that can
be rendered through a <EM>graphics context</EM> represented by a
<code>Graphics</code> or <code>Graphics2D</code> object can be printed.
<P>
A graphics context lets a program paint to a rendering device such
as a screen, printer, or offscreen image. Because Swing components are rendered
through a <code>Graphics</code> object using AWT graphics support, it is easy to
print Swing components with the new printing API. However, AWT components are not
rendered to a graphics device, so you must extend the AWT component class and
implement the AWT component paint method.
<H3>What is in the Package?</H3>
The <code>java.awt.print</code> consists of the following interfaces,
classes, and exceptions. Here is where you can view the
<A HREF=" http://java.sun.com/products/jdk/1.2/docs/api/index.html">API
specification</A>.
<UL>
<LI>Interfaces
<UL>
Pageable<BR>
Printable<BR>
PrinterGraphics
</UL>
<LI>Classes
<UL>
Book<BR>
PageFormat<BR>
Paper<BR>
PrinterJob
</UL>
<LI>Exceptions
<UL>
PrinterAbortException<BR>
PrinterException<BR>
PrinterIOException
</UL>
</UL>
<H3>Printing an AWT Component</H3>
<IMG SRC="./Art/awtbutton.gif" ALIGN=LEFT>
The <A HREF="./Code/AWT/printbutton.java">printbutton.java</A>
application displays a panel with <EM>MyButton</EM> on it. When
you click the button, the application prints the <EM>MyButton</EM>
component.
<P>
In the code, the Button class is extended to implement <code>Printable</code>
and includes the <code>paint</code> and <code>print</code> method
implementations. The <code>print</code> method is required because
the class implements <code>Printable</code>, and the <code>paint</code>
method is needed to describe how the button shape and
label text looks when printed.
<P>
To see the button, the printer graphics context is translated
into the imageable area of the printer, and to see the label
text, a font is set on the printer graphics context.
<P>
In this example, the button is printed at a 164/72 inches
inset from the left imageable margin (there are 72 pixels per inch)
and 5/72 inches from the top
imageable margin. This is where the button is positioned in the frame
by the layout manager
and those same numbers are returned by the following calls:
</FONT><PRE>
int X = (int)this.getLocation().getX();
int Y = (int)this.getLocation().getY();
</PRE><FONT FACE="Verdana, Arial, Helvetica">
And here is the <CODE>MyButton</CODE> class code:
</FONT><PRE>
class MyButton extends Button
implements Printable {
public MyButton() {
super("MyButton");
}
public void paint(Graphics g) {
//To see the label text, you must specify a font for
//the printer graphics context
Font f = new Font("Monospaced", Font.PLAIN,12);
g2.setFont (f);
//Using "g" render anything you want.
//Get the button's location, width, and height
int X = (int)this.getLocation().getX();
int Y = (int)this.getLocation().getY();
int W = (int)this.getSize().getWidth();
int H = (int)this.getSize().getHeight();
//Draw the button shape
g.drawRect(X, Y, W, H);
//Draw the button label
//For simplicity code to center the label inside the
//button shape is replaced by integer offset values
g.drawString(this.getLabel(), X+10, Y+15);
}
public int print(Graphics g, PageFormat pf, int pi)
throws PrinterException {
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) g;
//To see the button on the printed page, you
//must translate the printer graphics context
//into the imageable area
g2.translate(pf.getImageableX(), pf.getImageableY());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -