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

📄 用java设计面向对象的打印程序.htm

📁 用JAVA设计面向对象的打印程序
💻 HTM
📖 第 1 页 / 共 3 页
字号:
    float lineWidth;
    BasicStroke bs;
    /**
    *3D矩形
    */
    public final static int REC3D = 0;
    /**
    *普通矩形
    */
    public final static int RECNORMAL = 1;
    /**
    *圆角矩形
    */
    public final static int RECROUND = 2;
    int px;
    int py;
    int w;
    int h;
    /**
    * x 方向圆倒角直径
    */
    int ax;
    /**
    * y 方向圆倒角直径
    */
    int ay;
    int type;
    boolean raised;
    /**
    *设置线宽
    *@param w   线的宽度
    */
    public void setWidth(float w){
        lineWidth = w;
        bs  = new BasicStroke(lineWidth);
    }
    /**
    *构造矩形打印对象
    *@param x   矩形左上角 X 坐标
    *@param y   矩形左上角 y 坐标
    *@param width   矩形宽
    *@param height  矩形高
    */
    public PrintRectangle(int x,int y,int width ,int height){
        px = x;
        py = y;
        w = width;
        h = height;
    }
    /**
    *设置矩形类型
    *@param t   矩形类型
    */
    public void setType(int t){
        type = t;
    }
    /**
    *设置3D矩形类型
    *@param r   是否升起
    */
    public void set3DType(boolean r){
        raised =r;
    }
    /**
    *设置圆角矩形的圆角
    *@param xd  x方向圆角直径
    *@param yd  y方向圆角直径
    */
    public void setRound(int xd,int yd){
        ax = xd;
        ay = yd;
    }
    /**
    *@see PrintObject#print
    */
    public void print(Graphics gg){
        Graphics2D g = (Graphics2D)gg;
        g.setStroke(bs);
        switch (type){
            case REC3D:{
                g.draw3DRect(px, py, w, h, raised);
                break;
            }
            case RECNORMAL:{
                g.drawRect(px, py, w, h);
                break;
            }
            case RECROUND:{
                g.drawRoundRect(px, py, w, h, ax, ay);
                break;
            }
        }
    }
}
</CODE></PRE></TD></TR></TBODY></TABLE></P>
      <P><A name=1_5></A><SPAN class=atitle3>5、 打印内容的基类的设计</SPAN></P>
      <P>基类必须具有要有一个打印对象的容器,程序中使用了Vector对象。并且需要有将打印对象加入到容器中和取出的功能,具体设计如下:</P>
      <P>
      <TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc 
        border=1><TBODY>
        <TR>
          <TD><PRE><CODE>
package lilidb;

import java.util.*;
import java.awt.*;

public class PagePrint {

  /** 用来存储打印对象 **/
  Vector vc;

  public PagePrint() {
    vc = new Vector();
  }
  /**
  *将 PrintObject 对象放入 vc 中
  *@param po  PrintObject
  */
  public void putPrintO(PrintObject po){
    vc.add(po);
  }
  /**
  *取出PrintObject
  *@param i   在集合vc 中的序号
  */
  public PrintObject getPrintO(int i){
    return (PrintObject)vc.get(i);
  }

  /**
   * 遍历vc,执行每个PrintObject对象的print方面
   * @param g 将要打印的图形环境
   */
  public void print(Graphics g){
    int i = vc.size();
    Graphics2D g2d = (Graphics2D)g;
    for(int j=0;j&lt;i;j++){
      getPrintO(j).print(g2d);
    }
  }
}
</CODE></PRE></TD></TR></TBODY></TABLE></P>
      <P><A name=1_6></A><SPAN class=atitle3>6、 实际打印类的设计</SPAN></P>
      <P>实际的打印类的设计,可根据实际需要而定,添加必要的功能,但必须封装Printable接口并继承PagePrint类,一般形式如下:</P>
      <P>
      <TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc 
        border=1><TBODY>
        <TR>
          <TD><PRE><CODE>
class PrintAll extends PagePrint implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    super.print(g);
    return Printable.PAGE_EXISTS;
  }
}
</CODE></PRE></TD></TR></TBODY></TABLE></P>
      <P>采用以上方法编写的代码均是可重用的,实际编写具体的打印程序时,只需要将打印的内容分解成相应的打印对象,然后将打印对象加入到容器中即可。既使用一个或多个PrintAll对象(每个PrintAll对象作为一页来处理),将分解的打印对象定义,设置相应的属性,然后使用PrintAll.putPrintO(PrintObject 
      po)方法加入到容器中。这样编写打印程序就无需再考虑繁琐的逻辑了,只需分解对象,计算位置即可。是不是很爽呢?笔者在使用java开发一个金融项目时,就成功使用了上述技术,大大提高了工作效率。如需要更多的程序请和笔者联系,地址:<A 
      href="mailto:zhangld@mail.hf.ah.cn">zhangld@mail.hf.ah.cn</A></P>
      <P><A name=resources><SPAN class=atitle2>参考资料:</SPAN></A></P>
      <P>java平台上的打印:<A 
      href="http://java.sun.com/printing/">http://java.sun.com/printing/</A></P>
      <P>java网站的打印用例:<BR><A 
      href="http://java.sun.com/docs/books/tutorial/2d/printing/example-1dot2/ShapesPrint.java">http://java.sun.com/docs/books/tutorial/2d/printing/example-1dot2/ShapesPrint.java</A><BR><A 
      href="http://java.sun.com/docs/books/tutorial/2d/printing/example-1dot2/SimpleBook.java">http://java.sun.com/docs/books/tutorial/2d/printing/example-1dot2/SimpleBook.java</A></P>
      <P>developerworks上文枫的《java打印程序设计》:<BR><A 
      href="http://www-900.ibm.com/developerWorks/cn/java/l-javaprint/index.shtml">http://www-900.ibm.com/developerWorks/cn/java/l-javaprint/index.shtml</A></P>
      <P><A name=author1><SPAN class=atitle2>作者简介:</SPAN></A></P>
      <P>张来东 
      java技术的爱好者,从jdk1.1开始,跟踪使用java语言开发软件(服务器端和客户端),在《网络世界》、《中国计算机报》、《计算机世界报》上发表了多篇java方面的技术文章。目前在中国人民银行六安市中心支行科技科工作。<BR>联系地址:安徽省六安市人民路41号 
      中国人民银行六安市中心支行<BR>邮编:237006<BR>email: <A 
      href="mailto:zhangld@mail.hf.ah.cn">zhangld@mail.hf.ah.cn</A><BR><A 
      href="mailto:zld@ah163.com">zld@ah163.com</A></P></TD>
    <TD width=10><IMG height=1 alt="" src="用JAVA设计面向对象的打印程序.files/c.gif" 
      width=10 border=0></TD></TR></TBODY></TABLE><!-- END PAPER BODY--><BR 
