summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/html/k100164.html
blob: 919e062bd0e093b880e8268fd6de2a43b7e2d275 (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
<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,k100161.html;106,k100163.html;107,k100165.html;108,k100167.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="k100161.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100163.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100165.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100167.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 25 - Farben
</table>
<hr>


<!-- Section -->
<a name="sectlevel2id025003"></a>
<h2>25.3 Verwenden von Farben </h2>
<hr>
<ul>
<li><a href="k100164.html#sectlevel2id025003">25.3 Verwenden von Farben</a>
</ul>
<hr>

<p>
Um Farben bei der Ausgabe von Grafik oder Schrift zu verwenden, muss
ein geeignetes <a href="index_c.html#ixb101242"><font color=#000080><tt>Color</tt></font></a>-Objekt
beschafft und dem <a href="index_g.html#ixb101149"><font color=#000080><tt>Graphics</tt></font></a>-Objekt
mit Hilfe der Methode <a name="ixa101647"><a href="index_s.html#ixb101259"><font color=#000080><tt>setColor</tt></font></a></a>
zugewiesen werden. Die Ausgaben erfolgen dann so lange in der neuen
Farbe, bis durch Aufruf von <a href="index_s.html#ixb101259"><font color=#000080><tt>setColor</tt></font></a>
eine andere Farbe festgelegt wird. Mit Hilfe der Methode <a name="ixa101648"><a href="index_g.html#ixb101260"><font color=#000080><tt>getColor</tt></font></a></a>
kann die aktuelle Farbe ermittelt 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 setColor(Color c)

public Color getColor()
</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 zeigt die Verwendung von Farben und ihre Zusammensetzung
aus den drei Grundfarben Rot, Gr&uuml;n und Blau. Es stellt das Prinzip
der additiven Farbmischung mit Hilfe dreier &uuml;berlappender Kreise
dar: 
<a name="listingid025001"></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">/* Listing2501.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> Listing2501
<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>    Listing2501 wnd = <font color="#0000AA">new</font> Listing2501();
<font color="#555555">012 </font>  }
<font color="#555555">013 </font>
<font color="#555555">014 </font>  <font color="#0000AA">public</font> Listing2501()
<font color="#555555">015 </font>  {
<font color="#555555">016 </font>    <font color="#006699">super</font>(<font color="#0000FF">"Der Farbenkreis"</font>);
<font color="#555555">017 </font>    addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">018 </font>    setSize(300,200);
<font color="#555555">019 </font>    setVisible(<font color="#006699">true</font>);
<font color="#555555">020 </font>  }
<font color="#555555">021 </font>
<font color="#555555">022 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">023 </font>  {
<font color="#555555">024 </font>    <font color="#006699">int</font> top  = getInsets().top;
<font color="#555555">025 </font>    <font color="#006699">int</font> left = getInsets().left;
<font color="#555555">026 </font>    <font color="#006699">int</font> maxX = getSize().width-left-getInsets().right;
<font color="#555555">027 </font>    <font color="#006699">int</font> maxY = getSize().height-top-getInsets().bottom;
<font color="#555555">028 </font>    Color col;
<font color="#555555">029 </font>    <font color="#006699">int</font>[] arx   = {130,160,190};
<font color="#555555">030 </font>    <font color="#006699">int</font>[] ary   = {60,110,60};
<font color="#555555">031 </font>    <font color="#006699">int</font>[] arr   = {50,50,50};
<font color="#555555">032 </font>    <font color="#006699">int</font>[] arcol = {0,0,0};
<font color="#555555">033 </font>    <font color="#006699">boolean</font> paintit;
<font color="#555555">034 </font>    <font color="#006699">int</font> dx, dy;
<font color="#555555">035 </font>
<font color="#555555">036 </font>    <font color="#0000AA">for</font> (<font color="#006699">int</font> y = 0; y &lt; maxY; ++y) {
<font color="#555555">037 </font>      <font color="#0000AA">for</font> (<font color="#006699">int</font> x = 0; x &lt; maxX; ++x) {
<font color="#555555">038 </font>        paintit = <font color="#006699">false</font>;
<font color="#555555">039 </font>        <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i &lt; arcol.length; ++i) {
<font color="#555555">040 </font>          dx = x - arx[i];
<font color="#555555">041 </font>          dy = y - ary[i];
<font color="#555555">042 </font>          arcol[i] = 0;
<font color="#555555">043 </font>          <font color="#0000AA">if</font> ((dx*dx+dy*dy) &lt;= arr[i]*arr[i]) {
<font color="#555555">044 </font>            arcol[i] = 255;
<font color="#555555">045 </font>            paintit = <font color="#006699">true</font>;
<font color="#555555">046 </font>          }
<font color="#555555">047 </font>        }
<font color="#555555">048 </font>        <font color="#0000AA">if</font> (paintit) {
<font color="#555555">049 </font>          col = <font color="#0000AA">new</font> Color(arcol[0],arcol[1],arcol[2]);
<font color="#555555">050 </font>          g.setColor(col);
<font color="#555555">051 </font>          g.drawLine(x+left,y+top,x+left+1,y+top+1);
<font color="#555555">052 </font>        }
<font color="#555555">053 </font>      }
<font color="#555555">054 </font>    }
<font color="#555555">055 </font>  }
<font color="#555555">056 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing2501.java"><font color="#000055" size=-1>Listing2501.java</font></a></td>
</tr>
</table>
<i>
Listing 25.1: Darstellung des Farbenkreises</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 Programm arbeitet in der Weise, dass f&uuml;r jeden einzelnen
Punkt der Zeichenfl&auml;che berechnet wird, ob dieser in einem der
drei Kreise liegt. Ist dies der Fall, so wird die zugeh&ouml;rige
Farbkomponente auf 255 gesetzt, andernfalls auf 0. Die Bestimmung
der Kreiszugeh&ouml;rigkeit erfolgt mit Hilfe des Satzes von Pythagoras,
nach dem ein Punkt genau dann zu einem Kreis geh&ouml;rt, wenn die
Summe der Quadrate der Abst&auml;nde vom <font color="#000077"><tt>x</tt></font>-
und <font color="#000077"><tt>y</tt></font>-Wert zum Mittelpunkt kleiner
gleich dem Quadrat des Radius ist. Die drei Mittelpunkte werden in
unserem Beispiel in den Arrays <font color="#000077"><tt>arx</tt></font>
und <font color="#000077"><tt>ary</tt></font>, die Radien der Kreise
in <font color="#000077"><tt>arr</tt></font> gespeichert. Die boolesche
Variable <font color="#000077"><tt>paintit</tt></font> zeigt an, ob
der Punkt in wenigstens einem der drei Kreise liegt und daher &uuml;berhaupt
eine Ausgabe erforderlich ist.</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">&nbsp;Hinweis&nbsp;</font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
<a name="imageid025001"></a>
<img src="images/FarbenKreis.gif">
<p>

<p><i>
Abbildung 25.1: Der Farbenkreis</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="k100161.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100163.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100165.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100167.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>