⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 render.html

📁 jdbc书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  //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());
    g2.setColor(Color.black);
    paint(g2);
    return Printable.PAGE_EXISTS;
   }
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<BLOCKQUOTE>
<STRONG><FONT COLOR=PURPLE>Note:</FONT></STRONG>
The printing Graphics2D is based on the <code>BufferedImage</code> class and
on some platforms does not default to a foreground color of black. If
this is the case on your platform, you have to add 
<code>g2.setColor(Color.black)</code> to the <code>print</code> method 
before the <code>paint</code> invocation.
</BLOCKQUOTE>

<A NAME="swing"></A>
<H3>Printing a Project Swing Component</H3>

<IMG SRC="./Art/swbutton.gif" ALIGN=LEFT>
Printing a Project Swing component is almost the same as printing an AWT
component, except the <code>MyButton</code> subclass does not 
need a <code>paint</code> method implementation. It does, however, have 
a <code>print</code> method that calls the <CODE>paint</CODE>
method for the component. The <code>paint</code> method implementation
is not needed because Project Swing components know how to paint themselves. 
<P>
Here 
is the complete <A HREF="./Code/Swing/printbutton.java">printbutton.java</A>
source code for Project Swing.

</FONT>

<PRE>
class MyButton extends JButton implements Printable {

  public MyButton() {
    super("MyButton");
  }

  public int print(Graphics g, 
                     PageFormat pf, int pi)
                       throws PrinterException {
    if (pi &gt;= 1) {
      return Printable.NO_SUCH_PAGE;
    }

    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pf.getImageableX(), 
                 pf.getImageableY());
    Font  f = new Font("Monospaced", Font.PLAIN,12);
    g2.setFont (f);
    paint(g2);
    return Printable.PAGE_EXISTS;
   }
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

If you extend a <code>JPanel</code> and implement <code>Printable</code>,
you can print a panel component and all of its contents.  

</FONT>

