-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintableText.java
46 lines (34 loc) · 1.01 KB
/
PrintableText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
*
*/
import java.awt.*;
import java.awt.print.*;
import java.awt.geom.*;
import java.awt.font.*;
public class PrintableText implements Printable {
String text;
int POINTS_PER_INCH;
public PrintableText(String t) {
POINTS_PER_INCH = 72;
text = t;
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g; // Allow use of Java 2 graphics on
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setPaint(Color.black);
Point2D.Double pen = new Point2D.Double(0.25 * POINTS_PER_INCH, 0.25 * POINTS_PER_INCH);
Font font = new Font ("courier", Font.PLAIN, 12);
FontRenderContext frc = g2d.getFontRenderContext();
String lines[] = text.split("\n");
for (int i=0; i < lines.length; i++) {
if (lines[i].length() > 0) {
TextLayout layout = new TextLayout(lines[i], font, frc);
layout.draw(g2d, (float) pen.x, (float) (pen.y + i*14));
}
}
return PAGE_EXISTS;
}
}