15
15
* Host-ZX Keyboard mapping visual representation.
16
16
*/
17
17
public class KeyboardCanvas extends JComponent implements KeyboardDispatcher .OnKeyListener {
18
- private final static int bw = 45 ; // button width
18
+ private final static int bw = 42 ; // button width
19
19
private final static int bh = 33 ; // button height
20
20
private final static int bsw = 70 ; // backspace width
21
21
private final static int tabw = 60 ; // tab width
22
22
private final static int lshiftw = 55 ; // left shift width
23
- private final static int brakew = 300 ; // break width
23
+ private final static int brakew = 270 ; // break width
24
24
private final static int s = 5 ; // space between buttons
25
+ private final static double sHalf = s / 2.0 ; // space between buttons
25
26
private final static int arc = 15 ; // arc radius
26
27
private final static int margin = 10 ;
27
- private final static int rshiftw = 3 * bw - 3 * s - margin ; // right shift width
28
+ private final static int rshiftw = 3 * bw - 3 * s - margin + ( int ) sHalf ; // right shift width
28
29
29
- public final static int KEYBOARD_WIDTH = 13 * (bw + s ) + 10 + bsw + 10 ;
30
- public final static int KEYBOARD_HEIGHT = 5 * (bh + s ) + 2 * margin ;
30
+ public final static int KEYBOARD_WIDTH = 13 * (bw + s ) + bsw + 10 + 10 ;
31
+ public final static int KEYBOARD_HEIGHT = 5 * (bh + s ) + 2 * margin - s ;
31
32
32
33
private final static int X_SHIFT = (int ) ((ZOOM * SCREEN_IMAGE_WIDTH + 2 * MARGIN - KEYBOARD_WIDTH ) / 2.0 );
33
34
private final static int X_SHIFT_L = X_SHIFT + margin ;
34
35
private final static int Y_SHIFT_T = margin ;
35
-
36
- private final static Color USABLE_BUTTON_COLOR = Color .LIGHT_GRAY ;
37
-
36
+ private final static int STROKE_WIDTH = 3 ;
38
37
39
38
private final static double [][] KEY_MAP = new double [][]{
40
39
new double []{bw + s + bw / 2.0 , bh }, // 1
@@ -47,7 +46,7 @@ public class KeyboardCanvas extends JComponent implements KeyboardDispatcher.OnK
47
46
new double []{bw + s , 0 }, // 8
48
47
new double []{bw + s , 0 }, // 9
49
48
new double []{bw + s , 0 }, // 0,
50
- new double []{-(11 * bw + s ) + tabw + s , bh + s }, // Q
49
+ new double []{-(11 * bw + s ) + tabw + sHalf , bh + s }, // Q
51
50
new double []{bw + s , 0 }, // W
52
51
new double []{bw + s , 0 }, // E
53
52
new double []{bw + s , 0 }, // R
@@ -57,7 +56,7 @@ public class KeyboardCanvas extends JComponent implements KeyboardDispatcher.OnK
57
56
new double []{bw + s , 0 }, // I
58
57
new double []{bw + s , 0 }, // O
59
58
new double []{bw + s , 0 }, // P
60
- new double []{-(10 * bw + s ) - tabw + bsw + s , bh + s }, // A
59
+ new double []{-(10 * bw + s ) - tabw + bsw + sHalf , bh + s }, // A
61
60
new double []{bw + s , 0 }, // S
62
61
new double []{bw + s , 0 }, // D
63
62
new double []{bw + s , 0 }, // F
@@ -101,25 +100,35 @@ public class KeyboardCanvas extends JComponent implements KeyboardDispatcher.OnK
101
100
"SHIFT" , ":" , "£" , "?" , "/" , "*" , "," , "." , "SHIFT" , "SYM" , "SYM"
102
101
};
103
102
104
- private final BasicStroke outlineStroke = new BasicStroke (3.0f );
103
+ private final BasicStroke outlineStroke = new BasicStroke (STROKE_WIDTH );
104
+ private final Color usableButtonColor ;
105
+ private final Color outlineColor ;
106
+ private final Color brightColor ;
105
107
106
108
private boolean symShift = false ;
107
109
private boolean shift = false ;
108
110
109
- public KeyboardCanvas () {
111
+ public KeyboardCanvas (int alpha ) {
110
112
setDoubleBuffered (true );
113
+ this .usableButtonColor = new Color (
114
+ Color .LIGHT_GRAY .getRed (),
115
+ Color .LIGHT_GRAY .getGreen (),
116
+ Color .LIGHT_GRAY .getBlue (), alpha );
117
+ this .outlineColor = new Color (0 , 0 , 0 , alpha );
118
+ this .brightColor = new Color (255 , 255 , 255 , alpha );
111
119
}
112
120
113
121
@ Override
114
- public void onKeyEvent (KeyEvent e ) {
122
+ public boolean onKeyEvent (KeyEvent e ) {
115
123
boolean pressed = e .getID () == KEY_PRESSED ;
116
124
if (!pressed && e .getID () != KEY_RELEASED ) {
117
- return ;
125
+ return false ;
118
126
}
119
127
120
128
symShift = (e .getModifiersEx () & (KeyEvent .CTRL_DOWN_MASK | KeyEvent .ALT_DOWN_MASK )) != 0 ;
121
129
shift = (e .getModifiersEx () & (KeyEvent .SHIFT_DOWN_MASK )) != 0 ;
122
130
repaint ();
131
+ return true ;
123
132
}
124
133
125
134
public void paint (Graphics g ) {
@@ -133,7 +142,7 @@ public void paint(Graphics g) {
133
142
134
143
g2d .setFont (new Font ("SansSerif" , Font .PLAIN , 11 ));
135
144
g2d .setStroke (outlineStroke );
136
- g2d .setColor (Color . WHITE );
145
+ g2d .setColor (brightColor );
137
146
g2d .translate (X_SHIFT_L , 0 );
138
147
139
148
for (int i = 0 ; i < KEY_MAP .length ; i ++) {
@@ -152,7 +161,7 @@ public void paint(Graphics g) {
152
161
int sw = g2d .getFontMetrics ().stringWidth (text );
153
162
154
163
g2d .translate (KEY_MAP [i ][0 ] - sw / 2.0 , KEY_MAP [i ][1 ]);
155
- g2d .setColor (Color . BLACK );
164
+ g2d .setColor (outlineColor );
156
165
g2d .fill (textShape );
157
166
g2d .translate (sw / 2.0 , 0 );
158
167
}
@@ -163,14 +172,15 @@ private void drawKeyboard(Graphics2D g) {
163
172
164
173
// keyboard shape
165
174
g .setStroke (stroke );
166
- g .drawRoundRect (X_SHIFT , 0 , KEYBOARD_WIDTH , KEYBOARD_HEIGHT , arc , arc );
175
+ g .setColor (outlineColor );
176
+ g .drawRoundRect (X_SHIFT , -STROKE_WIDTH , KEYBOARD_WIDTH , KEYBOARD_HEIGHT , arc , arc );
167
177
168
178
// top row
169
179
for (int i = 0 ; i < 13 ; i ++) {
170
180
if (i >= 1 && i <= 10 ) {
171
- g .setColor (USABLE_BUTTON_COLOR );
181
+ g .setColor (usableButtonColor );
172
182
g .fillRoundRect (X_SHIFT_L + i * (bw + s ), Y_SHIFT_T , bw , bh , arc , arc );
173
- g .setColor (Color . BLACK );
183
+ g .setColor (outlineColor );
174
184
}
175
185
g .drawRoundRect (X_SHIFT_L + i * (bw + s ), Y_SHIFT_T , bw , bh , arc , arc );
176
186
}
@@ -183,9 +193,9 @@ private void drawKeyboard(Graphics2D g) {
183
193
g .drawRoundRect (X_SHIFT_L , y1 , tabw , bh , arc , arc );
184
194
for (int i = 0 ; i < 12 ; i ++) {
185
195
if (i < 10 ) {
186
- g .setColor (USABLE_BUTTON_COLOR );
196
+ g .setColor (usableButtonColor );
187
197
g .fillRoundRect (X_SHIFT_L + i * (bw + s ) + tabw + s , y1 , bw , bh , arc , arc );
188
- g .setColor (Color . BLACK );
198
+ g .setColor (outlineColor );
189
199
}
190
200
g .drawRoundRect (X_SHIFT_L + i * (bw + s ) + tabw + s , y1 , bw , bh , arc , arc );
191
201
}
@@ -194,66 +204,66 @@ private void drawKeyboard(Graphics2D g) {
194
204
int x0 = X_SHIFT_L + 12 * (bw + s ) + tabw + s ;
195
205
int y0 = Y_SHIFT_T + bh + s ;
196
206
Polygon enterPolygon = new Polygon (
197
- new int []{x0 , x0 + tabw - s , x0 + tabw - s , x0 + 2 * s , x0 + 2 * s , x0 },
207
+ new int []{x0 , x0 + tabw - 2 * s , x0 + tabw - 2 * s , x0 + 2 * s , x0 + 2 * s , x0 },
198
208
new int []{y0 , y0 , y0 + 2 * bh + s , y0 + 2 * bh + s , y0 + bh , y0 + bh },
199
209
6
200
210
);
201
211
202
- g .setColor (USABLE_BUTTON_COLOR );
212
+ g .setColor (usableButtonColor );
203
213
g .fillPolygon (enterPolygon );
204
- g .setColor (Color . BLACK );
214
+ g .setColor (outlineColor );
205
215
g .drawPolygon (enterPolygon );
206
216
207
217
// caps lock
208
218
int y2 = Y_SHIFT_T + (bh + s ) * 2 ;
209
219
g .drawRoundRect (X_SHIFT_L , y2 , bsw , bh , arc , arc );
210
220
for (int i = 0 ; i < 12 ; i ++) {
211
221
if (i < 9 ) {
212
- g .setColor (USABLE_BUTTON_COLOR );
222
+ g .setColor (usableButtonColor );
213
223
g .fillRoundRect (X_SHIFT_L + i * (bw + s ) + bsw + s , y2 , bw , bh , arc , arc );
214
- g .setColor (Color . BLACK );
224
+ g .setColor (outlineColor );
215
225
}
216
226
g .drawRoundRect (X_SHIFT_L + i * (bw + s ) + bsw + s , y2 , bw , bh , arc , arc );
217
227
}
218
228
219
229
// l shift
220
230
int y3 = Y_SHIFT_T + (bh + s ) * 3 ;
221
231
222
- g .setColor (USABLE_BUTTON_COLOR );
232
+ g .setColor (usableButtonColor );
223
233
g .fillRoundRect (X_SHIFT_L , y3 , lshiftw , bh , arc , arc );
224
- g .setColor (Color . BLACK );
234
+ g .setColor (outlineColor );
225
235
g .drawRoundRect (X_SHIFT_L , y3 , lshiftw , bh , arc , arc );
226
236
for (int i = 0 ; i < 11 ; i ++) {
227
237
if (i >= 1 && i < 8 ) {
228
- g .setColor (USABLE_BUTTON_COLOR );
238
+ g .setColor (usableButtonColor );
229
239
g .fillRoundRect (X_SHIFT_L + i * (bw + s ) + lshiftw + s , y3 , bw , bh , arc , arc );
230
- g .setColor (Color . BLACK );
240
+ g .setColor (outlineColor );
231
241
}
232
242
g .drawRoundRect (X_SHIFT_L + i * (bw + s ) + lshiftw + s , y3 , bw , bh , arc , arc );
233
243
}
234
- g .setColor (USABLE_BUTTON_COLOR );
244
+ g .setColor (usableButtonColor );
235
245
g .fillRoundRect (X_SHIFT_L + 11 * (bw + s ) + lshiftw + s , y3 , rshiftw , bh , arc , arc );
236
- g .setColor (Color . BLACK );
246
+ g .setColor (outlineColor );
237
247
g .drawRoundRect (X_SHIFT_L + 11 * (bw + s ) + lshiftw + s , y3 , rshiftw , bh , arc , arc );
238
248
239
249
// l ctrl
240
250
int y4 = Y_SHIFT_T + (bh + s ) * 4 ;
241
- g .setColor (USABLE_BUTTON_COLOR );
251
+ g .setColor (usableButtonColor );
242
252
g .fillRoundRect (X_SHIFT_L , y4 , tabw , bh , arc , arc );
243
- g .setColor (Color . BLACK );
253
+ g .setColor (outlineColor );
244
254
g .drawRoundRect (X_SHIFT_L , y4 , tabw , bh , arc , arc );
245
255
g .drawRoundRect (X_SHIFT_L + tabw + s , y4 , bw , bh , arc , arc );
246
256
g .drawRoundRect (X_SHIFT_L + tabw + bw + 2 * s , y4 , tabw , bh , arc , arc );
247
257
248
- g .setColor (USABLE_BUTTON_COLOR );
258
+ g .setColor (usableButtonColor );
249
259
g .fillRoundRect (X_SHIFT_L + 2 * (tabw + s ) + bw + 2 * s , y4 , brakew , bh , arc , arc );
250
- g .setColor (Color . BLACK );
260
+ g .setColor (outlineColor );
251
261
g .drawRoundRect (X_SHIFT_L + 2 * (tabw + s ) + bw + 2 * s , y4 , brakew , bh , arc , arc );
252
262
g .drawRoundRect (X_SHIFT_L + 2 * (tabw + s ) + bw + 4 * s + brakew , y4 , tabw , bh , arc , arc );
253
263
254
- g .setColor (USABLE_BUTTON_COLOR );
264
+ g .setColor (usableButtonColor );
255
265
g .fillRoundRect (X_SHIFT_L + 3 * (tabw + s ) + bw + 4 * s + brakew , y4 , bw , bh , arc , arc );
256
- g .setColor (Color . BLACK );
266
+ g .setColor (outlineColor );
257
267
g .drawRoundRect (X_SHIFT_L + 3 * (tabw + s ) + bw + 4 * s + brakew , y4 , bw , bh , arc , arc ); // RCTRL
258
268
g .drawRoundRect (X_SHIFT_L + 3 * (tabw + s ) + 2 * bw + 5 * s + brakew , y4 , bw , bh , arc , arc );
259
269
g .drawRoundRect (X_SHIFT_L + 3 * (tabw + s ) + 3 * bw + 6 * s + brakew , y4 , tabw , bh , arc , arc );
0 commit comments