|
Posted on 2008-03-15 22:35 沙漠中的鱼 阅读(812) 评论(0) 编辑 收藏 所属分类: 控件开发
以前用SWT 3232包的在WINDOWS下开发的打印程序,在linux下的包没有实现打印功能,不过最新的eclipse3.3下面已经实现了打印功能,但是需要linux2.10才可以,但是我们采用共创linux的内核只有2.06,只好改用AWT来实现,在网上找到一段简单的打印程序
1 import java.awt.*;
2 import java.awt.print.*;
3 import java.io.*;
4 import java.util.Vector;
5![](/Images/OutliningIndicators/None.gif)
6![](/Images/OutliningIndicators/ExpandedBlockStart.gif) public class PageableText implements Pageable, Printable {
7 // Constants for font name, size, style and line spacing
8 public static String FONTFAMILY = "Monospaced";
9 public static int FONTSIZE = 10;
10 public static int FONTSTYLE = Font.PLAIN;
11 public static float LINESPACEFACTOR = 1.1f;
12![](/Images/OutliningIndicators/InBlock.gif)
13 PageFormat format; // The page size, margins, and orientation
14 Vector lines; // The text to be printed, broken into lines
15 Font font; // The font to print with
16 int linespacing; // How much space between lines
17 int linesPerPage; // How many lines fit on a page
18 int numPages; // How many pages required to print all lines
19 int baseline = -1; // The baseline position of the font.
20![](/Images/OutliningIndicators/InBlock.gif)
21![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /** *//** Create a PageableText object for a string of text */
22![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public PageableText(String text, PageFormat format) throws IOException {
23 this(new StringReader(text), format);
24 }
25![](/Images/OutliningIndicators/InBlock.gif)
26![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /** *//** Create a PageableText object for a file of text */
27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public PageableText(File file, PageFormat format) throws IOException {
28 this(new FileReader(file), format);
29 }
30![](/Images/OutliningIndicators/InBlock.gif)
31![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /** *//** Create a PageableText object for a stream of text */
32![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public PageableText(Reader stream, PageFormat format) throws IOException {
33 this.format = format;
34![](/Images/OutliningIndicators/InBlock.gif)
35 // First, read all the text, breaking it into lines.
36 // This code ignores tabs, and does not wrap long lines.
37 BufferedReader in = new BufferedReader(stream);
38 lines = new Vector();
39 String line;
40 while((line = in.readLine()) != null)
41 lines.addElement(line);
42![](/Images/OutliningIndicators/InBlock.gif)
43 // Create the font we will use, and compute spacing between lines
44 font = new Font(FONTFAMILY, FONTSTYLE, FONTSIZE);
45 linespacing = (int) (FONTSIZE * LINESPACEFACTOR);
46![](/Images/OutliningIndicators/InBlock.gif)
47 // Figure out how many lines per page, and how many pages
48 linesPerPage = (int)Math.floor(format.getImageableHeight()/linespacing);
49 numPages = (lines.size()-1)/linesPerPage + 1;
50 }
51![](/Images/OutliningIndicators/InBlock.gif)
52 // These are the methods of the Pageable interface.
53 // Note that the getPrintable() method returns this object, which means
54 // that this class must also implement the Printable interface.
55![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public int getNumberOfPages() { return numPages; }
56![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public PageFormat getPageFormat(int pagenum) { return format; }
57![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public Printable getPrintable(int pagenum) { return this; }
58![](/Images/OutliningIndicators/InBlock.gif)
59![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /** *//**
60 * This is the print() method of the Printable interface.
61 * It does most of the printing work.
62 */
63![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public int print(Graphics g, PageFormat format, int pagenum) {
64 // Tell the PrinterJob if the page number is not a legal one.
65 if ((pagenum < 0) | (pagenum >= numPages))
66 return NO_SUCH_PAGE;
67![](/Images/OutliningIndicators/InBlock.gif)
68 // First time we're called, figure out the baseline for our font.
69 // We couldn't do this earlier because we needed a Graphics object
70![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if (baseline == -1) {
71 FontMetrics fm = g.getFontMetrics(font);
72 baseline = fm.getAscent();
73 }
74![](/Images/OutliningIndicators/InBlock.gif)
75 // Clear the background to white. This shouldn't be necessary, but is
76 // required on some systems to workaround an implementation bug
77 g.setColor(Color.white);
78 g.fillRect((int)format.getImageableX(), (int)format.getImageableY(),
79 (int)format.getImageableWidth(),
80 (int)format.getImageableHeight());
81![](/Images/OutliningIndicators/InBlock.gif)
82 // Set the font and the color we will be drawing with.
83 // Note that you cannot assume that black is the default color!
84 g.setFont(font);
85 g.setColor(Color.black);
86![](/Images/OutliningIndicators/InBlock.gif)
87 // Figure out which lines of text we will print on this page
88 int startLine = pagenum * linesPerPage;
89 int endLine = startLine + linesPerPage - 1;
90 if (endLine >= lines.size())
91 endLine = lines.size()-1;
92![](/Images/OutliningIndicators/InBlock.gif)
93 // Compute the position on the page of the first line.
94 int x0 = (int) format.getImageableX();
95 int y0 = (int) format.getImageableY() + baseline;
96 System.out.print("x:"+x0+",y:"+y0);
97 // Loop through the lines, drawing them all to the page.
98![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(int i=startLine; i <= endLine; i++) {
99 // Get the line
100 String line = (String)lines.elementAt(i);
101![](/Images/OutliningIndicators/InBlock.gif)
102 // Draw the line.
103 // We use the integer version of drawString(), not the Java 2D
104 // version that uses floating-point coordinates. A bug in early
105 // Java2 implementations prevents the Java 2D version from working.
106 if (line.length() > 0)
107 g.drawString(line, x0, y0);
108![](/Images/OutliningIndicators/InBlock.gif)
109 // Move down the page for the next line.
110 y0 += linespacing;
111 }
112 g.setColor(Color.red);
113 g.drawString("test", 20, 20);
114 // Tell the PrinterJob that we successfully printed the page.
115 return PAGE_EXISTS;
116 }
117![](/Images/OutliningIndicators/InBlock.gif)
118![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /** *//**
119 * This is a test program that demonstrates the use of PageableText
120 */
121![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public static void main(String[] args) throws IOException, PrinterException {
122 // Get the PrinterJob object that coordinates everything
123 PrinterJob job = PrinterJob.getPrinterJob();
124![](/Images/OutliningIndicators/InBlock.gif)
125 // Get the default page format, then ask the user to customize it.
126 PageFormat format = job.pageDialog(job.defaultPage());
127![](/Images/OutliningIndicators/InBlock.gif)
128
129 System.out.println("height:"+format.getHeight()+",mHeight:"+format.getImageableHeight());
130 // Create our PageableText object, and tell the PrinterJob about it
131 job.setPageable(new PageableText(new File("PageableText.java"), format));
132![](/Images/OutliningIndicators/InBlock.gif)
133 // Ask the user to select a printer, etc., and if not canceled, print!
134 if (job.printDialog())
135 job.print();
136 }
137 }
138![](/Images/OutliningIndicators/None.gif)
139![](/Images/OutliningIndicators/None.gif)
|