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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100167.html;106,k100169.html;107,k100171.html;108,k100173.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100167.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100169.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100171.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100173.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 26 - Drucken
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id026003"></a>
<h2>26.3 Drucken seit dem JDK 1.2 </h2>
<hr>
<ul>
<li><a href="k100170.html#sectlevel2id026003">26.3 Drucken seit dem JDK 1.2</a>
<ul>
<li><a href="k100170.html#sectlevel3id026003001">26.3.1 Überblick</a>
<li><a href="k100170.html#sectlevel3id026003002">26.3.2 Zusammenspiel der Klassen</a>
<ul>
<li><a href="k100170.html#sectlevel4id026003002001">Die Klasse PrinterJob</a>
<li><a href="k100170.html#sectlevel4id026003002002">Die Klassen Paper und PageFormat</a>
<li><a href="k100170.html#sectlevel4id026003002003">Die Konfigurationsdialoge</a>
<li><a href="k100170.html#sectlevel4id026003002004">Das Interface Printable</a>
<li><a href="k100170.html#sectlevel4id026003002005">Das Interface Pageable und die Klasse Book</a>
</ul>
<li><a href="k100170.html#sectlevel3id026003003">26.3.3 Ausdrucken einer Textdatei</a>
<ul>
<li><a href="k100170.html#sectlevel4id026003003001">Die Hilfsklasse FilePrintHelper</a>
<li><a href="k100170.html#sectlevel4id026003003002">Die Klasse SimpleFilePrinter</a>
</ul>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id026003001"></a>
<h3>26.3.1 Überblick </h3>
<p>
Seit dem JDK 1.2 wurde ein anderer Ansatz verfolgt. Anstelle der im
vorigen Abschnitt erwähnten Techniken sind nun die Klassen und
Interfaces aus dem Paket <a name="ixa101691"><a href="index_j.html#ixb101303"><font color=#000080><tt>java.awt.print</tt></font></a></a>
zu verwenden, die zwei unterschiedliche Arten von Funktionen zur Verfügung
stellen:
<ul>
<li>Die Klasse <a name="ixa101692"><a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a></a>
ist für die <i>Kontrolle des Druckauftrags</i> verantwortlich.
Sie dient zum Aufrufen der plattformspezifischen Konfigurationsdialoge,
registriert die Objekte zur Seitenausgabe und startet den eigentlichen
Druckvorgang.
<li>Der <i>eigentliche Ausdruck</i> wird von Objekten des Typs <a name="ixa101693"><a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a></a>
oder <a name="ixa101694"><a href="index_p.html#ixb101306"><font color=#000080><tt>Pageable</tt></font></a></a>
erledigt. Sie werden beim <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
registriert und zu geeigneter Zeit von diesem mit der Ausgabe der
Druckdaten beauftragt.
</ul>
<p>
Anders als im JDK 1.1 stehen nun <i>zwei</i> Konfigurationsdialoge
zur Verfügung. Mit dem einen können Job-Parameter wie <i>Anzahl
der Kopien</i>, <i>auszudruckende Seiten</i> oder der <i>Druckertyp</i>
eingestellt werden. Der andere dient zur Einstellung von Seitenparametern,
mit dem etwa das Papierformat, die Seitengröße oder die
Randeinstellungen konfiguriert werden können. Beide Dialoge sind
optional und können auch unterdrückt werden. Alle mit ihrer
Hilfe konfigurierbaren Parameter können auch programmgesteuert
verändert werden.
<!-- Section -->
<a name="sectlevel3id026003002"></a>
<h3>26.3.2 Zusammenspiel der Klassen </h3>
<!-- Section -->
<a name="sectlevel4id026003002001"></a>
<h4>Die Klasse PrinterJob </h4>
<p>
Die abstrakte Klasse <a name="ixa101695"><a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a></a>
aus dem Paket <a href="index_j.html#ixb101303"><font color=#000080><tt>java.awt.print</tt></font></a>
repräsentiert einen Druckauftrag und ist Ausgangspunkt für
alle Druckaktivitäten mit dem JDK 1.2. Sie besitzt eine statische
Methode <a name="ixa101696"><a href="index_g.html#ixb101307"><font color=#000080><tt>getPrinterJob</tt></font></a></a>,
mit der Instanzen erzeugt werden können:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public static PrinterJob getPrinterJob()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/PrinterJob.html" onClick="this.href=getApiDoc('java.awt.print.PrinterJob')"><font color="#660066" size=-1>java.awt.print.PrinterJob</font></a></td>
</tr>
</table>
<p>
<a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
besitzt Methoden, mit denen einige globale Eigenschaften des Druckauftrags
kontrolliert werden können:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public String getUserName()
public int getCopies()
public void setCopies(int copies)
public void setJobName(String jobName)
public String getJobName()
public void cancel()
public boolean isCancelled()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/PrinterJob.html" onClick="this.href=getApiDoc('java.awt.print.PrinterJob')"><font color="#660066" size=-1>java.awt.print.PrinterJob</font></a></td>
</tr>
</table>
<p>
<a name="ixa101697"><a href="index_g.html#ixb101308"><font color=#000080><tt>getUserName</tt></font></a></a>
liefert den Namen des Benutzers, der den Job gestartet hat. <a name="ixa101698"><a href="index_g.html#ixb101309"><font color=#000080><tt>getCopies</tt></font></a></a>
und <a name="ixa101699"><a href="index_s.html#ixb101310"><font color=#000080><tt>setCopies</tt></font></a></a>
dienen zum Zugriff auf die Anzahl der Kopien, die ausgedruckt werden
sollen. Mit <a name="ixa101700"><a href="index_g.html#ixb101311"><font color=#000080><tt>getJobName</tt></font></a></a>
und <a name="ixa101701"><a href="index_s.html#ixb101312"><font color=#000080><tt>setJobName</tt></font></a></a>
kann der Name des Druckjobs abgefragt bzw. eingestellt werden. Durch
Aufruf von <a name="ixa101702"><a href="index_c.html#ixb101313"><font color=#000080><tt>cancel</tt></font></a></a>
kann ein laufender Druckjob abgebrochen werden. <a name="ixa101703"><a href="index_i.html#ixb101314"><font color=#000080><tt>isCancelled</tt></font></a></a>
liefert <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>,
falls während eines laufenden Druckjobs <a href="index_c.html#ixb101313"><font color=#000080><tt>cancel</tt></font></a>
aufgerufen wurde und der Job bei der nächstmöglichen Gelegenheit
beendet wird.
<!-- Section -->
<a name="sectlevel4id026003002002"></a>
<h4>Die Klassen Paper und PageFormat </h4>
<p>
Neben den eigentlichen Druckdaten besitzt eine auszudruckende Seite
globale Eigenschaften wie Papiergröße und Randeinstellungen.
Diese werden durch die Klassen <a name="ixa101704"><a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a></a>
und <a name="ixa101705"><a href="index_p.html#ixb101316"><font color=#000080><tt>Paper</tt></font></a></a>
gekapselt. <a href="index_p.html#ixb101316"><font color=#000080><tt>Paper</tt></font></a>
repräsentiert ein Blatt Papier mit festgelegten Abmessungen für
Breite, Höhe und bedruckbaren Bereich:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public double getHeight()
public double getWidth()
public double getImageableX()
public double getImageableY()
public double getImageableWidth()
public double getImageableHeight()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/Paper.html" onClick="this.href=getApiDoc('java.awt.print.Paper')"><font color="#660066" size=-1>java.awt.print.Paper</font></a></td>
</tr>
</table>
<p>
<a name="ixa101706"><a href="index_g.html#ixb101240"><font color=#000080><tt>getHeight</tt></font></a></a>
und <a name="ixa101707"><a href="index_g.html#ixb101317"><font color=#000080><tt>getWidth</tt></font></a></a>
liefern die Höhe bzw. Breite des Papierblatts. <a name="ixa101708"><a href="index_g.html#ixb101318"><font color=#000080><tt>getImageableX</tt></font></a></a>
und <a name="ixa101709"><a href="index_g.html#ixb101319"><font color=#000080><tt>getImageableY</tt></font></a></a>
geben den Abstand des bedruckbaren Bereichs vom oberen bzw. linken
Blattrand an. <a name="ixa101710"><a href="index_g.html#ixb101320"><font color=#000080><tt>getImageableWidth</tt></font></a></a>
und <a name="ixa101711"><a href="index_g.html#ixb101321"><font color=#000080><tt>getImageableHeight</tt></font></a></a>
geben die Breite bzw. Höhe des bedruckbaren Bereichs an.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
<a href="index_p.html#ixb101316"><font color=#000080><tt>Paper</tt></font></a>
speichert und liefert alle Parameter in einer Einheit von 1/72 Zoll.
Das entspricht etwa der typografischen Maßeinheit »Punkt«
und ist etwas länger als ein Drittel Millimeter. Ein derartiges
Raster repräsentiert die Auflösung eines durchschnittlichen
Monitors und ist für Druckausgaben viel zu grob. Glücklicherweise
werden die Werte als Fließkommazahlen gespeichert und lassen
sich zur Erhöhung der Genauigkeit skalieren. Wir werden weiter
unter auf die dazu nötigen Techniken zurückkommen.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Die Klasse <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>
kapselt ein <a href="index_p.html#ixb101316"><font color=#000080><tt>Paper</tt></font></a>-Objekt
und dessen Orientierung:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public static final int PORTRAIT
public static final int LANDSCAPE
public static final int REVERSE_LANDSCAPE
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/PageFormat.html" onClick="this.href=getApiDoc('java.awt.print.PageFormat')"><font color="#660066" size=-1>java.awt.print.PageFormat</font></a></td>
</tr>
</table>
<p>
Ist die Orientierung <a name="ixa101712"><a href="index_p.html#ixb101322"><font color=#000080><tt>PORTRAIT</tt></font></a></a>,
so wird das Blatt in seiner Standardausrichtung verwendet. Es ist
also höher als breit, und der Koordinatenursprung befindet sich
links oben. Beim Drucken im <a name="ixa101713"><a href="index_l.html#ixb101323"><font color=#000080><tt>LANDSCAPE</tt></font></a></a>-Modus
wird das Papier dagegen quer verwendet, und der rechte Rand des Ausdrucks
liegt am oberen Blattrand. <a name="ixa101714"><a href="index_r.html#ixb101324"><font color=#000080><tt>REVERSE_LANDSCAPE</tt></font></a></a>
druckt ebenfalls quer, aber mit umgekehrter Druckrichtung.
<p>
<a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>
stellt Methoden zum Zugriff auf das Papier, seine Orientierung und
die aus <a href="index_p.html#ixb101316"><font color=#000080><tt>Paper</tt></font></a>
bekannten Methoden zum Zugriff auf die Seitenabmessungen zur Verfügung:
<a name="ixa101715"></a> <a name="ixa101716"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public Paper getPaper()
public int getOrientation()
public double getHeight()
public double getWidth()
public double getImageableX()
public double getImageableY()
public double getImageableWidth()
public double getImageableHeight()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/Paper.html" onClick="this.href=getApiDoc('java.awt.print.Paper')"><font color="#660066" size=-1>java.awt.print.Paper</font></a></td>
</tr>
</table>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Im Gegensatz zu <a href="index_p.html#ixb101316"><font color=#000080><tt>Paper</tt></font></a>
sind die Rückgabewerte nun aber an die Orientierung angepasst.
Wird also beispielsweise im <a href="index_l.html#ixb101323"><font color=#000080><tt>LANDSCAPE</tt></font></a>-Modus
ausgedruckt, liefert <a href="index_g.html#ixb101240"><font color=#000080><tt>getHeight</tt></font></a>
die Breite des Blattes und <a href="index_g.html#ixb101317"><font color=#000080><tt>getWidth</tt></font></a>
dessen Höhe. Die anderen Methoden verhalten sich analog.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Ein <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>-Objekt
wird gewöhnlich mit der Methode <a name="ixa101717"><a href="index_d.html#ixb101327"><font color=#000080><tt>defaultPage</tt></font></a></a>
der Klasse <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
beschafft:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public PageFormat defaultPage()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/PrinterJob.html" onClick="this.href=getApiDoc('java.awt.print.PrinterJob')"><font color="#660066" size=-1>java.awt.print.PrinterJob</font></a></td>
</tr>
</table>
<p>
Es kann dann entweder programmgesteuert oder durch Aufruf des Seitendialogs
konfiguriert werden.
<!-- Section -->
<a name="sectlevel4id026003002003"></a>
<h4>Die Konfigurationsdialoge </h4>
<p>
Die Klasse <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
besitzt zwei Konfigurationsdialoge. Der erste dient zur Konfiguration
eines <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>-Objekts
und wird mit <a name="ixa101718"><a href="index_p.html#ixb101328"><font color=#000080><tt>pageDialog</tt></font></a></a>
aufgerufen. Der zweite dient zur Konfiguration der globalen Job-Parameter
und wird mit <a name="ixa101719"><a href="index_p.html#ixb101329"><font color=#000080><tt>printDialog</tt></font></a></a>
aufgerufen:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public PageFormat pageDialog(PageFormat page)
public abstract boolean printDialog()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/PrinterJob.html" onClick="this.href=getApiDoc('java.awt.print.PrinterJob')"><font color="#660066" size=-1>java.awt.print.PrinterJob</font></a></td>
</tr>
</table>
<p>
<a href="index_p.html#ixb101328"><font color=#000080><tt>pageDialog</tt></font></a>
erwartet ein <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>-Objekt
(das typischerweise durch <a href="index_d.html#ixb101327"><font color=#000080><tt>defaultPage</tt></font></a>
erzeugt wird) und bietet dem Anwender die Möglichkeit, dessen
Eigenschaften zu verändern. Die dafür verwendeten Dialoge
sind plattformabhängig, die Windows-Variante ist in <a href="k100170.html#druckseitendialog">Abbildung 26.2</a>
zu sehen.
<p>
Der Rückgabewert von <a href="index_p.html#ixb101328"><font color=#000080><tt>pageDialog</tt></font></a>
ist das konfigurierte <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>-Objekt.
Hat der Anwender den Dialog mit »OK« beendet, wird eine
Kopie des als Parameter übergebenen Objekts erzeugt, mit den
Einstellungen des Anwenders versehen und an den Aufrufer zurückgegeben.
Hat der Anwender den Dialog dagegen abgebrochen, wird das unveränderte
Originalobjekt zurückgegeben. Durch Vergleichen der beiden Objektreferenzen
kann also unterschieden werden, ob der Druckvorgang fortgesetzt oder
abgebrochen werden soll.
<p>
<a name="druckseitendialog"></a>
<img src="images/SeiteEinrichten.gif">
<p>
<p><i>
Abbildung 26.2: Der Dialog zur Druckseitenkonfiguration unter Windows</i></p>
<p>
Durch Aufruf von <a name="ixa101719"><a href="index_p.html#ixb101329"><font color=#000080><tt>printDialog</tt></font></a></a>
wird der plattformspezifische Dialog zur Jobkonfiguration aufgerufen
(siehe <a href="k100170.html#druckjobdialog">Abbildung 26.3</a>. Er
konfiguriert das <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>-Objekt,
und die Änderungen können mit den oben beschriebenen Methoden
abgefragt werden. Der boolesche Rückgabewert zeigt an, ob der
Dialog mit »OK« beendet oder abgebrochen wurde.
<p>
<a name="druckjobdialog"></a>
<img src="images/DruckerKonfig.gif">
<p>
<p><i>
Abbildung 26.3: Der Dialog zur Druckjobkonfiguration unter Windows</i></p>
<!-- Section -->
<a name="sectlevel4id026003002004"></a>
<h4>Das Interface Printable </h4>
<p>
Nachdem wir die Konfiguration der Job- und Seiteneinstellungen besprochen
haben, wollen wir uns nun der Frage zuwenden, wie die eigentliche
Druckausgabe erzeugt wird. Dazu gibt es im Paket <a href="index_j.html#ixb101303"><font color=#000080><tt>java.awt.print</tt></font></a>
ein Interface <a name="ixa101720"><a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a></a>,
das nur eine Methode enthält:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/Printable.html" onClick="this.href=getApiDoc('java.awt.print.Printable')"><font color="#660066" size=-1>java.awt.print.Printable</font></a></td>
</tr>
</table>
<p>
Ein Programm, das Druckausgaben erzeugen will, muss ein Objekt zur
Verfügung stellen, das dieses Interface implementiert. Es muss
mit der Methode <a name="ixa101721"><a href="index_s.html#ixb101330"><font color=#000080><tt>setPrintable</tt></font></a></a>
an den <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
übergeben werden und wird von <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
automatisch aufgerufen, nachdem der Druckvorgang mit <a name="ixa101722"><a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a></a>
gestartet wurde:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void setPrintable(Printable painter)
public void setPrintable(Printable painter, PageFormat format)
public void print()
throws PrinterException
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/PrinterJob.html" onClick="this.href=getApiDoc('java.awt.print.PrinterJob')"><font color="#660066" size=-1>java.awt.print.PrinterJob</font></a></td>
</tr>
</table>
<p>
<a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>
sorgt dabei für die Druckausgabe, ähnlich wie es <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>
für die Bildschirmausgabe bei GUI-Komponenten tut. Jeder (von
<a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
initiierte) Aufruf von <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
fordert das <a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>-Objekt
auf, eine bestimmte Seite auszudrucken. Der Seitenindex wird dabei
in dem Parameter <font color="#000077"><tt>page</tt></font> übergeben,
dessen Zählung bei 0 beginnt.
<p>
Zur Erzeugung von Druckausgaben wird das übergebene <a href="index_g.html#ixb101149"><font color=#000080><tt>Graphics</tt></font></a>-Objekt
verwendet, das wie bei der Erzeugung von Bildschirmausgaben verwendet
werden kann. Das übergebene Objekt kann dabei in ein <a name="ixa101723"><a href="index_g.html#ixb101331"><font color=#000080><tt>Graphics2D</tt></font></a></a>-Objekt
konvertiert werden und stellt so die erweiterten Funktionen des 2D-APIs
zur Verfügung.
<p>
Weiterhin wird das konfigurierte <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>
als Argument an <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
übergeben. Innerhalb von <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
wird es dazu benutzt, die Größe des Papiers und dessen
bedruckbaren Bereich zu ermitteln.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Es ist sehr wichtig, zu wissen, dass <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
für ein und dieselbe Seite mehrfach aufgerufen werden kann (und
normalerweise auch wird). Da die JDK-Dokumentation keine Aussagen
darüber macht, wie oft und in welcher Reihenfolge die Seiten
abgefordert werden, muss das <a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>-Objekt
selbst für eine geeignete Verwaltung der benötigten Statusinformationen
sorgen.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Der Rückgabewert von <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
gibt an, ob die Seitenzahl gültig war und die Druckausgabe erzeugt
werden konnte, oder ob eine ungültige Seitenzahl übergeben
wurde. Im ersten Fall gibt <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
die in <a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>
definierte Konstante <a name="ixa101724"><a href="index_p.html#ixb101332"><font color=#000080><tt>PAGE_EXISTS</tt></font></a></a>
zurück, im zweiten Fall <a name="ixa101725"><a href="index_n.html#ixb101333"><font color=#000080><tt>NO_SUCH_PAGE</tt></font></a></a>.
Da der <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
nicht von Anfang an weiß, wie viele Seiten insgesamt ausgedruckt
werden sollen, muss er beim Aufruf von <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
schrittweise den Seitenzähler erhöhen und darauf warten,
dass <a href="index_n.html#ixb101333"><font color=#000080><tt>NO_SUCH_PAGE</tt></font></a>
zurückgegeben wird. Danach erfolgen keine weiteren Aufrufe von
<a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Will das <a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>-Objekt
den kompletten Job während der Ausgabe abbrechen, braucht es
dazu lediglich während des Aufrufs von <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
eine <a name="ixa101726"><a href="index_p.html#ixb101334"><font color=#000080><tt>PrinterException</tt></font></a></a>
auszulösen. Diese wird vom <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>
abgefangen und führt zum Abbruch des Druckjobs.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#0099CC">
<tr>
<td><font color="#FFFFFF"> Tipp </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel4id026003002005"></a>
<h4>Das Interface Pageable und die Klasse Book </h4>
<p>
Das Paket <a href="index_j.html#ixb101303"><font color=#000080><tt>java.awt.print</tt></font></a>
stellt auch eine Abstraktion für <i>mehrseitige</i> Dokumente
zur Verfügung. Das Interface <a name="ixa101727"><a href="index_p.html#ixb101306"><font color=#000080><tt>Pageable</tt></font></a></a>
definiert deren Eigenschaften:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public int getNumberOfPages()
public PageFormat getPageFormat(int pageIndex)
throws IndexOutOfBoundsException
public Printable getPrintable(int pageIndex)
throws IndexOutOfBoundsException
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/Pageable.html" onClick="this.href=getApiDoc('java.awt.print.Pageable')"><font color="#660066" size=-1>java.awt.print.Pageable</font></a></td>
</tr>
</table>
<p>
Ein <a href="index_p.html#ixb101306"><font color=#000080><tt>Pageable</tt></font></a>-Objekt
kennt (normalerweise) die Anzahl der auszudruckenden Seiten und stellt
diese auf Anfrage als Rückgabewert von <a name="ixa101728"><a href="index_g.html#ixb101335"><font color=#000080><tt>getNumberOfPages</tt></font></a></a>
zur Verfügung. Wahlweise kann aber auch die Konstante <a name="ixa101729"><a href="index_u.html#ixb101336"><font color=#000080><tt>UNKNOWN_NUMBER_OF_PAGES</tt></font></a></a>
zurückgegeben werden, wenn die Seitenzahl nicht von vorneherein
bekannt ist. Im Unterschied zu einem <a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>-Objekt
(das ja auch mehrere Seiten ausdrucken kann), kann ein <a href="index_p.html#ixb101306"><font color=#000080><tt>Pageable</tt></font></a>
zu den einzelnen Seiten unterschiedliche <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>-
und <a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>-Objekte
zur Verfügung stellen. Sie werden während des Druckvorgangs
mit den Methoden <a name="ixa101730"><a href="index_g.html#ixb101337"><font color=#000080><tt>getPageFormat</tt></font></a></a>
und <a name="ixa101731"><a href="index_g.html#ixb101338"><font color=#000080><tt>getPrintable</tt></font></a></a>
abgefragt.
<p>
Mit der Klasse <a name="ixa101732"><a href="index_b.html#ixb101339"><font color=#000080><tt>Book</tt></font></a></a>
stellt <a href="index_j.html#ixb101303"><font color=#000080><tt>java.awt.print</tt></font></a>
eine konkrete Implementierung von <a href="index_p.html#ixb101306"><font color=#000080><tt>Pageable</tt></font></a>
zur Verfügung. Ein <a href="index_b.html#ixb101339"><font color=#000080><tt>Book</tt></font></a>
ist dabei nichts anderes als eine nach Seitenzahlen indizierte Liste
von (<a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>/<a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>)-Paaren,
die mit einigen einfachen Methoden gepflegt werden kann:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void append(Printable painter, PageFormat pf)
public void append(Printable painter, PageFormat pf, int numPages)
public void setPage(int page, Printable painter, PageFormat pf)
throws IndexOutOfBoundsException
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/print/Book.html" onClick="this.href=getApiDoc('java.awt.print.Book')"><font color="#660066" size=-1>java.awt.print.Book</font></a></td>
</tr>
</table>
<p>
Mit <a name="ixa101733"><a href="index_a.html#ixb100546"><font color=#000080><tt>append</tt></font></a></a>
werden eine oder mehrere Seiten mit den angegebenen <a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>-
und <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>-Objekten
an das Ende des Buchs angehängt. Mit <a name="ixa101734"><a href="index_s.html#ixb101340"><font color=#000080><tt>setPage</tt></font></a></a>
kann gezielt eine bereits bestehende Seite verändert werden.
<!-- Section -->
<a name="sectlevel3id026003003"></a>
<h3>26.3.3 Ausdrucken einer Textdatei </h3>
<p>
Nach diesen Vorbemerkungen wollen wir uns in diesem Abschnitt ein
Anwendungsbeispiel für das Druck-API des JDK 1.2 ansehen. Dazu
soll ein Programm geschrieben werden, das beliebige Textdateien auf
einem Drucker ausgeben kann. Der Anwender soll sowohl Job- als auch
Seitenparameter im Dialog verändern können. Jede Seite soll
mit einer Kopfzeile versehen werden, die den Namen der Datei und die
Seitenzahl enthält. Die Kopfzeile soll fett gedruckt und unterstrichen
werden. Der Textteil soll dunkelblau gedruckt werden, sofern Farben
unterstützt werden.
<!-- Section -->
<a name="sectlevel4id026003003001"></a>
<h4>Die Hilfsklasse FilePrintHelper </h4>
<p>
Wir hatten oben erwähnt, dass die Methode <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
des <a href="index_p.html#ixb101305"><font color=#000080><tt>Printable</tt></font></a>-Objekts
mehrfach für eine einzelne Seite aufgerufen werden kann. Soll
eine Datei ausgedruckt werden, muss das Programm bei wiederholten
Besuchen derselben Seite in der Lage sein, an der Textstelle aufzusetzen,
die den Anfang der Seite markiert. Wir führen dazu eine Hilfsklasse
<font color="#000077"><tt>FilePrintHelper</tt></font> ein, die sich
zu jeder besuchten Seite deren Dateioffset merkt. Diesen stellt sie
auf Anfrage wieder zur Verfügung. Sie kann auch Auskunft darüber
geben, ob eine Seite überhaupt schon besucht wurde.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Auf den ersten Blick scheint es übertrieben, für das Speichern
eines einzigen Attributs eine eigene Klasse zu bemühen. Bei komplizierten
Druckaufgaben kann diese Konstruktion aber sehr hilfreich werden.
Denn neben dem Fileoffset werden dann oft noch weitere Informationen
je Seite benötigt, etwa eine Titelzeile, numerische Überträge,
Fußnoteninformationen oder ähnliches. In diesem Fall ist
unser Design noch tragfähig und die Hilfsklasse kann einfach
um die benötigten Attribute und Methoden erweitert werden.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Die Implementierung der Klasse <font color="#000077"><tt>FilePrintHelper</tt></font>
sieht so aus:
<a name="listingid026002"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* FilePrintHelper.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.util.*;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> FilePrintHelper
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#00AA00">//---Membervariablen----------------------------------</font>
<font color="#555555">008 </font> Vector pageinfo;
<font color="#555555">009 </font>
<font color="#555555">010 </font> <font color="#00AA00">//---Konstruktor--------------------------------------</font>
<font color="#555555">011 </font> <font color="#0000AA">public</font> FilePrintHelper()
<font color="#555555">012 </font> {
<font color="#555555">013 </font> pageinfo = <font color="#0000AA">new</font> Vector();
<font color="#555555">014 </font> }
<font color="#555555">015 </font>
<font color="#555555">016 </font> <font color="#00AA00">//---Seitendefinition und -abfrage--------------------</font>
<font color="#555555">017 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> createPage(<font color="#006699">int</font> page)
<font color="#555555">018 </font> {
<font color="#555555">019 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = pageinfo.size(); i <= page; ++i) {
<font color="#555555">020 </font> pageinfo.addElement(<font color="#0000AA">new</font> Entry());
<font color="#555555">021 </font> }
<font color="#555555">022 </font> }
<font color="#555555">023 </font>
<font color="#555555">024 </font> <font color="#0000AA">public</font> <font color="#006699">boolean</font> knownPage(<font color="#006699">int</font> page)
<font color="#555555">025 </font> {
<font color="#555555">026 </font> <font color="#0000AA">return</font> page < pageinfo.size();
<font color="#555555">027 </font> }
<font color="#555555">028 </font>
<font color="#555555">029 </font> <font color="#00AA00">//---Verwaltung der Offsets---------------------------</font>
<font color="#555555">030 </font> <font color="#0000AA">public</font> <font color="#006699">long</font> getFileOffset(<font color="#006699">int</font> page)
<font color="#555555">031 </font> {
<font color="#555555">032 </font> Entry entry = (Entry)pageinfo.elementAt(page);
<font color="#555555">033 </font> <font color="#0000AA">return</font> entry.fileoffset;
<font color="#555555">034 </font> }
<font color="#555555">035 </font>
<font color="#555555">036 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> setFileOffset(<font color="#006699">int</font> page, <font color="#006699">long</font> fileoffset)
<font color="#555555">037 </font> {
<font color="#555555">038 </font> Entry entry = (Entry)pageinfo.elementAt(page);
<font color="#555555">039 </font> entry.fileoffset = fileoffset;
<font color="#555555">040 </font> }
<font color="#555555">041 </font>
<font color="#555555">042 </font> <font color="#00AA00">//---Lokale Klasse Entry------------------------------</font>
<font color="#555555">043 </font> <font color="#0000AA">static</font> <font color="#0000AA">class</font> Entry
<font color="#555555">044 </font> {
<font color="#555555">045 </font> <font color="#0000AA">public</font> <font color="#006699">long</font> fileoffset;
<font color="#555555">046 </font>
<font color="#555555">047 </font> <font color="#0000AA">public</font> Entry()
<font color="#555555">048 </font> {
<font color="#555555">049 </font> <font color="#006699">this</font>.fileoffset = -1;
<font color="#555555">050 </font> }
<font color="#555555">051 </font> }
<font color="#555555">052 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/FilePrintHelper.java"><font color="#000055" size=-1>FilePrintHelper.java</font></a></td>
</tr>
</table>
<i>
Listing 26.2: Die Klasse FilePrintHelper</i></p>
<!-- Section -->
<a name="sectlevel4id026003003002"></a>
<h4>Die Klasse SimpleFilePrinter </h4>
<p>
Das Ausdrucken der Datei wird von der Klasse <font color="#000077"><tt>SimpleFilePrinter</tt></font>
erledigt. Ihre <a href="index_m.html#ixb100150"><font color=#000080><tt>main</tt></font></a>-Methode
gliedert die zu erledigende Arbeit in folgende Teilaufgaben:
<ul>
<li>Zunächst wird ein <font color="#000077"><tt>SimpleFilePrinter</tt></font>
instanziert. Es erzeugt durch Aufruf von <a href="index_g.html#ixb101307"><font color=#000080><tt>getPrinterJob</tt></font></a>
eine Instanz der Klasse <a href="index_p.html#ixb101304"><font color=#000080><tt>PrinterJob</tt></font></a>.
<li>In <font color="#000077"><tt>setupPageFormat</tt></font> wird
durch Aufruf von <a href="index_d.html#ixb101327"><font color=#000080><tt>defaultPage</tt></font></a>
ein <a href="index_p.html#ixb101315"><font color=#000080><tt>PageFormat</tt></font></a>
beschafft und zur Bearbeitung an den Seitendialog übergeben.
Die Methode prüft anschließend, ob der Dialog mit »OK«
oder »Abbrechen« verlassen wurde, und gibt entweder <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>
oder <a href="index_f.html#ixb100234"><font color=#000080><tt>false</tt></font></a>
zurück (letzteres führt in <a href="index_m.html#ixb100150"><font color=#000080><tt>main</tt></font></a>
zum Abbruch des Druckvorgangs).
<li>In <font color="#000077"><tt>setupJobOptions</tt></font> wird
der Dialog zur Konfiguration der Job-Parameter aufgerufen. Auch hier
führt ein Abbruch des Anwenders zum Ende des gesamten Druckvorgangs.
<li>Schließlich wird in <font color="#000077"><tt>printFile</tt></font>
der eigentliche Druckvorgang angestoßen. Dazu wird zunächst
ein <a href="index_r.html#ixb101016"><font color=#000080><tt>RandomAccessFile</tt></font></a>
für die auszudruckende Datei angelegt und ein <font color="#000077"><tt>FilePrintHelper</tt></font>
instanziert. Anschließend wird die Druckausgabe durch Aufruf
von <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
gestartet.
</ul>
<p>
Nachfolgend zeigen wir die Implementierung von <font color="#000077"><tt>SimpleFilePrinter</tt></font>.
Der eigentlich komplizierte Code steckt natürlich in der Methode
<a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>,
die wir im Anschluß an das Listing erläutern wollen.
<a name="simplefileprinter"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* SimpleFilePrinter.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.awt.print.*;
<font color="#555555">005 </font><font color="#0000AA">import</font> java.io.*;
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> SimpleFilePrinter
<font color="#555555">008 </font><font color="#0000AA">implements</font> Printable
<font color="#555555">009 </font>{
<font color="#555555">010 </font> <font color="#00AA00">//---Konstanten--------------------------------------</font>
<font color="#555555">011 </font> <font color="#0000AA">private</font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">int</font> RESMUL = 4;
<font color="#555555">012 </font>
<font color="#555555">013 </font> <font color="#00AA00">//---Membervariablen---------------------------------</font>
<font color="#555555">014 </font> <font color="#0000AA">private</font> PrinterJob pjob;
<font color="#555555">015 </font> <font color="#0000AA">private</font> PageFormat pageformat;
<font color="#555555">016 </font> <font color="#0000AA">private</font> FilePrintHelper fph;
<font color="#555555">017 </font> <font color="#0000AA">private</font> String fname;
<font color="#555555">018 </font> <font color="#0000AA">private</font> RandomAccessFile in;
<font color="#555555">019 </font>
<font color="#555555">020 </font> <font color="#00AA00">//---Konstruktoren-----------------------------------</font>
<font color="#555555">021 </font> <font color="#0000AA">public</font> SimpleFilePrinter(String fname)
<font color="#555555">022 </font> {
<font color="#555555">023 </font> <font color="#006699">this</font>.pjob = PrinterJob.getPrinterJob();
<font color="#555555">024 </font> <font color="#006699">this</font>.fname = fname;
<font color="#555555">025 </font> }
<font color="#555555">026 </font>
<font color="#555555">027 </font> <font color="#00AA00">//---Öffentliche Methoden----------------------------</font>
<font color="#555555">028 </font> <font color="#0000AA">public</font> <font color="#006699">boolean</font> setupPageFormat()
<font color="#555555">029 </font> {
<font color="#555555">030 </font> PageFormat defaultPF = pjob.defaultPage();
<font color="#555555">031 </font> <font color="#006699">this</font>.pageformat = pjob.pageDialog(defaultPF);
<font color="#555555">032 </font> pjob.setPrintable(<font color="#006699">this</font>, <font color="#006699">this</font>.pageformat);
<font color="#555555">033 </font> <font color="#0000AA">return</font> (<font color="#006699">this</font>.pageformat != defaultPF);
<font color="#555555">034 </font> }
<font color="#555555">035 </font>
<font color="#555555">036 </font> <font color="#0000AA">public</font> <font color="#006699">boolean</font> setupJobOptions()
<font color="#555555">037 </font> {
<font color="#555555">038 </font> <font color="#0000AA">return</font> pjob.printDialog();
<font color="#555555">039 </font> }
<font color="#555555">040 </font>
<font color="#555555">041 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> printFile()
<font color="#555555">042 </font> <font color="#0000AA">throws</font> PrinterException, IOException
<font color="#555555">043 </font> {
<font color="#555555">044 </font> fph = <font color="#0000AA">new</font> FilePrintHelper();
<font color="#555555">045 </font> in = <font color="#0000AA">new</font> RandomAccessFile(fname, <font color="#0000FF">"r"</font>);
<font color="#555555">046 </font> pjob.print();
<font color="#555555">047 </font> in.close();
<font color="#555555">048 </font> }
<font color="#555555">049 </font>
<font color="#555555">050 </font> <font color="#00AA00">//---Implementierung von Printable-------------------</font>
<font color="#555555">051 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> print(Graphics g, PageFormat pf, <font color="#006699">int</font> page)
<font color="#555555">052 </font> <font color="#0000AA">throws</font> PrinterException
<font color="#555555">053 </font> {
<font color="#555555">054 </font> <font color="#006699">int</font> ret = PAGE_EXISTS;
<font color="#555555">055 </font> String line = <font color="#006699">null</font>;
<font color="#555555">056 </font> <font color="#0000AA">try</font> {
<font color="#555555">057 </font> <font color="#0000AA">if</font> (fph.knownPage(page)) {
<font color="#555555">058 </font> in.seek(fph.getFileOffset(page));
<font color="#555555">059 </font> line = in.readLine();
<font color="#555555">060 </font> } <font color="#0000AA">else</font> {
<font color="#555555">061 </font> <font color="#006699">long</font> offset = in.getFilePointer();
<font color="#555555">062 </font> line = in.readLine();
<font color="#555555">063 </font> <font color="#0000AA">if</font> (line == <font color="#006699">null</font>) {
<font color="#555555">064 </font> ret = NO_SUCH_PAGE;
<font color="#555555">065 </font> } <font color="#0000AA">else</font> {
<font color="#555555">066 </font> fph.createPage(page);
<font color="#555555">067 </font> fph.setFileOffset(page, offset);
<font color="#555555">068 </font> }
<font color="#555555">069 </font> }
<font color="#555555">070 </font> <font color="#0000AA">if</font> (ret == PAGE_EXISTS) {
<font color="#555555">071 </font> <font color="#00AA00">//Seite ausgeben, Grafikkontext vorbereiten</font>
<font color="#555555">072 </font> Graphics2D g2 = (Graphics2D)g; <a name="simplefileprinter.a"></a>
<font color="#555555">073 </font> g2.scale(1.0 / RESMUL, 1.0 / RESMUL);
<font color="#555555">074 </font> <font color="#006699">int</font> ypos = (<font color="#006699">int</font>)pf.getImageableY() * RESMUL;
<font color="#555555">075 </font> <font color="#006699">int</font> xpos = ((<font color="#006699">int</font>)pf.getImageableX() + 2) * RESMUL;
<font color="#555555">076 </font> <font color="#006699">int</font> yd = 12 * RESMUL;
<font color="#555555">077 </font> <font color="#006699">int</font> ymax = ypos + (<font color="#006699">int</font>)pf.getImageableHeight() * RESMUL - yd;
<font color="#555555">078 </font> <font color="#00AA00">//Seitentitel ausgeben</font>
<font color="#555555">079 </font> ypos += yd; <a name="simplefileprinter.b"></a>
<font color="#555555">080 </font> g2.setColor(Color.black);
<font color="#555555">081 </font> g2.setFont(<font color="#0000AA">new</font> Font(<font color="#0000FF">"Monospaced"</font>, Font.BOLD, 10 * RESMUL));
<font color="#555555">082 </font> g.drawString(fname + <font color="#0000FF">", Seite "</font> + (page + 1), xpos, ypos);
<font color="#555555">083 </font> g.drawLine(
<font color="#555555">084 </font> xpos,
<font color="#555555">085 </font> ypos + 6 * RESMUL,
<font color="#555555">086 </font> xpos + (<font color="#006699">int</font>)pf.getImageableWidth() * RESMUL,
<font color="#555555">087 </font> ypos + 6 * RESMUL
<font color="#555555">088 </font> );
<font color="#555555">089 </font> ypos += 2 * yd;
<font color="#555555">090 </font> <font color="#00AA00">//Zeilen ausgeben</font>
<font color="#555555">091 </font> g2.setColor(<font color="#0000AA">new</font> Color(0, 0, 127)); <a name="simplefileprinter.c"></a>
<font color="#555555">092 </font> g2.setFont(<font color="#0000AA">new</font> Font(<font color="#0000FF">"Monospaced"</font>, Font.PLAIN, 10 * RESMUL));
<font color="#555555">093 </font> <font color="#0000AA">while</font> (line != <font color="#006699">null</font>) {
<font color="#555555">094 </font> g.drawString(line, xpos, ypos);
<font color="#555555">095 </font> ypos += yd;
<font color="#555555">096 </font> <font color="#0000AA">if</font> (ypos >= ymax) {
<font color="#555555">097 </font> <font color="#0000AA">break</font>;
<font color="#555555">098 </font> }
<font color="#555555">099 </font> line = in.readLine();
<font color="#555555">100 </font> }
<font color="#555555">101 </font> }
<font color="#555555">102 </font> } <font color="#0000AA">catch</font> (IOException e) {
<font color="#555555">103 </font> <font color="#0000AA">throw</font> <font color="#0000AA">new</font> PrinterException(e.toString());
<font color="#555555">104 </font> }
<font color="#555555">105 </font> <font color="#0000AA">return</font> ret;
<font color="#555555">106 </font> }
<font color="#555555">107 </font>
<font color="#555555">108 </font> <font color="#00AA00">//---Main--------------------------------------------</font>
<font color="#555555">109 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">110 </font> {
<font color="#555555">111 </font> SimpleFilePrinter sfp = <font color="#0000AA">new</font> SimpleFilePrinter(args[0]);
<font color="#555555">112 </font> <font color="#0000AA">if</font> (sfp.setupPageFormat()) {
<font color="#555555">113 </font> <font color="#0000AA">if</font> (sfp.setupJobOptions()) {
<font color="#555555">114 </font> <font color="#0000AA">try</font> {
<font color="#555555">115 </font> sfp.printFile();
<font color="#555555">116 </font> } <font color="#0000AA">catch</font> (Exception e) {
<font color="#555555">117 </font> System.err.println(e.toString());
<font color="#555555">118 </font> System.exit(1);
<font color="#555555">119 </font> }
<font color="#555555">120 </font> }
<font color="#555555">121 </font> }
<font color="#555555">122 </font> System.exit(0);
<font color="#555555">123 </font> }
<font color="#555555">124 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/SimpleFilePrinter.java"><font color="#000055" size=-1>SimpleFilePrinter.java</font></a></td>
</tr>
</table>
<i>
Listing 26.3: Die Klasse SimpleFilePrinter</i></p>
<p>
In <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
wird zunächst geprüft, ob die Seite schon einmal aufgerufen
wurde. Ist das der Fall, wird ihr Dateioffset vom <font color="#000077"><tt>FilePrintHelper</tt></font>
geholt und die erste Zeile der Seite erneut eingelesen. Falls nicht,
prüft die Methode, ob überhaupt noch weitere Daten vorhanden
sind. In diesem Fall übergibt sie den Dateioffset zur Speicherung
an den <font color="#000077"><tt>FilePrintHelper</tt></font>. Andernfalls
setzt es den Rückgabewert auf <a href="index_n.html#ixb101333"><font color=#000080><tt>NO_SUCH_PAGE</tt></font></a>,
und die Ausgabe der Seite ist beendet.
<p>
Falls die Seite existiert, wird sie nun ausgegeben (im Listing ab
<a href="k100170.html#simplefileprinter.a">Zeile 072</a>). Dazu wird
der Grafikkontext nach <a href="index_g.html#ixb101331"><font color=#000080><tt>Graphics2D</tt></font></a>
konvertiert und anschließend auf den Kehrwert der Konstante
<font color="#000077"><tt>RESMUL</tt></font> skaliert. Da <font color="#000077"><tt>RESMUL</tt></font>
den Wert 4 hat, führt das dazu, dass alle Ausgaben um den Faktor
4 verkleinert werden. Dieser Kunstgriff wird angewendet, um bei der
Ausgabe nicht mit der groben Standardauflösung von 72 dpi arbeiten
zu müssen. Die auf diese Weise simulierten 288 dpi lassen die
Linie unter der Kopfzeile hinreichend dünn erscheinen. Sie wäre
ansonsten mit einem viel zu kräftigen 1/72 Zoll breiten Pixelstrich
gezeichnet worden. Da nun auch alle anderen Positions- und Längenangaben
um den Faktor 4 verkleinert werden, multiplizieren wir sie in den
folgenden Programmzeilen jeweils mit <font color="#000077"><tt>RESMUL</tt></font>
und skalieren sie so auf ihre normale Größe zurück.
<p>
Ab <a href="k100170.html#simplefileprinter.b">Zeile 079</a> wird der
Seitentitel ausgeben. Dazu wird die Schriftfarbe auf schwarz eingestellt
und eine fette Schrift ausgewählt. Die Variable <font color="#000077"><tt>ypos</tt></font>
repräsentiert den vertikalen Offset vom oberen Papierrand. Sie
wird am Anfang auf das obere Ende des bedruckbaren Bereichs gestellt
und nach jeder Zeile um einen konstanten Wert erhöht. Ab <a href="k100170.html#simplefileprinter.c">Zeile 091</a>
werden die Textzeilen ausgegeben. Mit <a href="index_r.html#ixb100999"><font color=#000080><tt>readLine</tt></font></a>
wird dazu in einer Schleife die jeweils nächste Zeile aus der
Eingabedatei gelesen und - wenn vorhanden - an der aktuellen y-Position
ausgegeben. Falls keine weiteren Zeilen vorhanden sind oder das Ende
des bedruckbaren Bereichs erreicht ist, wird die Schleife beendet.
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100167.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100169.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100171.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100173.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|