?? 用java設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?htm
字號:
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;
/**
*設(shè)置線寬
*@param w 線的寬度
*/
public void setWidth(float w){
lineWidth = w;
bs = new BasicStroke(lineWidth);
}
/**
*構(gòu)造矩形打印對象
*@param x 矩形左上角 X 坐標(biāo)
*@param y 矩形左上角 y 坐標(biāo)
*@param width 矩形寬
*@param height 矩形高
*/
public PrintRectangle(int x,int y,int width ,int height){
px = x;
py = y;
w = width;
h = height;
}
/**
*設(shè)置矩形類型
*@param t 矩形類型
*/
public void setType(int t){
type = t;
}
/**
*設(shè)置3D矩形類型
*@param r 是否升起
*/
public void set3DType(boolean r){
raised =r;
}
/**
*設(shè)置圓角矩形的圓角
*@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、 打印內(nèi)容的基類的設(shè)計(jì)</SPAN></P>
<P>基類必須具有要有一個(gè)打印對象的容器,程序中使用了Vector對象。并且需要有將打印對象加入到容器中和取出的功能,具體設(shè)計(jì)如下:</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,執(zhí)行每個(gè)PrintObject對象的print方面
* @param g 將要打印的圖形環(huán)境
*/
public void print(Graphics g){
int i = vc.size();
Graphics2D g2d = (Graphics2D)g;
for(int j=0;j<i;j++){
getPrintO(j).print(g2d);
}
}
}
</CODE></PRE></TD></TR></TBODY></TABLE></P>
<P><A name=1_6></A><SPAN class=atitle3>6、 實(shí)際打印類的設(shè)計(jì)</SPAN></P>
<P>實(shí)際的打印類的設(shè)計(jì),可根據(jù)實(shí)際需要而定,添加必要的功能,但必須封裝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>采用以上方法編寫的代碼均是可重用的,實(shí)際編寫具體的打印程序時(shí),只需要將打印的內(nèi)容分解成相應(yīng)的打印對象,然后將打印對象加入到容器中即可。既使用一個(gè)或多個(gè)PrintAll對象(每個(gè)PrintAll對象作為一頁來處理),將分解的打印對象定義,設(shè)置相應(yīng)的屬性,然后使用PrintAll.putPrintO(PrintObject
po)方法加入到容器中。這樣編寫打印程序就無需再考慮繁瑣的邏輯了,只需分解對象,計(jì)算位置即可。是不是很爽呢?筆者在使用java開發(fā)一個(gè)金融項(xiàng)目時(shí),就成功使用了上述技術(shù),大大提高了工作效率。如需要更多的程序請和筆者聯(lián)系,地址:<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網(wǎng)站的打印用例:<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打印程序設(shè)計(jì)》:<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技術(shù)的愛好者,從jdk1.1開始,跟蹤使用java語言開發(fā)軟件(服務(wù)器端和客戶端),在《網(wǎng)絡(luò)世界》、《中國計(jì)算機(jī)報(bào)》、《計(jì)算機(jī)世界報(bào)》上發(fā)表了多篇java方面的技術(shù)文章。目前在中國人民銀行六安市中心支行科技科工作。<BR>聯(lián)系地址:安徽省六安市人民路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設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?files/c.gif"
width=10 border=0></TD></TR></TBODY></TABLE><!-- END PAPER BODY--><BR
clear=all><IMG height=10 alt="" src="用JAVA設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?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設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?files/c.gif"
width=5 border=0></TD></TR>
<TR vAlign=top>
<TD bgColor=#000000 colSpan=2><IMG height=1 alt=""
src="用JAVA設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?files/c.gif" width=100 border=0></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff colSpan=2><IMG height=8 alt=""
src="用JAVA設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?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設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?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設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?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設(shè)計(jì)面向?qū)ο蟮拇蛴〕绦?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> <A class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/index.shtml&origin=dwhead">關(guān)于
IBM</A><SPAN class=divider> | </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&origin=dwhead">隱私條約</A><SPAN
class=divider> | </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&origin=dwhead">使用條款</A><SPAN
class=divider> | </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&origin=dwhead">聯(lián)系
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>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -