📄 advprint.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 6: Advanced 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="@(#)advprint.src 1.26 09/21/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" 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>
<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>
</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" HEIGHT="42" WIDTH="319" 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>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- 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>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<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: Advanced Printing</FONT></H2>
<FONT SIZE="-1">[<A HREF="render.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="debug.html">NEXT>></A>]</FONT></DIV>
<P>
The previous section explained how to print simple components and
covered techniques that can be used to print screen captures.
However, if you want to print more than one component per page, or
if your component is larger than one page size, you need to do
some additional work inside your <CODE>print</CODE> method.
This section explains what you need to do and concludes with
an example of how to print the contents of a
<CODE>JTable</CODE> component.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#mc">Multiple Components Per Page</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#mp">Components Larger Than One Page</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#pe">Printing A <CODE>JTable</CODE> Component</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#sales">Printing A Sales Report</A></FONT>
</UL>
<HR>
<A NAME="mc"></A>
<H3>Multiple Components Per Page</H3>
There are times when printing one component on a page does not meet your
printing needs. For example, you might want to include a header on each page
or print a footer with the page number--something that isn't necessarily
displayed on the screen.
<P>
Unfortunately, printing multiple customized components on a page is not as easy
as adding additional <CODE>paint</CODE> calls because each <CODE>paint</CODE> call
overwrites the output of the previous call.
<P>
The key to printing more than one component on a page, is to use
the <CODE>translate(double, double)</CODE> and <CODE>setClip</CODE> methods
in the <CODE>Graphics2D</CODE> class.
<P>
The <CODE>translate</CODE> method moves an imaginary pen to the next position
of the print output where the component can be painted and then printed.
There are two <CODE>translate</CODE> methods
in the <CODE>Graphics2D</CODE> class. To print multiple components you need the one t
hat takes <CODE>double</CODE> arguments because this <CODE>translate</CODE> method allows
relative positioning. Be sure to cast any integer values to double
or float. Relative positioning in this context means that previous
calls to <CODE>translate</CODE> are taken into account when calculating
the new translated point.
<p>
The <CODE>setClip</CODE> method is used to restrict the component to only
be painted, and therefore printed, in the area specified. This lets you
print multiple components on a page by moving the imaginary pen to different
points on the page and then painting each component in the clip area.
<H4>Example</H4>
You can replace the <CODE>print</CODE> method in the
<A HREF="./Code/AWT/printbutton.java">Abstract Window Toolkit (AWT)</A>
and <A HREF="./Code/Swing/printbutton.java">Swing</A> printbutton.java
examples with the following code to add the footer message
<EM>Company Confidential</EM> to the page.
</FONT>
<PRE>
<FONT SIZE="-1">
public int print(Graphics g, PageFormat pf, int pi)
throws PrinterException {
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) g;
Font f= Font.getFont("Courier");
double height=pf.getImageableHeight();
double width=pf.getImageableWidth();
g2.translate(pf.getImageableX(),
pf.getImageableY());
g2.setColor(Color.black);
g2.drawString("Company Confidential", (int)width/2,
(int)height-g2.getFontMetrics().getHeight());
g2.translate(0f,0f);
g2.setClip(0,0,(int)width,
(int)(height-g2.getFontMetrics().getHeight()*2));
paint (g2);
return Printable.PAGE_EXISTS;
}
</FONT>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
In the new <CODE>print</CODE> method, the <CODE>Graphics2D</CODE>
context is clipped before calling the parent <CODE>JButton</CODE>
<CODE>paint</CODE> method. This prevents the <CODE>JButton paint</CODE>
method from overwriting the bottom of the page. The <CODE>translate</CODE>
method is used to point the <CODE>JButton paint</CODE> method to
start the <CODE>paint</CODE> at offset 0,0 from the visible part of the
page. The visible area was already calculated by the previous
<CODE>translate</CODE> call:
</FONT>
<PRE>
g2.translate(pf.getImageableX(), pf.getImageableY());
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
For some components, you might also need to set the foreground color
to see your results. In this example the text color was printed in black.
<H4>Useful Methods To Call In The <CODE>print</CODE> Method</H4>
The following methods are useful for calculating the number of pages
required and for shrinking components to fit on a page:
<p>
<STRONG><CODE>PageFormat</CODE> methods:</STRONG>
<P>
<CODE>getImageableHeight()</CODE><BR> returns the page height you can
user for printing your output.
<p>
<CODE>getImageableWidth()</CODE><BR> returns the page width you
can use for printing your output.
<P>
<STRONG><CODE>Graphics2D</CODE> method:</STRONG>
<P>
<CODE>scale(xratio, yratio)</CODE><BR> scales the 2D graphics context by
this size. A ratio of one maintains the size, less than one will shrink
the graphics context.
<P>
<A NAME="mp"></A>
<H3>Components Larger Than One Page</H3>
The Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> 2 Printing API has a <CODE>Book</CODE> API that
provides the concept of pages. However, the <CODE>Book</CODE> API
only adds printable objects to a collection of printable objects.
It does not calculate page breaks or split components over multiple pages.
<P>
When printing a simple component on a page, you only have to check for the
index value being greater or equal to one and return <CODE>NO_SUCH_PAGE</CODE>
when this value is reached.
<P>
To print multiple pages, you have to calculate the number of pages
needed to contain the component. You can calculate the total
number of pages needed by subtracting the space taken by the component from
the value returned by <CODE>getImageableHeight</CODE>.
Once the total number of pages is calculated, you can run the
following check inside the <CODE>print</CODE> method:
</FONT>
<PRE>
if (pageIndex >=TotalPages) {
return NO_SUCH_PAGE;
}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
The Printing framework calls the <CODE>print</CODE> method multiple times until
<CODE>pageIndex</CODE> is less than or equal to <CODE>TotalPages</CODE>.
All you need to do is create a new page from the same component on
each <CODE>print</CODE> loop. This is done by treating the printed page
like a sliding window over the component.
The part of the component that is to be printed
is selected by a <CODE>translate</CODE> call to mark the top of the page
and a <CODE>setClip</CODE> call to mark the bottom of the page.
The following diagram illustrates this process.
<P>
The left side of the diagram represents the page sent to
the printer. The right side contains the long component
being printed in the <CODE>print</CODE> method.
The first page can be represented as follows:
<P>
<IMG SRC="./Art/page1.gif">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -