我有一个小程序,您可以使用WASD键以4种方式中的任何一种方式制作32像素的播放器。但是当我添加textarea或标签时,我无法移动我的图像。与焦点有关。
代码:
/**
Tile Generator
Programmer: Dan J.
Thanks to: g00se, pbl, Manny.
Started May 23, 2010
**/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import java.util.*;
public class tileGen extends Applet implements KeyListener {
Image[] tiles; // tile arrays
Image player; // player image
int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile x - tile y
boolean left, right, down, up, canMove, respawn; // is true?
int[][] board; // row tiles for ultimate mapping experience!
final int NUM_TILES = 33; // how many tiles are we implementing?
Label lx, ly; // to see where we are!
int lastX, lastY, row, col;
Label lbl1;
public void init() {
board = loadBoard();
tiles = new Image[NUM_TILES];
for(int i = 0;i < NUM_TILES;i++) {
tiles[i] = getImage(getClass().getResource(String.format("tiles/t%d.png", i)));
}
player = getImage(getClass().getResource("player.png")); // our player
addKeyListener(this);
canMove = true;
int spawnX = 4;
int spawnY = 4;
px =0;
py =0;
lastX = 0;
lastY= 0;
lbl1 = new Label("Label 2");
add(lbl1);
this.requestFocusInWindow();
}
private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
static {
BLOCKED_TILES.add(24);
BLOCKED_TILES.add(0);
BLOCKED_TILES.add(6);
BLOCKED_TILES.add(25);
BLOCKED_TILES.add(3);
//add more tiles here
}
public void keyPressed(KeyEvent e) {
if (isInBound(lastX,lastY) == true) {
System.out.println("\nYOU WENT OFF THE GRID.\n");
}
int r1 = lastX + 1;
if (lastX > 0) {
r1 = lastX + 1;
}else{
r1 = 0;
}
int r2 = lastY;
int u1 = lastX;
int u2;
if (lastY > 0) {
u2 = lastY - 1;
}else{
u2 = 0;
}
int l1;
if (lastX > 0) {
l1 = lastX - 1;
}else{
l1 = 0;
}
int l2 = lastY;
int d1 = lastX;
int d2;
if (lastY > 0) {
d2 = lastY + 1;
}else{
d2 = 0;
}
right = true;
left = true;
up = true;
down = true;
if (blocked(r1,r2) == true) right = false; // we cannot go right
if (blocked(u1,u2) == true) up = false; // we cannot go up
if (blocked(l1,l2) == true) left = false; // we cannot go left
if (blocked(d1,d2) == true) down = false; // we cannot go down
if (left == true) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
px = px - 32;
lastX = lastX - 1;
}
}
if (right == true) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
px = px + 32;
lastX = lastX + 1;
}
}
if (down == true) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
py = py + 32;
lastY = lastY + 1;
}
}
if (up == true) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
py = py - 32;
lastY = lastY - 1;
}
}
repaint();
}
public void keyReleased(KeyEvent e){
} // ignore
public void keyTyped(KeyEvent e){} // ignore
public void paint(Graphics g) {
for (row = 0; row < board.length; row++) {
for (col = 0; col < board[row].length; col++) {
int index = board[row][col];
g.drawImage(tiles[index], 32 * col, 32 * row, this);
}
}
if (respawn == false) {
g.drawImage(player, px, py, this);
}
if (respawn == true) {
g.drawImage(player, 0,0, this);
System.out.println("Respawned!");
respawn = false;
}
} // end paint method
public void update(Graphics g)
{
paint(g);
}
public int[][] loadBoard() {
int[][] board = {
{ 2,2,24,24,24,24,24,1,3,0,0,0 },
{ 2,2,24,23,23,23,24,1,3,0,0,0 },
{ 1,1,24,23,23,23,24,1,3,3,3,1 },
{ 1,1,24,24,23,24,24,1,1,1,1,1 },
{ 1,1,1,1,7,1,1,1,1,1,1,1 },
{ 5,1,1,1,7,7,7,7,7,1,1,1 },
{ 6,1,3,1,1,1,3,1,7,7,7,1 },
{ 6,1,3,1,3,1,1,1,1,1,7,3 }
};
return board;
}
public boolean blocked(int tx, int ty) {
return BLOCKED_TILES.contains(board[ty][tx]);
}
public boolean isInBound(int r, int c) {
return (r >= 0) && (r < 8) && (c >= 12) && (c < 1);
}
} // end whole thing
答案 0 :(得分:1)
在Swing中使用KeyListener时,KeyEvent仅传递给具有焦点的组件。
我猜想默认情况下,当没有组件添加到Applet时,它就会有焦点。只要添加标签,它就可能具有焦点。您可以尝试使用
this.setFocusable( true );
在请求关注Applet之前。
另外,为什么不使用Swing而不是AWT?