@@ -25,7 +25,7 @@ public class KeyboardCanvas extends JComponent implements KeyboardDispatcher.OnK
25
25
private final static double sHalf = s / 2.0 ; // space between buttons
26
26
private final static int arc = 15 ; // arc radius
27
27
private final static int margin = 10 ;
28
- private final static int rshiftw = 3 * bw - 3 * s - margin + (int )sHalf ; // right shift width
28
+ private final static int rshiftw = 3 * bw - 3 * s - margin + (int ) sHalf ; // right shift width
29
29
30
30
public final static int KEYBOARD_WIDTH = 13 * (bw + s ) + bsw + 10 + 10 ;
31
31
public final static int KEYBOARD_HEIGHT = 5 * (bh + s ) + 2 * margin - s ;
@@ -104,12 +104,14 @@ public class KeyboardCanvas extends JComponent implements KeyboardDispatcher.OnK
104
104
private final Color usableButtonColor ;
105
105
private final Color outlineColor ;
106
106
private final Color brightColor ;
107
+ private int alpha ;
107
108
108
109
private boolean symShift = false ;
109
110
private boolean shift = false ;
110
111
111
112
public KeyboardCanvas (int alpha ) {
112
113
setDoubleBuffered (true );
114
+ this .alpha = alpha ;
113
115
this .usableButtonColor = new Color (
114
116
Color .LIGHT_GRAY .getRed (),
115
117
Color .LIGHT_GRAY .getGreen (),
@@ -131,6 +133,14 @@ public boolean onKeyEvent(KeyEvent e) {
131
133
return true ;
132
134
}
133
135
136
+ public int getAlpha () {
137
+ return alpha ;
138
+ }
139
+
140
+ public void setAlpha (int alpha ) {
141
+ this .alpha = alpha ;
142
+ }
143
+
134
144
public void paint (Graphics g ) {
135
145
Graphics2D g2d = (Graphics2D ) g ;
136
146
// we must set RenderingHints before any drawing
@@ -142,7 +152,7 @@ public void paint(Graphics g) {
142
152
143
153
g2d .setFont (new Font ("SansSerif" , Font .PLAIN , 11 ));
144
154
g2d .setStroke (outlineStroke );
145
- g2d .setColor (brightColor );
155
+ g2d .setColor (adjustAlpha ( brightColor ) );
146
156
g2d .translate (X_SHIFT_L , 0 );
147
157
148
158
for (int i = 0 ; i < KEY_MAP .length ; i ++) {
@@ -161,26 +171,28 @@ public void paint(Graphics g) {
161
171
int sw = g2d .getFontMetrics ().stringWidth (text );
162
172
163
173
g2d .translate (KEY_MAP [i ][0 ] - sw / 2.0 , KEY_MAP [i ][1 ]);
164
- g2d .setColor (outlineColor );
174
+ g2d .setColor (adjustAlpha ( outlineColor ) );
165
175
g2d .fill (textShape );
166
176
g2d .translate (sw / 2.0 , 0 );
167
177
}
168
178
}
169
179
170
180
private void drawKeyboard (Graphics2D g ) {
171
181
BasicStroke stroke = new BasicStroke (2.0f );
182
+ Color adjUsableButtonColor = adjustAlpha (usableButtonColor );
183
+ Color adjOutlineColor = adjustAlpha (outlineColor );
172
184
173
185
// keyboard shape
174
186
g .setStroke (stroke );
175
- g .setColor (outlineColor );
187
+ g .setColor (adjOutlineColor );
176
188
g .drawRoundRect (X_SHIFT , -STROKE_WIDTH , KEYBOARD_WIDTH , KEYBOARD_HEIGHT , arc , arc );
177
189
178
190
// top row
179
191
for (int i = 0 ; i < 13 ; i ++) {
180
192
if (i >= 1 && i <= 10 ) {
181
- g .setColor (usableButtonColor );
193
+ g .setColor (adjUsableButtonColor );
182
194
g .fillRoundRect (X_SHIFT_L + i * (bw + s ), Y_SHIFT_T , bw , bh , arc , arc );
183
- g .setColor (outlineColor );
195
+ g .setColor (adjOutlineColor );
184
196
}
185
197
g .drawRoundRect (X_SHIFT_L + i * (bw + s ), Y_SHIFT_T , bw , bh , arc , arc );
186
198
}
@@ -193,9 +205,9 @@ private void drawKeyboard(Graphics2D g) {
193
205
g .drawRoundRect (X_SHIFT_L , y1 , tabw , bh , arc , arc );
194
206
for (int i = 0 ; i < 12 ; i ++) {
195
207
if (i < 10 ) {
196
- g .setColor (usableButtonColor );
208
+ g .setColor (adjUsableButtonColor );
197
209
g .fillRoundRect (X_SHIFT_L + i * (bw + s ) + tabw + s , y1 , bw , bh , arc , arc );
198
- g .setColor (outlineColor );
210
+ g .setColor (adjOutlineColor );
199
211
}
200
212
g .drawRoundRect (X_SHIFT_L + i * (bw + s ) + tabw + s , y1 , bw , bh , arc , arc );
201
213
}
@@ -209,63 +221,70 @@ private void drawKeyboard(Graphics2D g) {
209
221
6
210
222
);
211
223
212
- g .setColor (usableButtonColor );
224
+ g .setColor (adjUsableButtonColor );
213
225
g .fillPolygon (enterPolygon );
214
- g .setColor (outlineColor );
226
+ g .setColor (adjOutlineColor );
215
227
g .drawPolygon (enterPolygon );
216
228
217
229
// caps lock
218
230
int y2 = Y_SHIFT_T + (bh + s ) * 2 ;
219
231
g .drawRoundRect (X_SHIFT_L , y2 , bsw , bh , arc , arc );
220
232
for (int i = 0 ; i < 12 ; i ++) {
221
233
if (i < 9 ) {
222
- g .setColor (usableButtonColor );
234
+ g .setColor (adjUsableButtonColor );
223
235
g .fillRoundRect (X_SHIFT_L + i * (bw + s ) + bsw + s , y2 , bw , bh , arc , arc );
224
- g .setColor (outlineColor );
236
+ g .setColor (adjOutlineColor );
225
237
}
226
238
g .drawRoundRect (X_SHIFT_L + i * (bw + s ) + bsw + s , y2 , bw , bh , arc , arc );
227
239
}
228
240
229
241
// l shift
230
242
int y3 = Y_SHIFT_T + (bh + s ) * 3 ;
231
243
232
- g .setColor (usableButtonColor );
244
+ g .setColor (adjUsableButtonColor );
233
245
g .fillRoundRect (X_SHIFT_L , y3 , lshiftw , bh , arc , arc );
234
- g .setColor (outlineColor );
246
+ g .setColor (adjOutlineColor );
235
247
g .drawRoundRect (X_SHIFT_L , y3 , lshiftw , bh , arc , arc );
236
248
for (int i = 0 ; i < 11 ; i ++) {
237
249
if (i >= 1 && i < 8 ) {
238
- g .setColor (usableButtonColor );
250
+ g .setColor (adjUsableButtonColor );
239
251
g .fillRoundRect (X_SHIFT_L + i * (bw + s ) + lshiftw + s , y3 , bw , bh , arc , arc );
240
- g .setColor (outlineColor );
252
+ g .setColor (adjOutlineColor );
241
253
}
242
254
g .drawRoundRect (X_SHIFT_L + i * (bw + s ) + lshiftw + s , y3 , bw , bh , arc , arc );
243
255
}
244
- g .setColor (usableButtonColor );
256
+ g .setColor (adjUsableButtonColor );
245
257
g .fillRoundRect (X_SHIFT_L + 11 * (bw + s ) + lshiftw + s , y3 , rshiftw , bh , arc , arc );
246
- g .setColor (outlineColor );
258
+ g .setColor (adjOutlineColor );
247
259
g .drawRoundRect (X_SHIFT_L + 11 * (bw + s ) + lshiftw + s , y3 , rshiftw , bh , arc , arc );
248
260
249
261
// l ctrl
250
262
int y4 = Y_SHIFT_T + (bh + s ) * 4 ;
251
- g .setColor (usableButtonColor );
263
+ g .setColor (adjUsableButtonColor );
252
264
g .fillRoundRect (X_SHIFT_L , y4 , tabw , bh , arc , arc );
253
- g .setColor (outlineColor );
265
+ g .setColor (adjOutlineColor );
254
266
g .drawRoundRect (X_SHIFT_L , y4 , tabw , bh , arc , arc );
255
267
g .drawRoundRect (X_SHIFT_L + tabw + s , y4 , bw , bh , arc , arc );
256
268
g .drawRoundRect (X_SHIFT_L + tabw + bw + 2 * s , y4 , tabw , bh , arc , arc );
257
269
258
- g .setColor (usableButtonColor );
270
+ g .setColor (adjUsableButtonColor );
259
271
g .fillRoundRect (X_SHIFT_L + 2 * (tabw + s ) + bw + 2 * s , y4 , brakew , bh , arc , arc );
260
- g .setColor (outlineColor );
272
+ g .setColor (adjOutlineColor );
261
273
g .drawRoundRect (X_SHIFT_L + 2 * (tabw + s ) + bw + 2 * s , y4 , brakew , bh , arc , arc );
262
274
g .drawRoundRect (X_SHIFT_L + 2 * (tabw + s ) + bw + 4 * s + brakew , y4 , tabw , bh , arc , arc );
263
275
264
- g .setColor (usableButtonColor );
276
+ g .setColor (adjUsableButtonColor );
265
277
g .fillRoundRect (X_SHIFT_L + 3 * (tabw + s ) + bw + 4 * s + brakew , y4 , bw , bh , arc , arc );
266
- g .setColor (outlineColor );
278
+ g .setColor (adjOutlineColor );
267
279
g .drawRoundRect (X_SHIFT_L + 3 * (tabw + s ) + bw + 4 * s + brakew , y4 , bw , bh , arc , arc ); // RCTRL
268
280
g .drawRoundRect (X_SHIFT_L + 3 * (tabw + s ) + 2 * bw + 5 * s + brakew , y4 , bw , bh , arc , arc );
269
281
g .drawRoundRect (X_SHIFT_L + 3 * (tabw + s ) + 3 * bw + 6 * s + brakew , y4 , tabw , bh , arc , arc );
270
282
}
283
+
284
+ private Color adjustAlpha (Color color ) {
285
+ int r = color .getRed ();
286
+ int g = color .getGreen ();
287
+ int b = color .getBlue ();
288
+ return new Color (r , g , b , this .alpha );
289
+ }
271
290
}
0 commit comments