-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreview.java
More file actions
321 lines (267 loc) · 9.8 KB
/
Preview.java
File metadata and controls
321 lines (267 loc) · 9.8 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package ToyProject;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.*;
import java.util.ArrayList;
import com.lamatek.swingextras.JFontChooser;
/**
* @author Jean-Pierre Dube <jpdube@videotron.ca>
* @ modified by Dongik Oh <dohdoh@sch.ac.kr> 2009-11-17
*/
public class Preview extends JFrame {
//--- Private instances declarations
//----------------------------------
private JPanel viewPanel = new JPanel ();
private PagePanel pagePanel = new PagePanel ();
private BorderLayout mainLayout = new BorderLayout ();
private BorderLayout pageLayout = new BorderLayout ();
private PreviewToolBar toolbar = new PreviewToolBar (this);
// ToyProject.Preview 창의 초기 크기. 이 값은 paint 메소드에서 zoom값이 1.0으로
// 세팅되었을 경우에 제대로 윤곽이 맞게 설정 된 것임
private Dimension preferredSize = new Dimension (650, 850);
private int pageIndex = 0;
private double zoom = 1.0;
private Color color = Color.black;
private Font currentFont = new Font("굴림", Font.PLAIN, 12);
private PrintObject title;
private RowObjects header;
private ArrayList<RowObjects> entireList;
private boolean pageOn;
private Book b;
private PageFormat fmt;
// 구성자
// 페이지의 제목, 칼럼헤딩, 전체 행의 리스트, 페이지번호를 출력할지 여부를 제공
public Preview(PrintObject title, RowObjects header, ArrayList<RowObjects> entireList, boolean pageOn) {
super();
this.title = title;
this.header = header;
this.entireList = entireList;
this.pageOn = pageOn;
preparePages();
}
private void preparePages() {
Paper p = new Paper(); // 먼저 종이를 설정하고
PageFormat fmt = new PageFormat(); // 페이지 포멧에 종이 등록
fmt.setPaper(p);
b= new Book(); // 어러 페이지가 들어갈 Book을 만듬
// strList의 내용을 30줄 단위로 잘라서 출력할 수 있는 페이지를 만드는 논리 필요
ArrayList<RowObjects> choppedList = new ArrayList<RowObjects>();
int i;
for (i=0; i < entireList.size(); i++) {
choppedList.add(entireList.get(i));
if (i%29 == 0 && i !=0) { // 한페이지에 30줄만 허용하기 (우선은)
b.append(new PrinterPage(40, title, header, choppedList, pageOn), fmt);
choppedList = new ArrayList<RowObjects>();
}
}
if (choppedList.size() != 0) { // 다 차지 않은 페이지도 마지막 페이지로
b.append(new PrinterPage(40, title, header, choppedList, pageOn), fmt);
}
}
public void zoomIn () {
if (zoom < 1.4) // 두배 이상은 크게 안함
zoom += 0.1; // 원래 크기의 10% 씩 크게 함
renderPage();
}
public void zoomOut () {
if (zoom > 0.7) // 1/2 이상은 작게 안함
zoom -= 0.1; // 원래 크기의 10% 씩 작게 함
renderPage();
}
public void font() {
JFontChooser fc = new JFontChooser(currentFont);
if (fc.showDialog(this, "폰트선택자") == JFontChooser.ACCEPT_OPTION)
currentFont = fc.getSelectedFont();
renderPage();
}
public void color () {
JFrame frame = new JFrame("색상선택");
color=JColorChooser.showDialog(frame, "색상선택", Color.black);
renderPage();
}
public void nextPage () {
pageIndex++;
if (pageIndex > b.getNumberOfPages()-1)
pageIndex--;
renderPage ();
}
public void previousPage () {
pageIndex--;
if (pageIndex < 0)
pageIndex = 0;
renderPage ();
}
public void firstPage () {
pageIndex = 0;
renderPage ();
}
public void lastPage () {
pageIndex = b.getNumberOfPages () - 1;
renderPage ();
}
public void print () {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPageable(b);
if (pj.printDialog()) {
try {
pj.print();
} catch (PrinterException pex) {
System.out.println("프린팅 중 에러 발생 " + pex.getMessage());
}
}
}
// 이 메소드에서 현재 페이지를 출력해 주면 됨
private void renderPage () {
pagePanel.renderPage (b.getPrintable(pageIndex));
}
public Dimension getPreferredSize () {
return (preferredSize);
}
public void preview () {
this.getContentPane ().setLayout (mainLayout);
this.getContentPane ().add (toolbar, BorderLayout.NORTH);
this.getContentPane ().add (pagePanel, BorderLayout.CENTER);
/* 아래 와 같이 스크롤 패인을 사용할 수도 있으나, 글자의 크기를 직접바꾸어 주지 않는 방식을
* paint()에서 사용하므로, 그냥 창을 늘리게 하는 것이 나을 것 같다.
this.getContentPane ().setLayout (mainLayout);
JScrollPane js = new JScrollPane(pagePanel);
js.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
js.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
this.getContentPane ().add (toolbar, BorderLayout.NORTH);
this.getContentPane ().add (js, BorderLayout.CENTER);
*/
this.setTitle ("Print preview");
this.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
this.pack ();
//--- Init the pagePanel
pagePanel.setBackground (Color.white);
pagePanel.setBorder (BorderFactory.createLineBorder (Color.lightGray, 10));
pagePanel.setPreferredSize (preferredSize);
renderPage ();
}
private class PagePanel extends JPanel {
private PrinterPage page;
public PagePanel () {
super ();
}
// 해당되는 페이지를 지정하고 repaint()를 호출하여 출력되게 함 (paint()가 호출됨)
public void renderPage (Printable parPage) {
page = (PrinterPage)parPage;
repaint ();
}
public void paint (Graphics parG) {
super.paint (parG);
Dimension size = this.getSize ();
BufferedImage doubleBuffer = new BufferedImage (size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) doubleBuffer.getGraphics ();
g2d.setColor (Color.white); // 배경색
g2d.fillRect (0, 0, size.width, size.height); // 흰색 배경을 만들고
g2d.scale (zoom, zoom); // zoom 비율에 따라 출력크기 조정
g2d.setColor (color); // 출력색
g2d.setFont(currentFont);
if (page != null)
page.print (g2d, new PageFormat (), 0);
if (doubleBuffer != null)
parG.drawImage (doubleBuffer, 0, 0, this);
}
}
public class PreviewToolBar extends JToolBar implements ActionListener {
//--- Private instances declarations
//----------------------------------
//--- Buttons
private JButton firstPage = new JButton ();
private JButton lastPage = new JButton ();
private JButton nextPage = new JButton ();
private JButton previousPage = new JButton ();
private JButton zoomIn = new JButton ();
private JButton zoomOut = new JButton ();
private JButton font = new JButton ();
private JButton color = new JButton ();
private JButton print = new JButton ();
Preview preview;
public PreviewToolBar (Preview parPreview) {
super ();
preview = parPreview;
init ();
}
private void init () {
// 툴바에 사용할 버튼들의 설정
firstPage.setText("첫페이지");
firstPage.setActionCommand ("firstPage");
firstPage.addActionListener (this);
previousPage.setText("이전페이지");
previousPage.setActionCommand ("previousPage");
previousPage.addActionListener (this);
nextPage.setText("다음페이지");
nextPage.setActionCommand ("nextPage");
nextPage.addActionListener (this);
lastPage.setText("마지막페이지");
lastPage.setActionCommand ("lastPage");
lastPage.addActionListener (this);
zoomIn.setText("페이지확대");
zoomIn.setActionCommand ("zoomIn");
zoomIn.addActionListener (this);
zoomOut.setText("페이지축소");
zoomOut.setActionCommand ("zoomOut");
zoomOut.addActionListener (this);
font.setText("폰트");
font.setActionCommand ("font");
font.addActionListener (this);
color.setText("색상");
color.setActionCommand ("color");
color.addActionListener (this);
print.setText("출력");
print.setActionCommand ("print");
print.addActionListener (this);
// toolbar의 초기화
this.add (firstPage);
this.add (previousPage);
this.add (nextPage);
this.add (lastPage);
this.add (zoomIn);
this.add (zoomOut);
this.add (font);
this.add (color);
this.add (print);
}
public void actionPerformed (ActionEvent parEvent) {
String command = parEvent.getActionCommand ();
if (command.equals ("nextPage"))
preview.nextPage ();
else if (command.equals ("previousPage"))
preview.previousPage ();
else if (command.equals ("firstPage"))
preview.firstPage ();
else if (command.equals ("lastPage"))
preview.lastPage ();
else if (command.equals ("zoomIn"))
preview.zoomIn();
else if (command.equals ("zoomOut"))
preview.zoomOut();
else if (command.equals ("font"))
preview.font();
else if (command.equals ("color"))
preview.color();
else if (command.equals ("print"))
preview.print ();
}
}
}// ToyProject.Preview