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
|
<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,k100150.html;106,k100152.html;107,k100154.html;108,k100156.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="k100150.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100152.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100154.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100156.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 23 - Grafikausgabe
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id023003"></a>
<h2>23.3 Elementare Grafikroutinen </h2>
<hr>
<ul>
<li><a href="k100153.html#sectlevel2id023003">23.3 Elementare Grafikroutinen</a>
<ul>
<li><a href="k100153.html#sectlevel3id023003001">23.3.1 Linie</a>
<li><a href="k100153.html#sectlevel3id023003002">23.3.2 Rechteck</a>
<li><a href="k100153.html#sectlevel3id023003003">23.3.3 Polygon</a>
<li><a href="k100153.html#sectlevel3id023003004">23.3.4 Kreis</a>
<li><a href="k100153.html#sectlevel3id023003005">23.3.5 Kreisbogen</a>
</ul>
</ul>
<hr>
<p>
Die Klasse <a href="index_g.html#ixb101149"><font color=#000080><tt>Graphics</tt></font></a>
stellt neben vielen anderen Funktionen auch eine Sammlung von linienbasierten
Zeichenoperationen zur Verfügung. Diese sind zur Darstellung
von einfachen Linien, Rechtecken oder Polygonen sowie von Kreisen,
Ellipsen und Kreisabschnitten geeignet. Wir wollen im folgenden jede
dieser Funktionsgruppen vorstellen und ihre Anwendung an einem Beispiel
zeigen.
<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>
Um nicht jeweils eine komplette Klassendefinition angeben zu müssen,
werden wir in den folgenden Beispielen jeweils nur die Implementierung
der <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>-Methode
zeigen. Diese könnte dann beispielsweise in das folgende Programm
eingebettet werden (auf der DVD haben diese Dateien die Erweiterung
<font color="#660099">.inc</font>):<a name="ixa101540"></a></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>
<a name="grafikrahmenprog"></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">/* GrafikBeispiel.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> GrafikBeispiel
<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> GrafikBeispiel wnd = <font color="#0000AA">new</font> GrafikBeispiel();
<font color="#555555">012 </font> }
<font color="#555555">013 </font>
<font color="#555555">014 </font> <font color="#0000AA">public</font> GrafikBeispiel()
<font color="#555555">015 </font> {
<font color="#555555">016 </font> <font color="#006699">super</font>(<font color="#0000FF">"GrafikBeispiel"</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(300,200);
<font color="#555555">020 </font> setVisible(<font color="#006699">true</font>);
<font color="#555555">021 </font> }
<font color="#555555">022 </font>
<font color="#555555">023 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">024 </font> {
<font color="#555555">025 </font> <font color="#00AA00">//wird in den folgenden Beispielen überlagert</font>
<font color="#555555">026 </font> }
<font color="#555555">027 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/GrafikBeispiel.java"><font color="#000055" size=-1>GrafikBeispiel.java</font></a></td>
</tr>
</table>
<i>
Listing 23.4: Rahmenprogramm für nachfolgende Beispiele</i></p>
<p>
Da die <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>-Methode
in diesem Programm noch keine Ausgabeoperationen enthält, erzeugt
das Programm lediglich ein leeres Fenster mit dem Titel »Grafikbeispiel«:
<p>
<a name="imageid023002"></a>
<img src="images/GrafikBeispiel.gif">
<p>
<p><i>
Abbildung 23.2: Ein einfaches Fenster</i></p>
<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>
Das Beispielprogramm <font color="#000077"><tt>GrafikBeispiel</tt></font>
ist so ungefähr der kleinstmögliche Rahmen, den man für
ein AWT-basiertes Java-Programm vorgeben kann, wenn es ein einzelnes
Fenster enthalten und beim Schließen desselben automatisch beendet
werden soll. Es besteht aus folgenden Komponenten:</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>
<ul>
<li>Die Klasse <font color="#000077"><tt>GrafikBeispiel</tt></font>
ist aus <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>
abgeleitet, um ein Top-Level-Window darzustellen.
<li>Sie enthält eine statische Methode <a href="index_m.html#ixb100150"><font color=#000080><tt>main</tt></font></a>,
die als Eintrittspunkt für das Programm dient und das Fensterobjekt
erzeugt.
<li>Der Konstruktor der Klasse enthält drei Methodenaufrufe,
<a name="ixa101541"><a href="index_s.html#ixb101159"><font color=#000080><tt>setBackground</tt></font></a></a>,
<a href="index_s.html#ixb101160"><font color=#000080><tt>setSize</tt></font></a>
und <a href="index_s.html#ixb101147"><font color=#000080><tt>setVisible</tt></font></a>,
um die Farbe des Fensterhintergrunds und seine Größe einzustellen
und das Fenster auf dem Bildschirm sichtbar zu machen.
<li>Weiterhin enthält der Konstruktor einen Aufruf von <a name="ixa101542"><a href="index_a.html#ixb101157"><font color=#000080><tt>addWindowListener</tt></font></a></a>,
mit dem ein <a href="index_w.html#ixb100159"><font color=#000080><tt>WindowClosingAdapter</tt></font></a>
zum Schließen des Fensters hinzugefügt wird.
<li>Schließlich gibt es eine Methode <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>,
die hier noch leer ist, aber in den nachfolgenden Beispielen dieses
Kapitels den Aufruf verschiedener Grafikroutinen zeigt.
</ul>
<!-- Section -->
<a name="sectlevel3id023003001"></a>
<h3>23.3.1 Linie<a name="ixa101543"></a> </h3>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void drawLine(int x1, int y1, int x2, int y2)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Graphics.html" onClick="this.href=getApiDoc('java.awt.Graphics')"><font color="#660066" size=-1>java.awt.Graphics</font></a></td>
</tr>
</table>
<p>
<a name="ixa101544"></a> Zieht eine Linie von der Position <font color="#000077"><tt>(x1,y1)</tt></font>
zur Position <font color="#000077"><tt>(x2,y2)</tt></font>. Beide
Punkte dürfen an beliebiger Stelle im Fenster liegen, das Einhalten
einer bestimmten Reihenfolge ist nicht erforderlich. Teile der Ausgabe,
die außerhalb des darstellbaren Bereichs liegen, werden, wie
in grafikorientierten Systemen üblich, unterdrückt.
<p>
Das folgende Beispiel zeichnet eine Reihe von gleich hohen Linien,
deren horizontaler Abstand durch einen Zufallszahlengenerator bestimmt
wird. Das Ergebnis hat dadurch Ähnlichkeit mit einem Barcode
(ist aber keiner):
<a name="listingid023005"></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">/* Linien.inc */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#006699">int</font> i;
<font color="#555555">006 </font> <font color="#006699">int</font> x = 80;
<font color="#555555">007 </font>
<font color="#555555">008 </font> <font color="#0000AA">for</font> (i=0; i<60; ++i) {
<font color="#555555">009 </font> g.drawLine(x,40,x,100);
<font color="#555555">010 </font> x += 1+3*Math.random();
<font color="#555555">011 </font> }
<font color="#555555">012 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Linien.inc"><font color="#000055" size=-1>Linien.inc</font></a></td>
</tr>
</table>
<i>
Listing 23.5: Ausgabe von Linien</i></p>
<p>
<a name="imageid023003"></a>
<img src="images/Linien.gif">
<p>
<p><i>
Abbildung 23.3: Ausgabe von Linien</i></p>
<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>
Die Methode <a href="index_d.html#ixb101162"><font color=#000080><tt>drawLine</tt></font></a>
zeichnet Linien grundsätzlich mit einer Dicke von einem Pixel.
Die hier angezeigten unterschiedlich breiten Linien kommen dadurch
zustande, dass zwei oder mehr Linien direkt nebeneinander ausgegeben
werden. Das ist die einfachste Möglichkeit, Linien darzustellen,
die dicker als 1 Pixel sind. Leider gibt es keine einfache Möglichkeit,
gestrichelte oder gepunktete Linien zu zeichnen oder ein selbstdefiniertes
Füllmuster zu verwenden.</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>
<!-- Section -->
<a name="sectlevel3id023003002"></a>
<h3>23.3.2 Rechteck<a name="ixa101545"></a> </h3>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void drawRect(int x, int y, int width, int height)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Graphics.html" onClick="this.href=getApiDoc('java.awt.Graphics')"><font color="#660066" size=-1>java.awt.Graphics</font></a></td>
</tr>
</table>
<p>
<a name="ixa101546"></a> Zeichnet ein Rechteck der Breite <font color="#000077"><tt>width</tt></font>
und der Höhe <font color="#000077"><tt>height</tt></font>, dessen
linke obere Ecke an der Position <font color="#000077"><tt>(x,y)</tt></font>
liegt. Eine größere Breite dehnt das Rechteck nach rechts
aus, eine größere Höhe nach unten.
<p>
Eine Variante von <a href="index_d.html#ixb101164"><font color=#000080><tt>drawRect</tt></font></a>
ist die Methode <a name="ixa101547"><a href="index_d.html#ixb101165"><font color=#000080><tt>drawRoundRect</tt></font></a></a>:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void drawRoundRect(
int x, int y,
int width, int height,
int arcWidth, int arcHeight
)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Graphics.html" onClick="this.href=getApiDoc('java.awt.Graphics')"><font color="#660066" size=-1>java.awt.Graphics</font></a></td>
</tr>
</table>
<p>
Gegenüber <a href="index_d.html#ixb101164"><font color=#000080><tt>drawRect</tt></font></a>
sind hier die Parameter <font color="#000077"><tt>arcWidth</tt></font>
und <font color="#000077"><tt>arcHeight</tt></font> dazugekommen.
Sie bestimmen den horizontalen und vertikalen Radius des Ellipsenabschnitts,
der zur Darstellung der runden »Ecke« verwendet wird.
<p>
Das folgende Beispiel zeichnet eine Kette von nebeneinanderliegenden
Rechtecken, deren Größe durch einen Zufallszahlengenerator
bestimmt wird. Der Zufallszahlengenerator entscheidet auch, ob ein
Rechteck an der Ober- oder Unterseite seines Vorgängers festgemacht
wird:
<a name="listingid023006"></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">/* Rechtecke.inc */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#006699">int</font> x = 10, y = 80;
<font color="#555555">006 </font> <font color="#006699">int</font> sizex, sizey = 0;
<font color="#555555">007 </font>
<font color="#555555">008 </font> <font color="#0000AA">while</font> (x < 280 && y < 180) {
<font color="#555555">009 </font> sizex = 4 + (<font color="#006699">int</font>) (Math.random() * 9);
<font color="#555555">010 </font> <font color="#0000AA">if</font> (Math.random() > 0.5) {
<font color="#555555">011 </font> y += sizey;
<font color="#555555">012 </font> sizey = 4 + (<font color="#006699">int</font>) (Math.random() * 6);
<font color="#555555">013 </font> } <font color="#0000AA">else</font> {
<font color="#555555">014 </font> sizey = 4 + (<font color="#006699">int</font>) (Math.random() * 6);
<font color="#555555">015 </font> y -= sizey;
<font color="#555555">016 </font> }
<font color="#555555">017 </font> g.drawRect(x,y,sizex,sizey);
<font color="#555555">018 </font> x += sizex;
<font color="#555555">019 </font> }
<font color="#555555">020 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Rechtecke.inc"><font color="#000055" size=-1>Rechtecke.inc</font></a></td>
</tr>
</table>
<i>
Listing 23.6: Ausgabe von Rechtecken</i></p>
<p>
<a name="imageid023004"></a>
<img src="images/Rechtecke.gif">
<p>
<p><i>
Abbildung 23.4: Ausgabe von Rechtecken</i></p>
<!-- Section -->
<a name="sectlevel3id023003003"></a>
<h3>23.3.3 Polygon<a name="ixa101548"></a> </h3>
<p>
Mit Hilfe der Methode <a name="ixa101549"><a href="index_d.html#ixb101167"><font color=#000080><tt>drawPolygon</tt></font></a></a>
ist es möglich, Linienzüge zu zeichnen, bei denen das Ende
eines Elements mit dem Anfang des jeweils nächsten verbunden
ist:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void drawPolygon(int[] arx, int[] ary, int cnt)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Graphics.html" onClick="this.href=getApiDoc('java.awt.Graphics')"><font color="#660066" size=-1>java.awt.Graphics</font></a></td>
</tr>
</table>
<p>
<a href="index_d.html#ixb101167"><font color=#000080><tt>drawPolygon</tt></font></a>
erwartet drei Parameter. Der erste ist ein Array mit einer Liste der
<i>x</i>-Koordinaten und der zweite ein Array mit einer Liste der
<i>y</i>-Koordinaten. Beide Arrays müssen so synchronisiert sein,
dass ein Paar von Werten an derselben Indexposition immer auch ein
Koordinatenpaar ergibt. Die Anzahl der gültigen Koordinatenpaare
wird durch den dritten Parameter festgelegt.
<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 zum JDK 1.0 wird das Polygon nach Abschluss der Ausgabe
automatisch geschlossen. Falls der erste und der letzte Punkt nicht
identisch sind, werden diese durch eine zusätzliche Linie miteinander
verbunden. Soll dagegen ein nichtgeschlossenes Polygon gezeichnet
werden, so kann dazu die Methode <a name="ixa101550"><a href="index_d.html#ixb101168"><font color=#000080><tt>drawPolyline</tt></font></a></a>
verwendet 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>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void drawPolyline(int[] arx, int[] ary, int cnt)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Graphics.html" onClick="this.href=getApiDoc('java.awt.Graphics')"><font color="#660066" size=-1>java.awt.Graphics</font></a></td>
</tr>
</table>
<p>
Eine zweite Variante, Polygone zu zeichnen, besteht darin, zunächst
ein Objekt der Klasse <a name="ixa101551"><a href="index_p.html#ixb101169"><font color=#000080><tt>Polygon</tt></font></a></a>
zu konstruieren und dieses dann an <a href="index_d.html#ixb101167"><font color=#000080><tt>drawPolygon</tt></font></a>
zu übergeben. <a href="index_p.html#ixb101169"><font color=#000080><tt>Polygon</tt></font></a>
besitzt zwei Konstruktoren, von denen einer parameterlos ist und der
andere dieselben Parameter wie die oben beschriebene Methode <a href="index_d.html#ixb101167"><font color=#000080><tt>drawPolygon</tt></font></a>
besitzt:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void Polygon()
public void Polygon(int[] arx, int[] ary, int cnt)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Polygon.html" onClick="this.href=getApiDoc('java.awt.Polygon')"><font color="#660066" size=-1>java.awt.Polygon</font></a></td>
</tr>
</table>
<p>
Mit Hilfe der Methode <a name="ixa101552"><a href="index_a.html#ixb101170"><font color=#000080><tt>addPoint</tt></font></a></a>
kann ein Polygon um weitere Punkte erweitert werden. Schließlich
kann das fertige Polygon an die Methode <a href="index_d.html#ixb101167"><font color=#000080><tt>drawPolygon</tt></font></a>
übergeben werden, die dann wie folgt aufzurufen ist:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void drawPolygon(Polygon p)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Graphics.html" onClick="this.href=getApiDoc('java.awt.Graphics')"><font color="#660066" size=-1>java.awt.Graphics</font></a></td>
</tr>
</table>
<p>
Das folgende Beispiel gibt den Buchstaben »F« mit Hilfe
eines geschlossenen Polygons aus:
<a name="listingid023007"></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">/* Polygon.inc */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#006699">int</font>[] arx = {50,50,120,120,80,80,100,100,80,80};
<font color="#555555">006 </font> <font color="#006699">int</font>[] ary = {170,40,40,70,70,100,100,130,130,170};
<font color="#555555">007 </font>
<font color="#555555">008 </font> g.drawPolygon(arx,ary,arx.length);
<font color="#555555">009 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Polygon.inc"><font color="#000055" size=-1>Polygon.inc</font></a></td>
</tr>
</table>
<i>
Listing 23.7: Ausgabe eines Polygons</i></p>
<p>
<a name="imageid023005"></a>
<img src="images/Polygon.gif">
<p>
<p><i>
Abbildung 23.5: Ausgabe eines Polygons</i></p>
<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>
An diesem Beispiel läßt sich ein potentielles Problem bei
der Angabe von <i>x</i>- und <i>y</i>-Koordinaten zur Bestimmung einzelner
Punkte in einem Fenster erkennen. Obwohl das Fenster durch Aufruf
der Methode <a href="index_s.html#ixb101160"><font color=#000080><tt>setSize</tt></font></a>
eine Größe von 300*200 Pixeln hat, ist die untere Kante
des »F« bereits sehr viel näher am unteren Rand des
Fensters, als dessen <i>y</i>-Wert von 170 vermuten läßt,
insbesondere, wenn man bedenkt, dass der obere Rand des Buchstabens
50 Pixel vom Fensterrand entfernt ist. Der Grund dafür ist, dass
die Größenangabe für <a href="index_s.html#ixb101160"><font color=#000080><tt>setSize</tt></font></a>
die Größe des kompletten Fensters festlegt und damit auch
den erforderlichen Platz für die Titelleiste und gegebenenfalls
das Menü einschließt. Der zur Ausgabe zur Verfügung
stehende Client-Bereich ist daher in aller Regel deutlich kleiner.
Wir werden später noch lernen, wie die <i>exakte</i> Größe
des Client-Bereichs bestimmt werden kann.</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"> Warnung </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="sectlevel3id023003004"></a>
<h3>23.3.4 Kreis<a name="ixa101553"></a> </h3>
<p>
Die <a href="index_g.html#ixb101149"><font color=#000080><tt>Graphics</tt></font></a>-Klasse
von Java erlaubt sowohl das Zeichnen von Kreisen als auch von Ellipsen
und Kreisabschnitten. Ein Kreis wird dabei als Spezialisierung einer
Ellipse angesehen, und beide Objekte werden mit der Methode <a name="ixa101554"><a href="index_d.html#ixb101172"><font color=#000080><tt>drawOval</tt></font></a></a>
gezeichnet:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void drawOval(int x, int y, int width, int height)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Graphics.html" onClick="this.href=getApiDoc('java.awt.Graphics')"><font color="#660066" size=-1>java.awt.Graphics</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>
Anders als in anderen Grafiksystemen werden bei dieser Methode nicht
der Mittelpunkt und der Radius des Kreises angegeben, sondern die
übergebenen Parameter spezifizieren ein Rechteck der Größe
<font color="#000077"><tt>width</tt></font> und <font color="#000077"><tt>heigth</tt></font>,
dessen linke obere Ecke an der Position <font color="#000077"><tt>(x,y)</tt></font>
liegt. Gezeichnet wird dann der größte Kreis, der vollständig
in das Rechteck hineinpasst.</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>
Daß diese Vorgehensweise zwar untypisch ist, aber durchaus ihre
Vorteile haben kann, zeigt das folgende Beispiel:
<a name="listingid023008"></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">/* Kreise.inc */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#006699">int</font> r = 8;
<font color="#555555">006 </font> <font color="#006699">int</font> i, j;
<font color="#555555">007 </font> <font color="#006699">int</font> x, y;
<font color="#555555">008 </font>
<font color="#555555">009 </font> <font color="#0000AA">for</font> (i=1; i<=10; ++i) {
<font color="#555555">010 </font> x = 150 - r * i;
<font color="#555555">011 </font> y = (<font color="#006699">int</font>) (40 + (i - 1) * 1.7321 * r);
<font color="#555555">012 </font> <font color="#0000AA">for</font> (j=1; j<=i; ++j) {
<font color="#555555">013 </font> g.drawOval(x,y,2*r,2*r);
<font color="#555555">014 </font> x += 2 * r;
<font color="#555555">015 </font> }
<font color="#555555">016 </font> }
<font color="#555555">017 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Kreise.inc"><font color="#000055" size=-1>Kreise.inc</font></a></td>
</tr>
</table>
<i>
Listing 23.8: Ausgabe von Kreisen</i></p>
<p>
Das Programm gibt dabei eine Pyramide von Kreisen aus. Da die an <a href="index_d.html#ixb101172"><font color=#000080><tt>drawOval</tt></font></a>
übergebenen Parameter bereits das umgebende Rechteck bezeichnen,
kann die Figur wie eine Pyramide aus Rechtecken dargestellt werden:
<p>
<a name="imageid023006"></a>
<img src="images/Kreise.gif">
<p>
<p><i>
Abbildung 23.6: Ausgabe von Kreisen</i></p>
<!-- Section -->
<a name="sectlevel3id023003005"></a>
<h3>23.3.5 Kreisbogen<a name="ixa101555"></a> </h3>
<p>
Ein Kreisbogen ist ein zusammenhängender Abschnitt der Umfangslinie
eines Kreises. Er kann mit der Methode <a name="ixa101556"><a href="index_d.html#ixb101174"><font color=#000080><tt>drawArc</tt></font></a></a>
gezeichnet werden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void drawArc(
int x, int y, int width, int height,
int startAngle, int arcAngle
)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Graphics.html" onClick="this.href=getApiDoc('java.awt.Graphics')"><font color="#660066" size=-1>java.awt.Graphics</font></a></td>
</tr>
</table>
<p>
Die ersten vier Parameter bezeichnen dabei den Kreis bzw. die Ellipse
so, wie dies auch bei <a href="index_d.html#ixb101172"><font color=#000080><tt>drawOval</tt></font></a>
der Fall war. Mit <font color="#000077"><tt>startAngle</tt></font>
wird der Winkel angegeben, an dem mit dem Kreisabschnitt begonnen
werden soll, und <font color="#000077"><tt>arcAngle</tt></font> gibt
den zu überdeckenden Bereich an. Dabei bezeichnet ein Winkel
von 0 Grad die 3-Uhr-Position, und positive Winkel werden <i>entgegen</i>
dem Uhrzeigersinn gemessen. Als Einheit wird <i>Grad</i> verwendet
und nicht das sonst übliche Bogenmaß.
<p>
Das folgende Beispiel zeichnet durch wiederholten Aufruf der Methode
<a href="index_d.html#ixb101174"><font color=#000080><tt>drawArc</tt></font></a>
eine Ellipse mit einer gestrichelten Umfangslinie. Ein Aufruf zeichnet
dabei jeweils 4 Grad der Ellipse und läßt dann eine Lücke
von 3 Grad, bevor das nächste Stück gezeichnet wird:
<a name="listingid023009"></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">/* KreisBoegen.inc */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#006699">int</font> line = 4;
<font color="#555555">006 </font> <font color="#006699">int</font> gap = 3;
<font color="#555555">007 </font> <font color="#006699">int</font> angle = 0;
<font color="#555555">008 </font>
<font color="#555555">009 </font> <font color="#0000AA">while</font> (angle < 360) {
<font color="#555555">010 </font> g.drawArc(20,40,250,140,angle,line);
<font color="#555555">011 </font> angle += gap + line;
<font color="#555555">012 </font> }
<font color="#555555">013 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/KreisBoegen.inc"><font color="#000055" size=-1>KreisBoegen.inc</font></a></td>
</tr>
</table>
<i>
Listing 23.9: Ausgabe von Kreisbögen</i></p>
<p>
<a name="imageid023007"></a>
<img src="images/Kreisboegen.gif">
<p>
<p><i>
Abbildung 23.7: Ausgabe von Kreisbögen</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"> 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="k100150.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100152.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100154.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100156.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>
|