clear=all><IMG height=10 alt="" src="用JAVA设计面向对象的打印程序.files/c.gif" width=100 
border=0><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
    <TD align=right width="100%"><A 
      href="http://www-900.ibm.com/developerWorks/cn/java/l-oojavaprint/#top">到页首</A></TD>
    <TD width=5><IMG height=1 alt="" src="用JAVA设计面向对象的打印程序.files/c.gif" 
      width=5 border=0></TD></TR>
  <TR vAlign=top>
    <TD bgColor=#000000 colSpan=2><IMG height=1 alt="" 
      src="用JAVA设计面向对象的打印程序.files/c.gif" width=100 border=0></TD></TR>
  <TR vAlign=top>
    <TD bgColor=#ffffff colSpan=2><IMG height=8 alt="" 
      src="用JAVA设计面向对象的打印程序.files/c.gif" width=100 border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=10 width="100%" border=0>
  <TBODY>
  <TR vAlign=top>
    <TD>
      <FORM name=getURL 
      action=/developerWorks/cn/cnratings.nsf/RateArticle?CreateDocument 
      method=post><INPUT type=hidden value=用JAVA设计面向对象的打印程序 name=ArticleTitle> 
      <INPUT type=hidden name=url>
      <SCRIPT language=javascript>getURL();</SCRIPT>
       <INPUT type=hidden value=Java name=Zone> <INPUT type=hidden 
      value=/developerWorks/cn/thankyou/feedback-java.html name=RedirectURL> <A 
      name=rating><B>您对这篇文章的看法如何?</B></A> 
      <TABLE cellSpacing=0 cellPadding=0 width=600 border=0>
        <TBODY>
        <TR>
          <TD colSpan=5><IMG height=8 alt="" 
            src="用JAVA设计面向对象的打印程序.files/c.gif" width=100 border=0></TD></TR>
        <TR vAlign=top>
          <TD width="16%"><INPUT type=radio value=5 name=Rating>真棒!(5)</TD>
          <TD width="20%"><INPUT type=radio value=4 name=Rating>好材料 (4)</TD>
          <TD width="24%"><INPUT type=radio value=3 name=Rating>一般;尚可 (3)</TD>
          <TD width="22%"><INPUT type=radio value=2 name=Rating>需提高 (2)</TD>
          <TD width="18%"><INPUT type=radio value=1 name=Rating>太差! 
        (1)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA name=Comments rows=5 wrap=virtual cols=60></TEXTAREA><BR><BR><INPUT type=submit value=提交反馈意见></FORM></TD></TR>
  <TR vAlign=top>
    <TD bgColor=#ffffff><IMG height=8 alt="" 
      src="用JAVA设计面向对象的打印程序.files/c.gif" width=100 border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
    <TD align=right>(c) Copyright IBM Corp. 2001, (c) Copyright IBM China 
      2001, All Right Reserved</TD></TR>
  <TR vAlign=top>
    <TD class=bbg height=21>&nbsp;&nbsp;<A class=mainlink 
      href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/index.shtml&amp;origin=dwhead">关于 
      IBM</A><SPAN class=divider>&nbsp;&nbsp;|&nbsp;&nbsp;</SPAN><A 
      class=mainlink 
      href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/privacy/index.shtml&amp;origin=dwhead">隐私条约</A><SPAN 
      class=divider>&nbsp;&nbsp;|&nbsp;&nbsp;</SPAN><A class=mainlink 
      href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/legal/index.shtml&amp;origin=dwhead">使用条款</A><SPAN 
      class=divider>&nbsp;&nbsp;|&nbsp;&nbsp;</SPAN><A class=mainlink 
      href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/contact/index.shtml&amp;origin=dwhead">联系 
      IBM</A></TD></TR></TBODY></TABLE>
<SCRIPT language=JavaScript1.2 src="" type=text/javascript></SCRIPT>
<NOSCRIPT><IMG height=1 alt="" src="" width=1 border=0></NOSCRIPT> 
</A></BODY></HTML>

⌨️ 快捷键说明

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