<PRE>
public class printpanel extends JPanel 
			implements ActionListener, 
			Printable {
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

Here is
the <A HREF="./Code/Swing/printpanel.java">printpanel.java</A> code
that prints a <code>JPanel</code> object and the <code>JButton</code>
it contains, and the 
<A HREF="./Code/Swing/ComponentPrinterFrame.java">ComponentPrinterFrame.java</A>
code that prints a <code>JFrame</code> object and the 
<code>JButton</code>, <code>JList</code>, <code>JCheckBox</code>,
and <code>JComboBox</code> components it contains.

<A NAME="graphics"></A>
<H3>Printing Graphics in Project Swing</H3>

In the same way the AWT example subclassed a <code>Button</code>
component and implemented the <code>paint</code> method to
draw the button, you can subclass an AWT or Project Swing component and implement 
the <code>paint</code> method to render 2D graphics to the screen or printer.
The <A HREF="./Code/Swing/ShapesPrint.java">ShapesPrint.java"</A> 
Project Swing application borrowed from 
<A HREF="http://java.sun.com/docs/books/tutorial/index.html">The Java Tutorial</A>
shows how this is done.  It is modified for this article to include
a <code>TextLayout</code> object.
<P>
<IMG SRC="./Art/shapes.gif">
<P>
The <code>paintComponent</code> method calls the <code>drawShapes</code> 
method to render the 2D graphics to the screen when the application starts.  
When you click the <code>Print</code> button, a printer graphics context 
is created and passed to the <code>drawShapes</code> method for printing.

<A NAME="print"></A>
<H3>Print Dialog</H3>

It is easy to display a Print dialog
so the end user can interactively change the print job
properties.  The <code>actionPerformed</code> method of
the previous Project Swing example is modified here
to do just that.
<P>
<IMG SRC="./Art/dialog.gif">
<P>

</FONT>

<PRE>
public void actionPerformed(ActionEvent e) {
  PrinterJob printJob = PrinterJob.getPrinterJob();
  printJob.setPrintable((MyButton) e.getSource());
  if(printJob.printDialog()){
    try { printJob.print(); } 
    catch (Exception PrinterExeption) { }
  }
}
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<BLOCKQUOTE>
<STRONG><FONT COLOR=PURPLE>Note:</FONT></STRONG>In Project Swing, the 
<code>printJob.setPageable((MyButton) e.getSource());</code>
statement can be written as
<code>printJob.setPrintable((MyButton) e.getSource());</code>. The
difference is <code>setPrintable</code> is for applications that
do not know the number of pages they are printing.  
If you use <code>setPrintable</code>, you need to add 
<code>if(pi &gt;= 1){ return Printable.NO_SUCH_PAGE: }</code>
to the beginnig of the <code>print</code> method.
</BLOCKQUOTE>

<A NAME="page"></A>
<H3>Page Setup Dialog</H3>

You can add a line of code that tells the <code>PrinterJob</code> object
to display a Page dialog so the end user can interactively modify the 
page format for printing in portrait, landscape, or reverse landscape mode.
The <code>actionPerformed</code> method of the previous Project Swing example is 
modified here to display Page and Print dialogs.
<P>
<BLOCKQUOTE>
<STRONG><FONT COLOR=PURPLE>Note:</FONT></STRONG> Some platforms
do not support a page dialog. On those platforms, the <code>pageDialog</code>
call simply returns the passed-in <code>PageFormat</code> object
and no dialog appears.
</BLOCKQUOTE>

</FONT>

<PRE>
public void actionPerformed(ActionEvent e) {
  PrinterJob printJob = PrinterJob.getPrinterJob();
  printJob.setPrintable((MyButton) e.getSource());
  PageFormat pf = printJob.pageDialog(
                             printJob.defaultPage());
  if(printJob.printDialog()){
    try { printJob.print(); } catch (Exception ex) { }
  }
}
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<A NAME="collec"></A>
<H3>Printing a Collection of Pages</H3>

You can use the <code>Book</code> class to print a collection
of pages that you append to the book. The pages can be in any order 
and have different page formats.
<P>
<IMG SRC="./Art/print2.gif" ALIGN=LEFT>
The <A HREF="./Code/Swing/print2button.java">print2button.java</A>
example puts the <code>Print</code> and <code>Print 2</code> buttons of 
type <code>MyButton</code> on a panel. 
It creates a book that contains the pages to print.
When you click either button, the book prints one copy of the 
<code>Print</code> button in
landscape mode and two copies of the <code>Print 2</code> button
in portrait more, as specified in the <code>actionPerformed</code> 
method implementation shown here.

<BLOCKQUOTE>
<HR>
<STRONG>Note:</STRONG>
Currently a bug restricts the Solaris platform to
only print in portrait mode.
<HR>
</BLOCKQUOTE>

</FONT>

<PRE>
public void actionPerformed(ActionEvent e) {
  PrinterJob printJob = PrinterJob.getPrinterJob();

/* Set up Book */
  PageFormat landscape = printJob.defaultPage();
  PageFormat portrait = printJob.defaultPage();
  landscape.setOrientation(PageFormat.LANDSCAPE);
  portrait.setOrientation(PageFormat.PORTRAIT);
  Book bk = new Book();
  bk.append((Printable)b, landscape);
  bk.append((Printable)b2, portrait, 2);
  printJob.setPageable(bk);

  try { printJob.print(); } catch (Exception ex) { }
}
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">


<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>

</FONT>
</TD>
</TR>
</TABLE>




<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->

</TD>
</TR>
</TABLE>

<!-- Copyright Insert -->

<BR CLEAR="ALL">

<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">   
  <TR>
    <TD VALIGN="TOP">
	
    <P ALIGN=CENTER>
    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
    [ This page was updated: <!-- new date --> 13-Oct-99 ]</font></P>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>
  
  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> | 
    <A HREF="/developer/index.html">Developer Connection</A> | 
    <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> | 
    <A HREF="/developer/support/index.html">Online Support</A><BR>
    <A HREF="/developer/community/index.html">Community Discussion</A> |
    <A HREF="http://java.sun.com/industry/">Industry News</A> | 
    <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | 
    <A HREF="http://java.sun.com/casestudies">Case Studies</A>
    </FONT>
    </CENTER>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>

  <TR>
    <TD ALIGN="CENTER">
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - 
    <A HREF="http://java.sun.com/applets/">Applets</A> - 
    <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - 
    <A HREF="http://java.sun.com/jobs/">Employment</A> - 
    <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> - 
    <A HREF="http://java.sun.com/javastore/">Java Store</A> -
    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
    </FONT>
    </TD>
  </TR>

  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <a href="/siteinfo/faq.html">FAQ</a> |
    <a href="/feedback/index.html">Feedback</a> | 
    <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | 
    <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
    </FONT>
    </CENTER>

    </TD>
  </TR>
  
  <TR>
    <TD>

    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
      <TR>
        <TD WIDTH="50%">
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        For more information on Java technology<BR>
        and other software from Sun Microsystems, call:<BR>
        </FONT>
        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
        (800) 786-7638<BR></FONT>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Outside the U.S. and Canada, dial your country's 
        <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>
        </FONT>
        </TD>

        <TD ALIGN="RIGHT" WIDTH="50%">
        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Copyright &copy; 1995-99
        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
        All Rights Reserved. 
        <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. 
        <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.
        </FONT>
        </TD>
      </TR>
    </TABLE>
	
    </TD>
  </TR> 
</TABLE>
</FORM>

<!-- End Copyright Insert -->


</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -