|
Posted on 2008-03-15 22:35 沙漠中的鱼 阅读(807) 评论(0) 编辑 收藏 所属分类: 控件开发
以前用SWT 3232包的在WINDOWS下开发的打印程序,在linux下的包没有实现打印功能,不过最新的eclipse3.3下面已经实现了打印功能,但是需要linux2.10才可以,但是我们采用共创linux的内核只有2.06,只好改用AWT来实现,在网上找到一段简单的打印程序
1import java.awt.*;
2import java.awt.print.*;
3import java.io.*;
4import java.util.Vector;
5
6public 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
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
21 /** *//** Create a PageableText object for a string of text */
22 public PageableText(String text, PageFormat format) throws IOException {
23 this(new StringReader(text), format);
24 }
25
26 /** *//** Create a PageableText object for a file of text */
27 public PageableText(File file, PageFormat format) throws IOException {
28 this(new FileReader(file), format);
29 }
30
31 /** *//** Create a PageableText object for a stream of text */
32 public PageableText(Reader stream, PageFormat format) throws IOException {
33 this.format = format;
34
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
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
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
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 public int getNumberOfPages() { return numPages; }
56 public PageFormat getPageFormat(int pagenum) { return format; }
57 public Printable getPrintable(int pagenum) { return this; }
58
59 /** *//**
60 * This is the print() method of the Printable interface.
61 * It does most of the printing work.
62 */
63 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
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 if (baseline == -1) {
71 FontMetrics fm = g.getFontMetrics(font);
72 baseline = fm.getAscent();
73 }
74
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
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
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
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 for(int i=startLine; i <= endLine; i++) {
99 // Get the line
100 String line = (String)lines.elementAt(i);
101
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
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
118 /** *//**
119 * This is a test program that demonstrates the use of PageableText
120 */
121 public static void main(String[] args) throws IOException, PrinterException {
122 // Get the PrinterJob object that coordinates everything
123 PrinterJob job = PrinterJob.getPrinterJob();
124
125 // Get the default page format, then ask the user to customize it.
126 PageFormat format = job.pageDialog(job.defaultPage());
127
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
133 // Ask the user to select a printer, etc., and if not canceled, print!
134 if (job.printDialog())
135 job.print();
136 }
137}
138
139
|