1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package unbbayes.controller;
22
23 import java.awt.Dimension;
24 import java.awt.Graphics;
25 import java.awt.print.PageFormat;
26 import java.awt.print.Pageable;
27 import java.awt.print.Printable;
28
29 import javax.swing.JTextArea;
30
31
32
33
34 public class PrintText implements Printable, Pageable {
35
36 protected JTextArea texto;
37 protected PageFormat pageFormat;
38
39 public PrintText(JTextArea texto, PageFormat pf) {
40 this.texto = texto;
41 pageFormat = pf;
42 }
43
44
45
46
47 public int print(Graphics g, PageFormat pf, int index) {
48 Dimension size = new Dimension();
49
50 if ((texto.getWidth() == 0) || (texto.getHeight() == 0)) {
51 texto.setSize(texto.getPreferredSize());
52 }
53
54 int textHeight = texto.getHeight();
55
56 int positionX = 0;
57 int positionY = 0;
58
59
60 int pageIndex = 0;
61 while (positionY < textHeight) {
62 positionX = 0;
63
64 size.setSize(getPrintSize(positionX, positionY));
65 if (pageIndex == index) {
66
67 paintText(g, positionX, positionY, size);
68 return Printable.PAGE_EXISTS;
69 }
70 pageIndex++;
71 positionX += size.width;
72
73 positionY += size.height;
74 }
75 return Printable.NO_SUCH_PAGE;
76 }
77
78
79
80
81
82 protected Dimension getPrintSize(int positionX, int positionY) {
83
84 int maxWidth = (int)(pageFormat.getImageableWidth());
85 int maxHeight = (int)(pageFormat.getImageableHeight());
86
87
88 return new Dimension(maxWidth, maxHeight);
89 }
90
91
92
93
94 protected void paintText(Graphics g, int positionX, int positionY,
95 Dimension size) {
96 int offsetX = (int)(pageFormat.getImageableX());
97 int offsetY = (int)(pageFormat.getImageableY());
98 g.translate(offsetX - positionX, offsetY - positionY);
99 g.clipRect(positionX, positionY, size.width, size.height);
100 texto.paint(g);
101 }
102
103
104
105
106 public int getNumberOfPages() {
107 Dimension size = new Dimension();
108 int textHeight = texto.getHeight();
109 int positionX = 0;
110 int positionY = 0;
111
112 int pageIndex = 0;
113 while (positionY < textHeight) {
114 positionX = 0;
115
116 size.setSize(getPrintSize(positionX, positionY));
117 positionX += size.width;
118 pageIndex++;
119
120 positionY += size.height;
121 }
122 return pageIndex;
123 }
124
125 public Printable getPrintable(int index) {
126 return this;
127 }
128
129 public PageFormat getPageFormat(int index) {
130 return pageFormat;
131 }
132 }