📄 render.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 6: Building a User Interface</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="@(#)swing.src 1.3 07/20/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: Printing API</FONT></H2>
<FONT SIZE="-1">[<A HREF="swing2.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="advprint.html">NEXT>></A>]</FONT></DIV>
<P>
The Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> 2 platform <code>java.awt.print</code> package lets you
print anything that can be rendered to a <CODE>Graphics</CODE> or
<CODE>Graphics2D</CODE> context — including AWT
components, Java Foundation Classes (JFC) Project Swing
(Project Swing) components, and 2D graphics.
The Printing 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>
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.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#what">What is in the Package?</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#awt">Printing an AWT Component</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#swing">Printing a Project Swing Component</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#graphic">Printing Graphics in Project Swing</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#print">Print Dialog</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#page">Page Setup Dialog</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#collec">Printing a Collection of Pages</A></FONT>
</UL>
<HR>
<A NAME="what"></A>
<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><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Interfaces</FONT>
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Pageable</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Printable</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">PrinterGraphics</FONT>
</UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Classes</FONT>
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Book</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">PageFormat</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Paper</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">PrinterJob</FONT>
</UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Exceptions</FONT>
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">PrinterAbortException</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">PrinterException</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">PrinterIOException</FONT>
</UL>
</UL>
<A NAME="awt"></A>
<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, sans-serif">
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -