summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/html/k100169.html
blob: 35fc7d8cae76b249717f656997be06e10c0564ba (plain)
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
<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,k100168.html;107,k100170.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">&nbsp;Titel&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html">&nbsp;Inhalt&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html">&nbsp;Suchen&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html">&nbsp;Index&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()">&nbsp;DOC&nbsp;</a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100167.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100168.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100170.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100173.html">&nbsp;&gt;&gt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()">&nbsp;API&nbsp;</a>
<td align="right">Kapitel 26 - Drucken
</table>
<hr>


<!-- Section -->
<a name="sectlevel2id026002"></a>
<h2>26.2 Drucken mit dem JDK 1.1 </h2>
<hr>
<ul>
<li><a href="k100169.html#sectlevel2id026002">26.2 Drucken mit dem JDK 1.1</a>
<ul>
<li><a href="k100169.html#sectlevel3id026002001">26.2.1 Grundlagen</a>
<li><a href="k100169.html#sectlevel3id026002002">26.2.2 Seitenweise Ausgabe</a>
<li><a href="k100169.html#sectlevel3id026002003">26.2.3 platzierung des Codes zur Druckausgabe</a>
</ul>
</ul>
<hr>


<!-- Section -->
<a name="sectlevel3id026002001"></a>
<h3>26.2.1 Grundlagen </h3>

<p>
Das Druck-API der Version 1.1 ist recht &uuml;bersichtlich und leicht
zu verstehen. Es gibt zwar einige Restriktionen und Besonderheiten,
die beim Erstellen von Druckausgaben zu Problemen f&uuml;hren k&ouml;nnen,
aber f&uuml;r einfache Ausdrucke von Text und Grafik ist die Schnittstelle
dennoch geeignet. 

<p>
Grundlage der Druckausgabe ist die Methode <a name="ixa101680"><a href="index_g.html#ixb101292"><font color=#000080><tt>getPrintJob</tt></font></a></a>
der Klasse <a href="index_t.html#ixb100506"><font color=#000080><tt>Toolkit</tt></font></a>:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public PrintJob getPrintJob(
   Frame frame,
   String jobtitle,
   Properties props
)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Toolkit.html" onClick="this.href=getApiDoc('java.awt.Toolkit')"><font color="#660066" size=-1>java.awt.Toolkit</font></a></td>
</tr>
</table>

<p>
Sie liefert ein Objekt des Typs <a name="ixa101681"><a href="index_p.html#ixb101293"><font color=#000080><tt>PrintJob</tt></font></a></a>,
das zur Initialisierung eines Druckjobs verwendet werden kann. Ein
Aufruf von <a href="index_g.html#ixb101292"><font color=#000080><tt>getPrintJob</tt></font></a>
f&uuml;hrt gleichzeitig dazu, dass ein plattformspezifischer Druckdialog
aufgerufen wird, der vom Anwender best&auml;tigt werden muss. Bricht
der Anwender den Druckdialog ab, liefert <a href="index_g.html#ixb101292"><font color=#000080><tt>getPrintJob</tt></font></a>
den R&uuml;ckgabewert <a href="index_n.html#ixb100235"><font color=#000080><tt>null</tt></font></a>.
Andernfalls wird der Drucker initialisiert und die Ausgabe kann beginnen.

<p>
Die Klasse <a href="index_p.html#ixb101293"><font color=#000080><tt>PrintJob</tt></font></a>
stellt einige Methoden zur Verf&uuml;gung, die f&uuml;r den Ausdruck
ben&ouml;tigt werden: 
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public Graphics getGraphics()

public Dimension getPageDimension()

public int getPageResolution()

public abstract void end()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/PrintJob.html" onClick="this.href=getApiDoc('java.awt.PrintJob')"><font color="#660066" size=-1>java.awt.PrintJob</font></a></td>
</tr>
</table>

<p>
Die wichtigste von ihnen ist <a name="ixa101682"><a href="index_g.html#ixb101294"><font color=#000080><tt>getGraphics</tt></font></a></a>.
Sie liefert den Devicekontext zur Ausgabe auf den Drucker. Der R&uuml;ckgabewert
ist ein Objekt vom Typ <a name="ixa101683"><a href="index_p.html#ixb101295"><font color=#000080><tt>PrintGraphics</tt></font></a></a>,
das aus <a href="index_g.html#ixb101149"><font color=#000080><tt>Graphics</tt></font></a>
abgeleitet ist und wie ein normaler Devicekontext verwendet werden
kann (so, wie er beispielsweise auch an <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>
&uuml;bergeben wird). Mit Hilfe des von <a href="index_g.html#ixb101294"><font color=#000080><tt>getGraphics</tt></font></a>
zur&uuml;ckgegebenen Devicekontexts k&ouml;nnen alle Grafik- und Textroutinen,
die auch in <a href="index_g.html#ixb101149"><font color=#000080><tt>Graphics</tt></font></a>
zur Verf&uuml;gung stehen, verwendet werden. Bez&uuml;glich der Verwendung
von Farben gilt scheinbar, dass diese bei den linienbezogenen Ausgaberoutinen
nicht unterst&uuml;tzt werden. Hier wird alles schwarz gezeichnet,
was nicht den Farbwert <i>(255, 255, 255)</i> hat. Im Gegensatz dazu
stellen die F&uuml;llfunktionen Farben (auf einem Schwarzwei&szlig;-Drucker)
als Grauwerte dar. Dabei kann - je nach Druckertyp - auch Wei&szlig;
eine Vollfarbe sein und dahinter liegende Objekte verdecken. 

<!-- Section -->

<a name="sectlevel3id026002002"></a>
<h3>26.2.2 Seitenweise Ausgabe </h3>

<p>
Ein wichtiger Unterschied zu einem bildschirmbezogenen Devicekontext
besteht darin, dass jeder Aufruf von <a href="index_g.html#ixb101294"><font color=#000080><tt>getGraphics</tt></font></a>
eine neue Druckseite beginnt. Die fertige Druckseite wird durch Aufruf
von <a name="ixa101684"><a href="index_d.html#ixb101296"><font color=#000080><tt>dispose</tt></font></a></a>
an den Drucker geschickt. F&uuml;r den Aufbau der Seite, das Ausgeben
von Kopf- oder Fu&szlig;zeilen, die Seitennumerierung und &auml;hnliche
Dinge ist die Anwendung selbst verantwortlich. Die Methode <a name="ixa101685"><a href="index_e.html#ixb101297"><font color=#000080><tt>end</tt></font></a></a>
ist aufzurufen, wenn der Druckjob beendet ist und alle Seiten ausgegeben
wurden. Dadurch werden alle belegten Ressourcen freigegeben und die
Druckerschnittstelle geschlossen. 
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>

<p>
Bei der Druckausgabe ist es wichtig zu wissen, wie gro&szlig; die
Abmessungen des Ausgabeger&auml;ts sind. Die hierzu angebotenen Methoden
<a name="ixa101686"><a href="index_g.html#ixb101298"><font color=#000080><tt>getPageDimension</tt></font></a></a>
und <a name="ixa101687"><a href="index_g.html#ixb101299"><font color=#000080><tt>getPageResolution</tt></font></a></a>
sind im JDK 1.1 leider vollkommen unbrauchbar. <a href="index_g.html#ixb101299"><font color=#000080><tt>getPageResolution</tt></font></a>
liefert die tats&auml;chliche Aufl&ouml;sung des Druckers in Pixel
per Zoll (also z.B. 600 f&uuml;r einen Laserjet IV), w&auml;hrend
<a href="index_g.html#ixb101298"><font color=#000080><tt>getPageDimension</tt></font></a>
die Anzahl der Pixel liefert, die sich errechnet, wenn man ein Blatt
Papier im US-Letter-Format (8,5 mal 11 Zoll) mit 72 dpi Aufl&ouml;sung
darstellen w&uuml;rde. Leider erfolgt die Druckausgabe nicht mit 72
dpi, sondern in der aktuellen Bildschirmaufl&ouml;sung, wie sie von
der Methode <a name="ixa101688"><a href="index_g.html#ixb101300"><font color=#000080><tt>getScreenResolution</tt></font></a></a>
der Klasse <a href="index_t.html#ixb100506"><font color=#000080><tt>Toolkit</tt></font></a>
geliefert wird. Da diese typischerweise bei 120 dpi liegt, f&uuml;llen
die von <a href="index_g.html#ixb101299"><font color=#000080><tt>getPageResolution</tt></font></a>
gelieferten Abmessungen nur etwa 60 % einer Seite.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#CC0000">
<tr>
<td><font color="#FFFFFF">&nbsp;Warnung&nbsp;</font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
</tr>
</table>

