| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
package uk.co.javagear; |
| 22 |
|
|
| 23 |
|
import java.awt.AlphaComposite; |
| 24 |
|
import java.awt.Color; |
| 25 |
|
import java.awt.Cursor; |
| 26 |
|
import java.awt.Dimension; |
| 27 |
|
import java.awt.Font; |
| 28 |
|
import java.awt.FontMetrics; |
| 29 |
|
import java.awt.Graphics; |
| 30 |
|
import java.awt.Graphics2D; |
| 31 |
|
import java.awt.Image; |
| 32 |
|
import java.awt.MediaTracker; |
| 33 |
|
import java.awt.Toolkit; |
| 34 |
|
import java.awt.event.KeyEvent; |
| 35 |
|
import java.awt.event.KeyListener; |
| 36 |
|
import java.awt.event.MouseEvent; |
| 37 |
|
import java.awt.event.MouseListener; |
| 38 |
|
import java.awt.event.MouseMotionListener; |
| 39 |
|
import java.awt.geom.RoundRectangle2D; |
| 40 |
|
import java.net.URL; |
| 41 |
|
import javax.swing.JPanel; |
| 42 |
|
import org.apache.log4j.Logger; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
public final class KeyConfig extends JPanel |
| 52 |
|
implements MouseMotionListener, MouseListener, KeyListener { |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
private Graphics2D g2; |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
private Image gameGear; |
| 63 |
|
|
| 64 |
|
private Font f; |
| 65 |
|
|
| 66 |
|
private FontMetrics fm; |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
0 |
private int x = 0; |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
0 |
private int y = 0; |
| 77 |
|
|
| 78 |
|
|
| 79 |
0 |
private RoundRectangle2D button1 = new RoundRectangle2D.Double(295, 78, 20, 20, 30, 30); |
| 80 |
0 |
private RoundRectangle2D button2 = new RoundRectangle2D.Double(320, 58, 20, 20, 30, 30); |
| 81 |
0 |
private RoundRectangle2D buttonS = new RoundRectangle2D.Double(302, 32, 11, 18, 30, 30); |
| 82 |
0 |
private RoundRectangle2D buttonL = new RoundRectangle2D.Double(27, 72, 10, 13, 0, 0); |
| 83 |
0 |
private RoundRectangle2D buttonR = new RoundRectangle2D.Double(56, 72, 10, 14, 0, 0); |
| 84 |
0 |
private RoundRectangle2D buttonU = new RoundRectangle2D.Double(40, 60, 12, 12, 0, 0); |
| 85 |
0 |
private RoundRectangle2D buttonD = new RoundRectangle2D.Double(41, 87, 11, 12, 0, 0); |
| 86 |
0 |
private RoundRectangle2D buttonCurrent = null; |
| 87 |
|
|
| 88 |
|
|
| 89 |
0 |
private int up = KeyEvent.VK_UP; |
| 90 |
0 |
private int down = KeyEvent.VK_DOWN; |
| 91 |
0 |
private int left = KeyEvent.VK_LEFT; |
| 92 |
0 |
private int right = KeyEvent.VK_RIGHT; |
| 93 |
0 |
private int fire1 = KeyEvent.VK_Z; |
| 94 |
0 |
private int fire2 = KeyEvent.VK_X; |
| 95 |
0 |
private int start = KeyEvent.VK_ENTER; |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
0 |
private boolean mousePressed = false; |
| 101 |
|
|
| 102 |
0 |
public KeyConfig() { |
| 103 |
0 |
f = new Font("SansSerif", Font.PLAIN, 6); |
| 104 |
|
|
| 105 |
|
|
| 106 |
0 |
URL imageURL = KeyFrame.class.getResource("/images/keyconfig.jpg"); |
| 107 |
0 |
gameGear = Toolkit.getDefaultToolkit().getImage(imageURL); |
| 108 |
0 |
MediaTracker tracker = new MediaTracker(this); |
| 109 |
0 |
tracker.addImage(gameGear, 0); |
| 110 |
|
try { |
| 111 |
0 |
tracker.waitForID(0); |
| 112 |
0 |
} catch (InterruptedException ie) { |
| 113 |
0 |
Logger.getLogger(this.getClass()).error("The thread was interrupted.", ie); |
| 114 |
0 |
} |
| 115 |
|
|
| 116 |
0 |
addKeyListener(this); |
| 117 |
0 |
addMouseMotionListener(this); |
| 118 |
0 |
addMouseListener(this); |
| 119 |
0 |
} |
| 120 |
|
|
| 121 |
|
public void setKeys(int [] newKeys) { |
| 122 |
0 |
up = newKeys[0]; |
| 123 |
0 |
down = newKeys[1]; |
| 124 |
0 |
left = newKeys[2]; |
| 125 |
0 |
right = newKeys[3]; |
| 126 |
0 |
fire1 = newKeys[4]; |
| 127 |
0 |
fire2 = newKeys[5]; |
| 128 |
0 |
start = newKeys[6]; |
| 129 |
0 |
} |
| 130 |
|
|
| 131 |
|
public int[] getKeys() { |
| 132 |
0 |
int[] keys = {up, down, left, right, fire1, fire2, start}; |
| 133 |
0 |
return keys; |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
public void mouseMoved(MouseEvent evt) { |
| 137 |
0 |
x = evt.getX(); |
| 138 |
0 |
y = evt.getY(); |
| 139 |
|
|
| 140 |
0 |
if (buttonCurrent != null && !overButton(buttonCurrent)) { |
| 141 |
0 |
refresh(); |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
0 |
if (!mousePressed) { |
| 146 |
0 |
if (overButton(button1) |
| 147 |
|
|| overButton(button2) |
| 148 |
|
|| overButton(buttonS) |
| 149 |
|
|| overButton(buttonL) |
| 150 |
|
|| overButton(buttonR) |
| 151 |
|
|| overButton(buttonU) |
| 152 |
|
|| overButton(buttonD)) { |
| 153 |
0 |
refresh(); |
| 154 |
|
} |
| 155 |
|
} |
| 156 |
|
|
| 157 |
0 |
if (buttonCurrent != null) { |
| 158 |
0 |
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| 159 |
|
} else { |
| 160 |
0 |
setCursor(Cursor.getDefaultCursor()); |
| 161 |
|
} |
| 162 |
0 |
} |
| 163 |
|
|
| 164 |
|
public void mousePressed(MouseEvent evt) { |
| 165 |
0 |
if (buttonCurrent != null) { |
| 166 |
0 |
mousePressed = true; |
| 167 |
|
} else { |
| 168 |
0 |
mousePressed = false; |
| 169 |
|
} |
| 170 |
0 |
requestFocus(); |
| 171 |
0 |
refresh(); |
| 172 |
0 |
} |
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
public void mouseDragged(MouseEvent evt) { |
| 180 |
0 |
} |
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
public void mouseClicked(MouseEvent evt) { |
| 188 |
0 |
} |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
public void mouseReleased(MouseEvent evt) { |
| 196 |
0 |
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
public void mouseEntered(MouseEvent evt) { |
| 204 |
0 |
} |
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
public void mouseExited(MouseEvent evt) { |
| 212 |
0 |
} |
| 213 |
|
|
| 214 |
|
|
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
public void keyTyped(KeyEvent evt) { |
| 220 |
0 |
} |
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
public void keyReleased(KeyEvent evt) { |
| 228 |
0 |
} |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
public void keyPressed(KeyEvent evt) { |
| 236 |
|
|
| 237 |
0 |
if (mousePressed) { |
| 238 |
0 |
if (buttonCurrent.equals(button1)) { |
| 239 |
0 |
fire1 = evt.getKeyCode(); |
| 240 |
0 |
} else if (buttonCurrent.equals(button2)) { |
| 241 |
0 |
fire2 = evt.getKeyCode(); |
| 242 |
0 |
} else if (buttonCurrent.equals(buttonS)) { |
| 243 |
0 |
start = evt.getKeyCode(); |
| 244 |
0 |
} else if (buttonCurrent.equals(buttonU)) { |
| 245 |
0 |
up = evt.getKeyCode(); |
| 246 |
0 |
} else if (buttonCurrent.equals(buttonD)) { |
| 247 |
0 |
down = evt.getKeyCode(); |
| 248 |
0 |
} else if (buttonCurrent.equals(buttonL)) { |
| 249 |
0 |
left = evt.getKeyCode(); |
| 250 |
0 |
} else if (buttonCurrent.equals(buttonR)) { |
| 251 |
0 |
right = evt.getKeyCode(); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
0 |
mousePressed = false; |
| 255 |
|
} |
| 256 |
0 |
refresh(); |
| 257 |
0 |
} |
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
|
| 262 |
|
public boolean overButton(RoundRectangle2D button) { |
| 263 |
0 |
if ((x >= button.getX()) && (x <= button.getX() + button.getWidth()) |
| 264 |
|
&& (y >= button.getY()) && (y <= button.getY() + button.getHeight())) { |
| 265 |
0 |
buttonCurrent = button; |
| 266 |
0 |
return true; |
| 267 |
|
} else { |
| 268 |
0 |
buttonCurrent = null; |
| 269 |
0 |
return false; |
| 270 |
|
} |
| 271 |
|
} |
| 272 |
|
|
| 273 |
|
public void refresh() { |
| 274 |
0 |
if (g2 == null) { |
| 275 |
0 |
g2 = (Graphics2D) getGraphics(); |
| 276 |
|
} |
| 277 |
0 |
g2.drawImage(gameGear, 0, 0, this); |
| 278 |
|
|
| 279 |
0 |
if (buttonCurrent != null) { |
| 280 |
0 |
int rule = AlphaComposite.SRC_OVER; |
| 281 |
0 |
float alpha = (float) 0.5; |
| 282 |
0 |
g2.setComposite(AlphaComposite.getInstance(rule, alpha)); |
| 283 |
0 |
g2.setPaint(Color.red); |
| 284 |
|
|
| 285 |
0 |
g2.fill(buttonCurrent); |
| 286 |
|
|
| 287 |
0 |
alpha = 1; |
| 288 |
0 |
g2.setComposite(AlphaComposite.getInstance(rule, alpha)); |
| 289 |
|
} |
| 290 |
|
|
| 291 |
0 |
g2.setPaint(Color.white); |
| 292 |
0 |
fm = g2.getFontMetrics(f); |
| 293 |
0 |
int height = fm.getHeight() + 2; |
| 294 |
0 |
int textX = 133; |
| 295 |
0 |
int textY = 43; |
| 296 |
0 |
int offset = 50; |
| 297 |
|
|
| 298 |
0 |
g2.drawString("Up", textX, textY); |
| 299 |
0 |
textY += height; |
| 300 |
0 |
g2.drawString("Down", textX, textY); |
| 301 |
0 |
textY += height; |
| 302 |
0 |
g2.drawString("Left", textX, textY); |
| 303 |
0 |
textY += height; |
| 304 |
0 |
g2.drawString("Right", textX, textY); |
| 305 |
0 |
textY += height; |
| 306 |
0 |
g2.drawString("Fire 1", textX, textY); |
| 307 |
0 |
textY += height; |
| 308 |
0 |
g2.drawString("Fire 2", textX, textY); |
| 309 |
0 |
textY += height; |
| 310 |
0 |
g2.drawString("Start", textX, textY); |
| 311 |
|
|
| 312 |
0 |
textY = 43; |
| 313 |
0 |
textX += 45; |
| 314 |
|
|
| 315 |
0 |
if (buttonCurrent != null) { |
| 316 |
0 |
if (buttonCurrent.equals(buttonU)) { |
| 317 |
0 |
g2.setPaint(Color.red); |
| 318 |
|
} else { |
| 319 |
0 |
g2.setPaint(Color.white); |
| 320 |
|
} |
| 321 |
|
} |
| 322 |
|
|
| 323 |
0 |
if ((mousePressed) && (buttonCurrent.equals(buttonU))) { |
| 324 |
0 |
g2.drawString("Press Key", textX, textY); |
| 325 |
|
} else { |
| 326 |
0 |
g2.drawString(KeyEvent.getKeyText(up), textX, textY); |
| 327 |
|
} |
| 328 |
|
|
| 329 |
0 |
if (buttonCurrent != null) { |
| 330 |
0 |
if (buttonCurrent.equals(buttonD)) { |
| 331 |
0 |
g2.setPaint(Color.red); |
| 332 |
|
} else { |
| 333 |
0 |
g2.setPaint(Color.white); |
| 334 |
|
} |
| 335 |
|
} |
| 336 |
|
|
| 337 |
0 |
if ((mousePressed) && (buttonCurrent.equals(buttonD))) { |
| 338 |
0 |
textY += height; |
| 339 |
0 |
g2.drawString("Press Key", textX, textY); |
| 340 |
|
} else { |
| 341 |
0 |
textY += height; |
| 342 |
0 |
g2.drawString(KeyEvent.getKeyText(down), textX, textY); |
| 343 |
|
} |
| 344 |
|
|
| 345 |
0 |
if (buttonCurrent != null) { |
| 346 |
0 |
if (buttonCurrent.equals(buttonL)) { |
| 347 |
0 |
g2.setPaint(Color.red); |
| 348 |
|
} else { |
| 349 |
0 |
g2.setPaint(Color.white); |
| 350 |
|
} |
| 351 |
|
} |
| 352 |
|
|
| 353 |
0 |
if ((mousePressed) && (buttonCurrent.equals(buttonL))) { |
| 354 |
0 |
textY += height; |
| 355 |
0 |
g2.drawString("Press Key", textX, textY); |
| 356 |
|
} else { |
| 357 |
0 |
textY += height; |
| 358 |
0 |
g2.drawString(KeyEvent.getKeyText(left), textX, textY); |
| 359 |
|
} |
| 360 |
|
|
| 361 |
0 |
if (buttonCurrent != null) { |
| 362 |
0 |
if (buttonCurrent.equals(buttonR)) { |
| 363 |
0 |
g2.setPaint(Color.red); |
| 364 |
|
} else { |
| 365 |
0 |
g2.setPaint(Color.white); |
| 366 |
|
} |
| 367 |
|
} |
| 368 |
|
|
| 369 |
0 |
if ((mousePressed) && (buttonCurrent.equals(buttonR))) { |
| 370 |
0 |
textY += height; |
| 371 |
0 |
g2.drawString("Press Key", textX, textY); |
| 372 |
|
} else { |
| 373 |
0 |
textY += height; |
| 374 |
0 |
g2.drawString(KeyEvent.getKeyText(right), textX, textY); |
| 375 |
|
} |
| 376 |
|
|
| 377 |
0 |
if (buttonCurrent != null) { |
| 378 |
0 |
if (buttonCurrent.equals(button1)) { |
| 379 |
0 |
g2.setPaint(Color.red); |
| 380 |
|
} else { |
| 381 |
0 |
g2.setPaint(Color.white); |
| 382 |
|
} |
| 383 |
|
} |
| 384 |
|
|
| 385 |
0 |
if ((mousePressed) && (buttonCurrent.equals(button1))) { |
| 386 |
0 |
textY += height; |
| 387 |
0 |
g2.drawString("Press Key", textX, textY); |
| 388 |
|
} else { |
| 389 |
0 |
textY += height; |
| 390 |
0 |
g2.drawString(KeyEvent.getKeyText(fire1), textX, textY); |
| 391 |
0 |
} if (buttonCurrent != null) { |
| 392 |
0 |
if (buttonCurrent.equals(button2)) { |
| 393 |
0 |
g2.setPaint(Color.red); |
| 394 |
|
} else { |
| 395 |
0 |
g2.setPaint(Color.white); |
| 396 |
|
} |
| 397 |
|
} |
| 398 |
|
|
| 399 |
0 |
if ((mousePressed) && (buttonCurrent.equals(button2))) { |
| 400 |
0 |
textY += height; |
| 401 |
0 |
g2.drawString("Press Key", textX, textY); |
| 402 |
|
} else { |
| 403 |
0 |
textY += height; |
| 404 |
0 |
g2.drawString(KeyEvent.getKeyText(fire2), textX, textY); |
| 405 |
|
} |
| 406 |
|
|
| 407 |
0 |
if (buttonCurrent != null) { |
| 408 |
0 |
if (buttonCurrent.equals(buttonS)) { |
| 409 |
0 |
g2.setPaint(Color.red); |
| 410 |
|
} else { |
| 411 |
0 |
g2.setPaint(Color.white); |
| 412 |
|
} |
| 413 |
|
} |
| 414 |
|
|
| 415 |
0 |
if ((mousePressed) && (buttonCurrent.equals(buttonS))) { |
| 416 |
0 |
textY += height; |
| 417 |
0 |
g2.drawString("Press Key", textX, textY); |
| 418 |
|
} else { |
| 419 |
0 |
textY += height; |
| 420 |
0 |
g2.drawString(KeyEvent.getKeyText(start), textX, textY); |
| 421 |
|
} |
| 422 |
0 |
} |
| 423 |
|
|
| 424 |
|
public void paintComponent(Graphics g) { |
| 425 |
0 |
} |
| 426 |
|
|
| 427 |
|
public void update(Graphics g) { |
| 428 |
0 |
} |
| 429 |
|
|
| 430 |
|
public void repaint(long tm, int x, int y, int width, int height) { |
| 431 |
0 |
} |
| 432 |
|
|
| 433 |
|
public void paintAll(Graphics g) { |
| 434 |
0 |
} |
| 435 |
|
|
| 436 |
|
public Dimension getPreferredSize() { |
| 437 |
0 |
return new Dimension(gameGear.getWidth(this), gameGear.getHeight(this)); |
| 438 |
|
} |
| 439 |
|
|
| 440 |
|
public Dimension getMinimumSize() { |
| 441 |
0 |
return getPreferredSize(); |
| 442 |
|
} |
| 443 |
|
|
| 444 |
|
} |