2010-07-10 205 views
1

我有一個小程序,你可以使用WASD鍵以4種方式中的任意一種方式製作32像素的播放器。但是當我添加一個textarea或標籤時,我無法再移動我的圖像。與焦點有關的事情。java - 失去焦點

代碼:

/** 
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

你的格式都搞砸了,確保整個代碼被代碼塊包圍或縮進四個空格 – I82Much 2010-07-10 03:02:27

+0

我不知道該如何,對不起。 – nn2 2010-07-10 03:08:52

+0

我修好了。點擊上面編輯後的藍色鏈接,閱讀編輯評論。此外,在輸入/編輯消息時,請閱讀格式規則的右欄。 **編輯** omg,你再次搞砸了......我會回滾它:) – BalusC 2010-07-10 03:09:35

回答

1

當使用在Swing一個的KeyListener該KeyEvent僅傳遞給具有焦點的組件。

我想,默認情況下,當沒有組件添加到小程序,然後它有焦點。只要你添加一個標籤,它可能就有焦點。您可以嘗試使用

this.setFocusable(true); 

請求專注於Applet之前。

此外,爲什麼不使用Swing而不是AWT?

+0

因爲我學會了更好地使用使用Swing的AWT嗎? – nn2 2010-07-10 03:40:11

+0

你也不會碰巧知道一種方法來定位applet上的像素標籤嗎? – nn2 2010-07-10 03:40:58