<p>
Derzeit gibt es keine portable L&ouml;sung f&uuml;r dieses Problem.
Ein Workaround besteht darin, die Papiergr&ouml;&szlig;e als fest
anzunehmen (beispielsweise DIN A4 mit 21,0*29,7 cm), davon den nicht
bedruckbaren Rand abzuziehen, das Ergebnis durch 2,54 (Anzahl cm je
Zoll) zu teilen und mit der Aufl&ouml;sung von 120 dpi malzunehmen.
Wir werden sp&auml;ter ein Beispiel sehen, in dem diese Technik angewandt
wird. Portabel ist sie allerdings nicht, denn das Programm muss Annahmen
&uuml;ber die Papier- und Randgr&ouml;&szlig;e machen. Es bleibt demnach
zu hoffen, dass die nachfolgenden Versionen des JDK die Bestimmung
der Abmessungen auf eine flexiblere Weise erm&ouml;glichen. 
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>

<p>
Durch die fixe Einstellung der Ausgabeaufl&ouml;sung ergibt sich ein
weiteres Problem. So kann ein Drucker mit 600 dpi aus Java heraus
n&auml;mlich nur mit der aktuellen Bildschirmaufl&ouml;sung (z.B.
120 dpi) angesteuert werden. Das bedeutet zwar nicht automatisch,
dass Schriften oder schr&auml;ge Linien mit Treppenmustern dargestellt
werden, denn sie werden meist als Vektorgrafiken an den Drucker &uuml;bergeben.
Allerdings k&ouml;nnen Pixelgrafiken beispielsweise nicht in der aktuellen
Druckeraufl&ouml;sung ausgegeben werden, denn die Positioniergenauigkeit
eines einzelnen Pixels liegt bei 120 dpi. Eine L&ouml;sung f&uuml;r
dieses Problem ist derzeit nicht bekannt.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#CC0000">
<tr>
<td><font color="#FFFFFF">&nbsp;Warnung&nbsp;</font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
</tr>
</table>


<!-- Section -->
<a name="sectlevel3id026002003"></a>
<h3>26.2.3 platzierung des Codes zur Druckausgabe </h3>

<p>
Es gibt grunds&auml;tzlich zwei M&ouml;glichkeiten, die Druckausgabe
im Programm zu platzieren. Einmal kann die <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>-Methode
dazu verwendet werden, sowohl Bildschirm- als auch Druckausgaben zu
realisieren. Bei einem Aufruf der Methode <a name="ixa101689"><a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a></a>
oder <a name="ixa101690"><a href="index_p.html#ixb101301"><font color=#000080><tt>printAll</tt></font></a></a>
der Klasse <a href="index_c.html#ixb101302"><font color=#000080><tt>Component</tt></font></a>
wird n&auml;mlich ein <a href="index_p.html#ixb101293"><font color=#000080><tt>PrintJob</tt></font></a>
erstellt, daraus der Grafikkontext beschafft und an <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>
&uuml;bergeben: 
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void print(Graphics g)

