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
|
<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,k100219.html;106,k100220.html;107,k100222.html;108,k100224.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="k100219.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100220.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100222.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100224.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 33 - Eigene Dialogelemente
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id033002"></a>
<h2>33.2 Entwicklung einer <a name="ixa102293">7-Segment-Anzeige</a>
</h2>
<hr>
<ul>
<li><a href="k100221.html#sectlevel2id033002">33.2 Entwicklung einer 7-Segment-Anzeige</a>
<ul>
<li><a href="k100221.html#sectlevel3id033002001">33.2.1 Anforderungen</a>
<li><a href="k100221.html#gui7segmentanzeige">33.2.2 Bildschirmanzeige</a>
<li><a href="k100221.html#sectlevel3id033002003">33.2.3 Ereignisbehandlung</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id033002001"></a>
<h3>33.2.1 Anforderungen </h3>
<p>
In diesem Abschnitt wollen wir uns ein konkretes Beispiel zur Komponentenentwicklung
ansehen. Dazu soll eine einstellige 7-Segment-Anzeige entwickelt werden,
die in der Lage ist, die Ziffern 0 bis 9 darzustellen. Des weiteren
sollen folgende Eigenschaften realisiert werden:
<ul>
<li>Die Anzeige soll in der Größe skalierbar sein und sich
zu allen Layoutmanagern kompatibel verhalten.
<li>Die 7-Segment-Anzeige soll auf Mausklicks reagieren. Nach einem
Mausklick soll sie den Fokus erhalten. Nach Drücken der linken
Maustaste soll der Anzeigewert herunter- und nach Drücken der
rechten Maustaste heraufgezählt werden. Wird während des
Mausklicks die <font color="#007700"><tt>[UMSCHALT]</tt></font>-Taste
gedrückt, so soll lediglich der Fokus zugewiesen werden, der
Wert aber unverändert bleiben.
<li>Der Anzeigewert soll durch Drücken der Tasten <font color="#007700"><tt>[+]</tt></font>
und <font color="#007700"><tt>[-]</tt></font> herauf- bzw. heruntergezählt
werden. Wird eine Zifferntaste gedrückt, so soll die Anzeige
auf diesen Wert gesetzt werden.
<li>Eine Anzeige, die den Fokus hat, soll visuell von einer ohne Fokus
unterscheidbar sein. Der Fokus soll weiterhin - wie unter Windows
üblich - mit Hilfe der Tasten <font color="#007700"><tt>[TAB]</tt></font>
und <font color="#007700"><tt>[UMSCHALT]</tt></font>+<font color="#007700"><tt>[TAB]</tt></font>
von einem Dialogelement zum nächsten weitergereicht werden können.
</ul>
<!-- Section -->
<a name="gui7segmentanzeige"></a>
<h3>33.2.2 Bildschirmanzeige </h3>
<p>
Die Architektur unserer Anzeigekomponente ist denkbar einfach. Wir
definieren dazu eine neue Klasse <a name="ixa102294"><a href="index_s.html#ixb101714"><font color=#000080><tt>Segment7</tt></font></a></a>,
die aus <a href="index_c.html#ixb101712"><font color=#000080><tt>Canvas</tt></font></a>
abgeleitet wird. <a href="index_s.html#ixb101714"><font color=#000080><tt>Segment7</tt></font></a>
besitzt eine Membervariable <font color="#000077"><tt>digit</tt></font>,
die den aktuellen Anzeigewert speichert. Dieser kann mit den öffentlichen
Methoden <font color="#000077"><tt>getValue</tt></font> und <font color="#000077"><tt>setValue</tt></font>
abgefragt bzw. gesetzt werden. Die Klasse bekommt zwei Konstruktoren,
die es erlauben, den Anzeigewert wahlweise bei der Instanzierung zu
setzen oder die Voreinstellung 0 zu verwenden.
<p>
<a href="index_s.html#ixb101714"><font color=#000080><tt>Segment7</tt></font></a>
überlagert die Methoden <a name="ixa102295"><a href="index_g.html#ixb101706"><font color=#000080><tt>getPreferredSize</tt></font></a></a>
und <a name="ixa102296"><a href="index_g.html#ixb101715"><font color=#000080><tt>getMinimumSize</tt></font></a></a>
der Klasse <a href="index_c.html#ixb101302"><font color=#000080><tt>Component</tt></font></a>,
um den Layoutmanagern die gewünschte Größe mitzuteilen:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public Dimension getPreferredSize()
public Dimension getMinimumSize()
</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>
Beide Methoden liefern ein Objekt der Klasse <a name="ixa102297"><a href="index_d.html#ixb101716"><font color=#000080><tt>Dimension</tt></font></a></a>,
also ein rechteckiges Element mit einer Höhe und Breite. <a href="index_g.html#ixb101706"><font color=#000080><tt>getPreferredSize</tt></font></a>
teilt dem Layoutmanager mit, welches die <i>gewünschte</i> Größe
der Komponente ist, und <a href="index_g.html#ixb101715"><font color=#000080><tt>getMinimumSize</tt></font></a>
gibt an, welches die kleinste akzeptable Größe ist. Der
Layoutmanager <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
beispielsweise verwendet <a href="index_g.html#ixb101706"><font color=#000080><tt>getPreferredSize</tt></font></a>,
um die Größe der Komponente zu bestimmen. <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
gibt die Größe selbst vor und passt sie an die Gitterelemente
an. Durch Aufruf von <a name="ixa102298"><a href="index_p.html#ixb101611"><font color=#000080><tt>pack</tt></font></a></a>
kann allerdings auch <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
dazu veranlaßt werden, die gewünschte Größe
der Komponenten abzufragen und zur Dimensionierung des Fensters (und
damit letztlich zur Dimensionierung der Einzelkomponenten) zu verwenden.
Seit dem JDK 1.1 gibt es noch eine dritte Methode <a name="ixa102299"><a href="index_g.html#ixb101717"><font color=#000080><tt>getMaximumSize</tt></font></a></a>,
mit der die Komponente ihre maximale Größe mitteilen kann.
Sie wird in unserem Beispiel nicht benötigt.
<p>
Zur Darstellung der Leuchtdiodenanzeige auf dem Bildschirm wird die
Methode <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>
überlagert; in ihr befindet sich die Logik zur Darstellung der
sieben Segmente (siehe <a href="k100221.html#aufbau7segment">Abbildung 33.1</a>).
In <a href="index_s.html#ixb101714"><font color=#000080><tt>Segment7</tt></font></a>
wurden dazu drei Arrays <font color="#000077"><tt>digits</tt></font>,
<font color="#000077"><tt>polysx</tt></font> und <font color="#000077"><tt>polysy</tt></font>
definiert, die die Belegung der Segmente für jede einzelne Ziffer
darstellen und die Eckpunkte jedes einzelnen Segments vorgeben.
<p>
<a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>
verwendet das Array <font color="#000077"><tt>digits</tt></font>,
um herauszufinden, welche Segmente zur Darstellung der aktuellen Ziffer
verwendet werden. Für jedes der beteiligten Segmente wird dann
aus den Arrays <font color="#000077"><tt>polysx</tt></font> und <font color="#000077"><tt>polysy</tt></font>
das passende Polygon gebildet und mit <a href="index_f.html#ixb101179"><font color=#000080><tt>fillPolygon</tt></font></a>
angezeigt. Als interne Berechnungseinheit werden zwei Parameter <font color="#000077"><tt>dx</tt></font>
und <font color="#000077"><tt>dy</tt></font> verwendet, die beim Aufruf
von <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>
aus dem für die Komponente verfügbaren Platz berechnet werden.
<p>
<a name="aufbau7segment"></a>
<img src="images/SiebenSegment.gif">
<p>
<p><i>
Abbildung 33.1: Der Aufbau der 7-Segment-Anzeige</i></p>
<!-- Section -->
<a name="sectlevel3id033002003"></a>
<h3>33.2.3 Ereignisbehandlung </h3>
<p>
Wie in <a href="k100180.html#kapiteleventhandling">Kapitel 28</a>
erwähnt, erfolgt die Ereignisbehandlung in selbstdefinierten
Komponenten üblicherweise auf der Basis des vierten vorgestellten
Architekturmodells. Bei diesem werden die Ereignisse nicht durch registrierte
Listener-Klassen bearbeitet, sondern durch Überlagern der Methoden
<font color="#000077"><tt>process...Event</tt></font> der Klasse <a href="index_c.html#ixb101302"><font color=#000080><tt>Component</tt></font></a>.
<p>
Damit die Ereignisse tatsächlich an diese Methoden weitergegeben
werden, müssen sie zuvor durch Aufruf von <a name="ixa102300"><a href="index_e.html#ixb101500"><font color=#000080><tt>enableEvents</tt></font></a></a>
und Übergabe der zugehörigen Ereignismaske aktiviert werden.
Da wir Component-, Focus-, Key- und Mouse-Ereignisse behandeln wollen,
rufen wir <a href="index_e.html#ixb101500"><font color=#000080><tt>enableEvents</tt></font></a>
mit den Konstanten <a name="ixa102301"><a href="index_c.html#ixb101503"><font color=#000080><tt>AWTEvent.COMPONENT_EVENT_MASK</tt></font></a></a>,
<a name="ixa102302"><a href="index_f.html#ixb101718"><font color=#000080><tt>AWTEvent.FOCUS_EVENT_MASK</tt></font></a></a>,
<a name="ixa102303"><a href="index_m.html#ixb101719"><font color=#000080><tt>AWTEvent.MOUSE_EVENT_MASK</tt></font></a></a>
und <a name="ixa102304"><a href="index_k.html#ixb101720"><font color=#000080><tt>AWTEvent.KEY_EVENT_MASK</tt></font></a></a>
auf. Beim Überlagern dieser Methoden sollte in jedem Fall der
entsprechende Ereignishandler der Superklasse aufgerufen werden, um
die korrekte Standard-Ereignisbehandlung sicherzustellen.
<p>
Die Reaktion auf Mausklicks wird durch Überlagern der Methode
<a name="ixa102305"><a href="index_p.html#ixb101499"><font color=#000080><tt>processMouseEvent</tt></font></a></a>
realisiert. Hier wird zunächst überprüft, ob es sich
um ein <a name="ixa102306"><a href="index_m.html#ixb101721"><font color=#000080><tt>MOUSE_PRESSED</tt></font></a></a>-Ereignis
handelt, also ob eine der Maustasten <i>gedrückt</i> wurde. Ist
dies der Fall, wird dem Anzeigeelement durch Aufruf von <a name="ixa102307"><a href="index_r.html#ixb101522"><font color=#000080><tt>requestFocus</tt></font></a></a>
der Eingabefokus zugewiesen.
<p>
Anschließend wird überprüft, ob die <font color="#007700"><tt>[UMSCHALT]</tt></font>-Taste
gedrückt wurde. Ist das der Fall, wird die Ereignisbehandlung
beendet, andernfalls wird der Anzeigewert hoch- bzw. heruntergezählt,
je nachdem, ob die rechte oder linke Maustaste gedrückt wurde.
Am Ende von <a href="index_p.html#ixb101499"><font color=#000080><tt>processMouseEvent</tt></font></a>
wird in jedem Fall <font color="#000077"><tt>super.processMouseEvent</tt></font>
aufgerufen, um sicherzustellen, dass die normale Ereignisbehandlung
aufgerufen wird.
<p>
Ein selbstdefiniertes Dialogelement bekommt nicht automatisch den
Fokus zugewiesen, wenn mit der Maus darauf geklickt wird. Statt dessen
muss es selbst auf Mausklicks reagieren und sich - wie zuvor beschrieben
- durch Aufruf von <a href="index_r.html#ixb101522"><font color=#000080><tt>requestFocus</tt></font></a>
selbst den Fokus zuweisen. Bei jeder Fokusänderung wird ein Focus-Event
ausgelöst, das wir durch Überlagern der Methode <a name="ixa102308"><a href="index_p.html#ixb101722"><font color=#000080><tt>processFocusEvent</tt></font></a></a>
bearbeiten. Hier unterscheiden wir zunächst, ob es sich um ein
<a name="ixa102309"><a href="index_f.html#ixb101723"><font color=#000080><tt>FOCUS_GAINED</tt></font></a></a>-
oder <a name="ixa102310"><a href="index_f.html#ixb101724"><font color=#000080><tt>FOCUS_LOST</tt></font></a></a>-Ereignis
handelt, und setzen eine interne Statusvariable <font color="#000077"><tt>hasfocus</tt></font>
entsprechend. Diese wird nach dem anschließenden Aufruf von
<a name="ixa102311"><a href="index_r.html#ixb101509"><font color=#000080><tt>repaint</tt></font></a></a>
verwendet, um in <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>
durch Modifikation der Anzeigefarbe ein visuelles Feedback zu geben.
Hat ein Element den Fokus, so ist die Farbe der Anzeigesegmente gelb,
andernfalls rot.
<p>
Seit dem JDK 1.1 gibt es einen Mechanismus, der es erlaubt, mit den
Tasten <font color="#007700"><tt>[TAB]</tt></font> und <font color="#007700"><tt>[UMSCHALT]</tt></font>+<font color="#007700"><tt>[TAB]</tt></font>
zwischen den Eingabefeldern eines Dialogs zu wechseln. Genauer gesagt
wird dadurch der Fokus an das nächste Element weiter- bzw. zum
vorigen zurückgegeben. Da diese Vorgehensweise nicht bei jedem
Dialogelement sinnvoll ist, kann das Dialogelement sie durch Überlagern
der Methode <a name="ixa102312"><a href="index_i.html#ixb101725"><font color=#000080><tt>isFocusTraversable</tt></font></a></a>
selbst bestimmen. Liefert <a href="index_i.html#ixb101725"><font color=#000080><tt>isFocusTraversable</tt></font></a>
den Rückgabewert <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>,
so nimmt das Objekt an der <font color="#007700"><tt>[TAB]</tt></font>-Behandlung
teil, andernfalls nicht. Die Klasse <a href="index_s.html#ixb101714"><font color=#000080><tt>Segment7</tt></font></a>
überlagert <a href="index_i.html#ixb101725"><font color=#000080><tt>isFocusTraversable</tt></font></a>
und gibt <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>
zurück. So kann mit <font color="#007700"><tt>[TAB]</tt></font>
und <font color="#007700"><tt>[UMSCHALT]</tt></font>+<font color="#007700"><tt>[TAB]</tt></font>
wie besprochen zwischen den 7-Segment-Anzeigen gewechselt werden.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Mit der Version 1.4 des JDK wurde <a href="index_i.html#ixb101725"><font color=#000080><tt>isFocusTraversable</tt></font></a>
als deprecated markiert, und sollte durch <a name="ixa102313"><a href="index_i.html#ixb101726"><font color=#000080><tt>isFocusable</tt></font></a></a>
ersetzt werden. Wegen der großen Anzahl vorhandener Bugs und
Inkonsistenzen wurde nämlich die Architektur des <a name="ixa102314"><i>Fokus-Subsystems</i></a>
vollständig überarbeitet und mit einem teilweise neuen API
versehen. Programmcode, der mit der Fokussierung von AWT- oder Swing-Komponenten
zu tun hat, muss daher beim Umstieg auf das JDK 1.4 oder neuere Versionen
vermutlich überarbeitet werden.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#FF9900">
<tr>
<td><font color="#FFFFFF"> JDK1.1-6.0 </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Ein Dialogelement enthält nur dann Tastatureingaben, wenn es
den Fokus hat. Durch den zuvor beschriebenen Mechanismus des Aufrufs
von <a href="index_r.html#ixb101522"><font color=#000080><tt>requestFocus</tt></font></a>
stellen wir sicher, dass nach einem Mausklick bzw. nach dem Wechsel
des Fokus mit <font color="#007700"><tt>[TAB]</tt></font> und <font color="#007700"><tt>[UMSCHALT]</tt></font>+<font color="#007700"><tt>[TAB]</tt></font>
Tastaturereignisse an das Element gesendet werden. Diese werden durch
Überlagern der Methode <a name="ixa102315"><a href="index_p.html#ixb101504"><font color=#000080><tt>processKeyEvent</tt></font></a></a>
behandelt. Wir überprüfen darin zunächst, ob das Ereignis
vom Typ <a name="ixa102316"><a href="index_k.html#ixb101728"><font color=#000080><tt>KEY_PRESSED</tt></font></a></a>
ist, und besorgen dann mit <a name="ixa102317"><a href="index_g.html#ixb101413"><font color=#000080><tt>getKeyChar</tt></font></a></a>
den Wert der gedrückten Taste. Ist er '+', so wird der Anzeigewert
um 1 erhöht, bei '-' entsprechend verringert. Wurde eine der
Zifferntasten gedrückt, so erhält das Anzeigeelement diesen
Wert. Anschließend wird durch Aufruf von <a href="index_r.html#ixb101509"><font color=#000080><tt>repaint</tt></font></a>
die Anzeige neu gezeichnet.
<p>
Ein Component-Ereignis brauchen wir in unserem Beispiel nur, damit
wir dem Dialogelement unmittelbar nach der Anzeige auf dem Bildschirm
den Fokus zuweisen können. Dazu überlagern wir die Methode
<a name="ixa102318"><a href="index_p.html#ixb101729"><font color=#000080><tt>processComponentEvent</tt></font></a></a>
und überprüfen, ob das Ereignis vom Typ <a name="ixa102319"><a href="index_c.html#ixb101730"><font color=#000080><tt>COMPONENT_SHOWN</tt></font></a></a>
ist. In diesem Fall wird <a href="index_r.html#ixb101522"><font color=#000080><tt>requestFocus</tt></font></a>
aufgerufen, andernfalls passiert nichts.
<p>
Damit ist die Konstruktion der Komponente auch schon abgeschlossen.
Durch die Definition von <a href="index_g.html#ixb101706"><font color=#000080><tt>getPreferredSize</tt></font></a>
und <a href="index_g.html#ixb101715"><font color=#000080><tt>getMinimumSize</tt></font></a>
und die automatische Skalierung in der <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>-Methode
verhält sich unsere neue Komponente so, wie es die Layoutmanager
von ihr erwarten. Daher kann sie wie eine vordefinierte Komponente
verwendet werden. Hier ist der Quellcode von <a href="index_s.html#ixb101714"><font color=#000080><tt>Segment7</tt></font></a>:
<a name="listingid033001"></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">/* Segment7.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">class</font> Segment7
<font color="#555555">007 </font><font color="#0000AA">extends</font> Canvas
<font color="#555555">008 </font>{
<font color="#555555">009 </font> <font color="#0000AA">private</font> <font color="#006699">int</font> digit;
<font color="#555555">010 </font> <font color="#0000AA">private</font> <font color="#006699">boolean</font> hasfocus;
<font color="#555555">011 </font> <font color="#0000AA">private</font> <font color="#006699">int</font>[][] polysx = {
<font color="#555555">012 </font> { 1, 2, 8, 9, 8, 2}, <font color="#00AA00">//Segment 0</font>
<font color="#555555">013 </font> { 9,10,10, 9, 8, 8}, <font color="#00AA00">//Segment 1</font>
<font color="#555555">014 </font> { 9,10,10, 9, 8, 8}, <font color="#00AA00">//Segment 2</font>
<font color="#555555">015 </font> { 1, 2, 8, 9, 8, 2}, <font color="#00AA00">//Segment 3</font>
<font color="#555555">016 </font> { 1, 2, 2, 1, 0, 0}, <font color="#00AA00">//Segment 4</font>
<font color="#555555">017 </font> { 1, 2, 2, 1, 0, 0}, <font color="#00AA00">//Segment 5</font>
<font color="#555555">018 </font> { 1, 2, 8, 9, 8, 2}, <font color="#00AA00">//Segment 6</font>
<font color="#555555">019 </font> };
<font color="#555555">020 </font> <font color="#0000AA">private</font> <font color="#006699">int</font>[][] polysy = {
<font color="#555555">021 </font> { 1, 0, 0, 1, 2, 2}, <font color="#00AA00">//Segment 0</font>
<font color="#555555">022 </font> { 1, 2, 8, 9, 8, 2}, <font color="#00AA00">//Segment 1</font>
<font color="#555555">023 </font> { 9,10,16,17,16,10}, <font color="#00AA00">//Segment 2</font>
<font color="#555555">024 </font> {17,16,16,17,18,18}, <font color="#00AA00">//Segment 3</font>
<font color="#555555">025 </font> { 9,10,16,17,16,10}, <font color="#00AA00">//Segment 4</font>
<font color="#555555">026 </font> { 1, 2, 8, 9, 8, 2}, <font color="#00AA00">//Segment 5</font>
<font color="#555555">027 </font> { 9, 8, 8, 9,10,10}, <font color="#00AA00">//Segment 6</font>
<font color="#555555">028 </font> };
<font color="#555555">029 </font> <font color="#0000AA">private</font> <font color="#006699">int</font>[][] digits = {
<font color="#555555">030 </font> {1,1,1,1,1,1,0}, <font color="#00AA00">//Ziffer 0</font>
<font color="#555555">031 </font> {0,1,1,0,0,0,0}, <font color="#00AA00">//Ziffer 1</font>
<font color="#555555">032 </font> {1,1,0,1,1,0,1}, <font color="#00AA00">//Ziffer 2</font>
<font color="#555555">033 </font> {1,1,1,1,0,0,1}, <font color="#00AA00">//Ziffer 3</font>
<font color="#555555">034 </font> {0,1,1,0,0,1,1}, <font color="#00AA00">//Ziffer 4</font>
<font color="#555555">035 </font> {1,0,1,1,0,1,1}, <font color="#00AA00">//Ziffer 5</font>
<font color="#555555">036 </font> {1,0,1,1,1,1,1}, <font color="#00AA00">//Ziffer 6</font>
<font color="#555555">037 </font> {1,1,1,0,0,0,0}, <font color="#00AA00">//Ziffer 7</font>
<font color="#555555">038 </font> {1,1,1,1,1,1,1}, <font color="#00AA00">//Ziffer 8</font>
<font color="#555555">039 </font> {1,1,1,1,0,1,1} <font color="#00AA00">//Ziffer 9</font>
<font color="#555555">040 </font> };
<font color="#555555">041 </font>
<font color="#555555">042 </font> <font color="#0000AA">public</font> Segment7()
<font color="#555555">043 </font> {
<font color="#555555">044 </font> <font color="#006699">this</font>(0);
<font color="#555555">045 </font> }
<font color="#555555">046 </font>
<font color="#555555">047 </font> <font color="#0000AA">public</font> Segment7(<font color="#006699">int</font> digit)
<font color="#555555">048 </font> {
<font color="#555555">049 </font> <font color="#006699">super</font>();
<font color="#555555">050 </font> <font color="#006699">this</font>.digit = digit;
<font color="#555555">051 </font> <font color="#006699">this</font>.hasfocus = <font color="#006699">false</font>;
<font color="#555555">052 </font> enableEvents(AWTEvent.COMPONENT_EVENT_MASK);
<font color="#555555">053 </font> enableEvents(AWTEvent.FOCUS_EVENT_MASK);
<font color="#555555">054 </font> enableEvents(AWTEvent.MOUSE_EVENT_MASK);
<font color="#555555">055 </font> enableEvents(AWTEvent.KEY_EVENT_MASK);
<font color="#555555">056 </font> }
<font color="#555555">057 </font>
<font color="#555555">058 </font> <font color="#0000AA">public</font> Dimension getPreferredSize()
<font color="#555555">059 </font> {
<font color="#555555">060 </font> <font color="#0000AA">return</font> <font color="#0000AA">new</font> Dimension(5*10,5*18);
<font color="#555555">061 </font> }
<font color="#555555">062 </font>
<font color="#555555">063 </font> <font color="#0000AA">public</font> Dimension getMinimumSize()
<font color="#555555">064 </font> {
<font color="#555555">065 </font> <font color="#0000AA">return</font> <font color="#0000AA">new</font> Dimension(1*10,1*18);
<font color="#555555">066 </font> }
<font color="#555555">067 </font>
<font color="#555555">068 </font> <font color="#0000AA">public</font> <font color="#006699">boolean</font> isFocusTraversable()
<font color="#555555">069 </font> {
<font color="#555555">070 </font> <font color="#0000AA">return</font> <font color="#006699">true</font>;
<font color="#555555">071 </font> }
<font color="#555555">072 </font>
<font color="#555555">073 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">074 </font> {
<font color="#555555">075 </font> Color darkred = <font color="#0000AA">new</font> Color(127,0,0);
<font color="#555555">076 </font> Color lightred = <font color="#0000AA">new</font> Color(255,0,0);
<font color="#555555">077 </font> Color yellow = <font color="#0000AA">new</font> Color(255,255,0);
<font color="#555555">078 </font> <font color="#00AA00">//dx und dy berechnen</font>
<font color="#555555">079 </font> <font color="#006699">int</font> dx = getSize().width / 10;
<font color="#555555">080 </font> <font color="#006699">int</font> dy = getSize().height / 18;
<font color="#555555">081 </font> <font color="#00AA00">//Hintergrund</font>
<font color="#555555">082 </font> g.setColor(darkred);
<font color="#555555">083 </font> g.fillRect(0,0,getSize().width,getSize().height);
<font color="#555555">084 </font> <font color="#00AA00">//Segmente</font>
<font color="#555555">085 </font> <font color="#0000AA">if</font> (hasfocus) {
<font color="#555555">086 </font> g.setColor(yellow);
<font color="#555555">087 </font> } <font color="#0000AA">else</font> {
<font color="#555555">088 </font> g.setColor(lightred);
<font color="#555555">089 </font> }
<font color="#555555">090 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i=0; i < 7; ++i) { <font color="#00AA00">//alle Segmente</font>
<font color="#555555">091 </font> <font color="#0000AA">if</font> (digits[digit][i] == 1) {
<font color="#555555">092 </font> Polygon poly = <font color="#0000AA">new</font> Polygon();
<font color="#555555">093 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> j = 0; j < 6; ++j) { <font color="#00AA00">//alle Eckpunkte</font>
<font color="#555555">094 </font> poly.addPoint(dx*polysx[i][j],dy*polysy[i][j]);
<font color="#555555">095 </font> }
<font color="#555555">096 </font> g.fillPolygon(poly);
<font color="#555555">097 </font> }
<font color="#555555">098 </font> }
<font color="#555555">099 </font> <font color="#00AA00">//Trennlinien</font>
<font color="#555555">100 </font> g.setColor(darkred);
<font color="#555555">101 </font> g.drawLine(0,0,dx*10,dy*10);
<font color="#555555">102 </font> g.drawLine(0,8*dy,10*dx,18*dy);
<font color="#555555">103 </font> g.drawLine(0,10*dy,10*dx,0);
<font color="#555555">104 </font> g.drawLine(0,18*dy,10*dx,8*dy);
<font color="#555555">105 </font> }
<font color="#555555">106 </font>
<font color="#555555">107 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> getValue()
<font color="#555555">108 </font> {
<font color="#555555">109 </font> <font color="#0000AA">return</font> digit;
<font color="#555555">110 </font> }
<font color="#555555">111 </font>
<font color="#555555">112 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> setValue(<font color="#006699">int</font> value)
<font color="#555555">113 </font> {
<font color="#555555">114 </font> digit = value % 10;
<font color="#555555">115 </font> }
<font color="#555555">116 </font>
<font color="#555555">117 </font> <font color="#0000AA">protected</font> <font color="#006699">void</font> processComponentEvent(ComponentEvent event)
<font color="#555555">118 </font> {
<font color="#555555">119 </font> <font color="#0000AA">if</font> (event.getID() == ComponentEvent.COMPONENT_SHOWN) {
<font color="#555555">120 </font> requestFocus();
<font color="#555555">121 </font> }
<font color="#555555">122 </font> <font color="#006699">super</font>.processComponentEvent(event);
<font color="#555555">123 </font> }
<font color="#555555">124 </font>
<font color="#555555">125 </font> <font color="#0000AA">protected</font> <font color="#006699">void</font> processFocusEvent(FocusEvent event)
<font color="#555555">126 </font> {
<font color="#555555">127 </font> <font color="#0000AA">if</font> (event.getID() == FocusEvent.FOCUS_GAINED) {
<font color="#555555">128 </font> hasfocus = <font color="#006699">true</font>;
<font color="#555555">129 </font> repaint();
<font color="#555555">130 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (event.getID() == FocusEvent.FOCUS_LOST) {
<font color="#555555">131 </font> hasfocus = <font color="#006699">false</font>;
<font color="#555555">132 </font> repaint();
<font color="#555555">133 </font> }
<font color="#555555">134 </font> <font color="#006699">super</font>.processFocusEvent(event);
<font color="#555555">135 </font> }
<font color="#555555">136 </font>
<font color="#555555">137 </font> <font color="#0000AA">protected</font> <font color="#006699">void</font> processMouseEvent(MouseEvent event)
<font color="#555555">138 </font> {
<font color="#555555">139 </font> <font color="#0000AA">if</font> (event.getID() == MouseEvent.MOUSE_PRESSED) {
<font color="#555555">140 </font> requestFocus();
<font color="#555555">141 </font> <font color="#0000AA">if</font> (!event.isShiftDown()) {
<font color="#555555">142 </font> <font color="#0000AA">if</font> (event.isMetaDown()) {
<font color="#555555">143 </font> setValue(getValue() + 1); <font color="#00AA00">//increment by 1</font>
<font color="#555555">144 </font> } <font color="#0000AA">else</font> {
<font color="#555555">145 </font> setValue(getValue() + 9); <font color="#00AA00">//decrement by 1</font>
<font color="#555555">146 </font> }
<font color="#555555">147 </font> }
<font color="#555555">148 </font> repaint();
<font color="#555555">149 </font> }
<font color="#555555">150 </font> <font color="#006699">super</font>.processMouseEvent(event);
<font color="#555555">151 </font> }
<font color="#555555">152 </font>
<font color="#555555">153 </font> <font color="#0000AA">protected</font> <font color="#006699">void</font> processKeyEvent(KeyEvent event)
<font color="#555555">154 </font> {
<font color="#555555">155 </font> <font color="#0000AA">if</font> (event.getID() == KeyEvent.KEY_PRESSED) {
<font color="#555555">156 </font> <font color="#006699">char</font> key = event.getKeyChar();
<font color="#555555">157 </font> <font color="#0000AA">if</font> (key >= <font color="#0000FF">'0'</font> && key <= <font color="#0000FF">'9'</font>) {
<font color="#555555">158 </font> setValue(key - <font color="#0000FF">'0'</font>);
<font color="#555555">159 </font> repaint();
<font color="#555555">160 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (key == <font color="#0000FF">'+'</font>) {
<font color="#555555">161 </font> setValue(getValue() + 1); <font color="#00AA00">//increment by 1</font>
<font color="#555555">162 </font> repaint();
<font color="#555555">163 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (key == <font color="#0000FF">'-'</font>) {
<font color="#555555">164 </font> setValue(getValue() + 9); <font color="#00AA00">//decrement by 1</font>
<font color="#555555">165 </font> repaint();
<font color="#555555">166 </font> }
<font color="#555555">167 </font> }
<font color="#555555">168 </font> <font color="#006699">super</font>.processKeyEvent(event);
<font color="#555555">169 </font> }
<font color="#555555">170 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Segment7.java"><font color="#000055" size=-1>Segment7.java</font></a></td>
</tr>
</table>
<i>
Listing 33.1: Eine 7-Segment-Anzeige</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="k100219.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100220.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100222.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100224.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>
|