public void printAll(Graphics g)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Component.html" onClick="this.href=getApiDoc('java.awt.Component')"><font color="#660066" size=-1>java.awt.Component</font></a></td>
</tr>
</table>

<p>
Auf diese Weise kann bereits ohne zus&auml;tzliche Erweiterungen eine
einfache Druckausgabe realisiert werden, die der Bildschirmausgabe
relativ &auml;hnlich sieht. Im Gegensatz zu <a href="index_p.html#ixb100922"><font color=#000080><tt>print</tt></font></a>
gibt <a href="index_p.html#ixb101301"><font color=#000080><tt>printAll</tt></font></a>
dabei nicht nur die aktuelle Komponente, sondern die komplette Container-Hierarchie
eines komplexen Dialogs aus. Soll innerhalb von <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>
zwischen Bildschirm- und Druckerausgabe unterschieden werden, kann
mit dem Ausdruck <font color="#000077"><tt>g instanceof PrintGraphics</tt></font>
das &uuml;bergebene <a href="index_g.html#ixb101149"><font color=#000080><tt>Graphics</tt></font></a>-Objekt
<font color="#000077"><tt>g</tt></font> auf Zugeh&ouml;rigkeit zur
Klasse <a href="index_p.html#ixb101295"><font color=#000080><tt>PrintGraphics</tt></font></a>
getestet werden. 

<p>
Die zweite M&ouml;glichkeit, die Druckausgabe zu platzieren, besteht
darin, eine eigene Methode zu schreiben, die nur f&uuml;r die Ausgabe
auf den Drucker verantwortlich ist. Diese k&ouml;nnte zun&auml;chst
den <a href="index_p.html#ixb101293"><font color=#000080><tt>PrintJob</tt></font></a>
und das <a href="index_p.html#ixb101295"><font color=#000080><tt>PrintGraphics</tt></font></a>-Objekt
beschaffen und anschlie&szlig;end die Abmessungen der Ausgabefl&auml;che
wie zuvor besprochen bestimmen. Die Methode m&uuml;sste dann nicht
so programmiert werden, dass sie f&uuml;r Bildschirm- und Druckausgabe
vern&uuml;nftige Resultate liefert, sondern k&ouml;nnte ihre Ausgaben
ausschlie&szlig;lich f&uuml;r die Druckausgabe optimieren. Der Nachteil
bei dieser L&ouml;ung ist nat&uuml;rlich, dass Programmcode zum Erstellen
der Ausgabe m&ouml;glicherweise doppelt vorhanden ist und doppelt
gepflegt werden muss. 

<p>
Das nachfolgende Listing kombiniert beide Varianten und zeigt den
Ausdruck einer Testseite. Das Programm erstellt ein Hauptfenster und
ruft zwei Sekunden sp&auml;ter die Methode <font color="#000077"><tt>printTestPage</tt></font>
zur Druckausgabe auf. Darin wird zun&auml;chst ein <a href="index_p.html#ixb101293"><font color=#000080><tt>PrintJob</tt></font></a>
erzeugt und dann gem&auml;&szlig; dem oben beschriebenen Verfahren
die Ausgabegr&ouml;&szlig;e ermittelt. Anschlie&szlig;end wird der
Grafikkontext beschafft und ein Rahmen, einige Textzeilen mit Angaben
zu den Metriken und eine Graustufenmatrix ausgegeben. Nach Ende der
Druckausgabe wird die Seite mit <a href="index_d.html#ixb101296"><font color=#000080><tt>dispose</tt></font></a>
ausgegeben und der Druckjob mit <a href="index_e.html#ixb101297"><font color=#000080><tt>end</tt></font></a>
geschlossen. Der Code f&uuml;r die Darstellung der Graustufenmatrix
wurde in der Methode <font color="#000077"><tt>paintGrayBoxes</tt></font>
implementiert und wird von der Bildschirm- und Druckausgabe gemeinsam
verwendet: 
<a name="listingid026001"></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">/* Listing2601.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.event.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing2601
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<font color="#555555">008 </font>{
<font color="#555555">009 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">010 </font>  {
<font color="#555555">011 </font>    Listing2601 wnd = <font color="#0000AA">new</font> Listing2601();
<font color="#555555">012 </font>  }
<font color="#555555">013 </font>
<font color="#555555">014 </font>  <font color="#0000AA">public</font> Listing2601()
<font color="#555555">015 </font>  {
<font color="#555555">016 </font>    <font color="#006699">super</font>(<font color="#0000FF">"Drucken"</font>);
<font color="#555555">017 </font>    addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">018 </font>    setBackground(Color.lightGray);
<font color="#555555">019 </font>    setSize(400,400);
<font color="#555555">020 </font>    setVisible(<font color="#006699">true</font>);
<font color="#555555">021 </font>    <font color="#00AA00">//Ausdruck in 2 Sekunden starten</font>
<font color="#555555">022 </font>    <font color="#0000AA">try</font> {
<font color="#555555">023 </font>      Thread.sleep(2000);
<font color="#555555">024 </font>    } <font color="#0000AA">catch</font> (InterruptedException e) {
<font color="#555555">025 </font>      <font color="#00AA00">//nichts</font>
<font color="#555555">026 </font>    }
<font color="#555555">027 </font>    printTestPage();
<font color="#555555">028 </font>  }
<font color="#555555">029 </font>
<font color="#555555">030 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">031 </font>  {
<font color="#555555">032 </font>    paintGrayBoxes(g, 40, 50);
<font color="#555555">033 </font>  }
<font color="#555555">034 </font>
<font color="#555555">035 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> printTestPage()
<font color="#555555">036 </font>  {
<font color="#555555">037 </font>    PrintJob pjob = getToolkit().getPrintJob(
<font color="#555555">038 </font>      <font color="#006699">this</font>,
<font color="#555555">039 </font>      <font color="#0000FF">"Testseite"</font>,
<font color="#555555">040 </font>      <font color="#006699">null</font>
<font color="#555555">041 </font>    );
<font color="#555555">042 </font>    <font color="#0000AA">if</font> (pjob != <font color="#006699">null</font>) {
<font color="#555555">043 </font>      <font color="#00AA00">//Metriken</font>
<font color="#555555">044 </font>      <font color="#006699">int</font> pres = pjob.getPageResolution();
<font color="#555555">045 </font>      <font color="#006699">int</font> sres = getToolkit().getScreenResolution();
<font color="#555555">046 </font>      Dimension d2 = <font color="#0000AA">new</font> Dimension(
<font color="#555555">047 </font>        (<font color="#006699">int</font>)(((21.0 - 2.0) / 2.54) * sres),
<font color="#555555">048 </font>        (<font color="#006699">int</font>)(((29.7 - 2.0) / 2.54) * sres)
<font color="#555555">049 </font>      );
<font color="#555555">050 </font>      <font color="#00AA00">//Ausdruck beginnt</font>
<font color="#555555">051 </font>      Graphics pg = pjob.getGraphics();
<font color="#555555">052 </font>      <font color="#0000AA">if</font> (pg != <font color="#006699">null</font>) {
<font color="#555555">053 </font>        <font color="#00AA00">//Rahmen</font>
<font color="#555555">054 </font>        pg.drawRect(0, 0, d2.width, d2.height);
<font color="#555555">055 </font>        <font color="#00AA00">//Text</font>
<font color="#555555">056 </font>        pg.setFont(<font color="#0000AA">new</font> Font(<font color="#0000FF">"TimesRoman"</font>,Font.PLAIN,24));
<font color="#555555">057 </font>        pg.drawString(<font color="#0000FF">"Testseite"</font>,40,70);
<font color="#555555">058 </font>        pg.drawString(
<font color="#555555">059 </font>          <font color="#0000FF">"Druckeraufl&ouml;sung : "</font> + pres + <font color="#0000FF">" dpi"</font>,
<font color="#555555">060 </font>          40,
<font color="#555555">061 </font>          100
<font color="#555555">062 </font>        );
<font color="#555555">063 </font>        pg.drawString(
<font color="#555555">064 </font>          <font color="#0000FF">"Bildschirmaufl&ouml;sung : "</font> + sres + <font color="#0000FF">" dpi"</font>,
<font color="#555555">065 </font>          40,
<font color="#555555">066 </font>          130
<font color="#555555">067 </font>        );
<font color="#555555">068 </font>        pg.drawString(
<font color="#555555">069 </font>          <font color="#0000FF">"Seitengr&ouml;&szlig;e : "</font> + d2.width + <font color="#0000FF">" * "</font> + d2.height,
<font color="#555555">070 </font>          40,
<font color="#555555">071 </font>          160
<font color="#555555">072 </font>        );
<font color="#555555">073 </font>        <font color="#00AA00">//Graustufenk&auml;stchen</font>
<font color="#555555">074 </font>        paintGrayBoxes(pg, 40, 200);
<font color="#555555">075 </font>        <font color="#00AA00">//Seite ausgeben</font>
<font color="#555555">076 </font>        pg.dispose();
<font color="#555555">077 </font>      }
<font color="#555555">078 </font>      pjob.end();
<font color="#555555">079 </font>    }
<font color="#555555">080 </font>  }
<font color="#555555">081 </font>
<font color="#555555">082 </font>  <font color="#0000AA">private</font> <font color="#006699">void</font> paintGrayBoxes(Graphics g, <font color="#006699">int</font> x, <font color="#006699">int</font> y)
<font color="#555555">083 </font>  {
<font color="#555555">084 </font>    <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i &lt; 16; ++i) {
<font color="#555555">085 </font>      <font color="#0000AA">for</font> (<font color="#006699">int</font> j = 0; j &lt; 16; ++j) {
<font color="#555555">086 </font>        <font color="#006699">int</font> level = 16 * i + j;
<font color="#555555">087 </font>        g.setColor(Color.black);
<font color="#555555">088 </font>        g.drawRect(x + 20 * j, y + 20 * i, 20, 20);
<font color="#555555">089 </font>        g.setColor(<font color="#0000AA">new</font> Color(level, level, level));
<font color="#555555">090 </font>        g.fillRect(x + 1 + 20 * j, y + 1 + 20 * i, 19, 19);
<font color="#555555">091 </font>      }
<font color="#555555">092 </font>    }
<font color="#555555">093 </font>  }
<font color="#555555">094 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing2601.java"><font color="#000055" size=-1>Listing2601.java</font></a></td>
</tr>
</table>
<i>
Listing 26.1: Ausdruck einer Testseite</i></p>

<p>
Die Bildschirmausgabe des Programms kann <a href="k100169.html#programmdruckausgabe">Abbildung 26.1</a>
entnommen werden. Die Druckausgabe sieht &auml;hnlich aus, enth&auml;lt
aber zus&auml;tzlich noch einen Rahmen und die Textausgabe mit den
Informationen zu den Druckmetriken. 
<p>
<a name="programmdruckausgabe"></a>
<img src="images/Drucken11.gif">
<p>

<p><i>
Abbildung 26.1: Das Programm zur Druckausgabe</i></p>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html">&nbsp;Titel&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html">&nbsp;Inhalt&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html">&nbsp;Suchen&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html">&nbsp;Index&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()">&nbsp;DOC&nbsp;</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">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100168.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100170.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100173.html">&nbsp;&gt;&gt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()">&nbsp;API&nbsp;</a>
<td align="right">&copy; 1998, 2007 Guido Kr&uuml;ger &amp; Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>