2015-12-03 78 views
1

我必須爲我的任務製作一款青蛙遊戲,而且我的關鍵方法無法正常工作。我工作公共無效的鑰匙,不知道爲什麼我的箭頭鍵不註冊? 對不起,我是這個網站的新手。我的代碼:如何使用箭頭鍵移動圖像

// FroggerComponent.java 

    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import java.io.*; 
    import javax.imageio.*; 
    public class FroggerComponent extends JComponent { 
// Size of the game grid 
public static final int WIDTH = 20; 
public static final int HEIGHT = 7; 
// Initial pixel size for each grid square 
public static final int PIXELS = 50; 
// Image filenames for car, lily, and frog 
public static final String[] IMAGES = new String[] { "Images/frog.png", "Images/car.png", "Images/lily.png" }; 
// Colors for ROAD, WATER, and DIRT 
public static final Color[] COLORS = new Color[] { Color.BLACK, Color.BLUE, Color.GRAY }; 
// Codes to store what is in each square in the grid 
public static final int EMPTY = 0; 
public static final int CAR = 1; 
public static final int LILY = 2; 
private Image frog; 
private Image car; 
private Image lily; 

private int[][] grid = new int[WIDTH][HEIGHT]; 
Row[] rows = new Row[HEIGHT]; 
private int x; 
private int y; 
int x1 = 6; 
int y1 = 10; 
int dx; 
private int dy; 

private boolean dead; 

/* 
* Provided utility method to read in an Image object. If the image cannot 
* load, prints error output and returns null. Uses Java standard 
* ImageIO.read() method. 
*/ 
private Image readImage(String filename) { 
    Image image = null; 
    try { 
     image = ImageIO.read(new File(filename)); 
    } catch (IOException e) { 
     System.out.println("Failed to load image '" + filename + "'"); 
     e.printStackTrace(); 
    } 
    return (image); 
} 

private void readRow(String file) { 
    try { 
     FileReader fr = new FileReader(file); 
     BufferedReader br = new BufferedReader(fr); 
     String line = br.readLine(); 
     int count = 1; 
     while ((line != null) && (line != "\n") && (line != "\r")) { 
      Row r = new Row(line); 
      rows[count] = r; 
      line = br.readLine(); 
      count++; 
     } 
    } catch (IOException ex) { 
     System.out.println("File not found!"); 
    } 

} 

public FroggerComponent(String filename) { 
    setPreferredSize(new Dimension(WIDTH * PIXELS, HEIGHT * PIXELS)); 
    readRow(filename); 
    /* 
    * Add your code here ... readImage(...); ... 
    */ 

    frog = readImage("images/frog.png"); 
    lily = readImage("Images/lily.png"); 
    car = readImage("Images/car-red.png"); 
    readRow("Images/world.txt"); 
    reset(); 

} 

public void reset() { 
    for (int x = 0; x < grid.length; x++) 
     for (int y = 0; y < grid[x].length; y++) 
      grid[x][y] = EMPTY; 
    dead = false; 
    x = 0; 
    y = 6; 
    repaint(); 
} 

private void moveBy(int dx, int dy) { 
    if ((x1 + dx >= 0 && x + dx < WIDTH) && (y1 + dy >= 0 && y1 + dy < HEIGHT)) { 
     x1 += dx; 
     y1 += dy; 
    } 
} 

public boolean isWin() { 
    return (y == 0); 
} 

public void key(int code) throws ArrayIndexOutOfBoundsException { 
    /* 
    * Add your code here 
    */ 

    if (code == KeyEvent.VK_UP) { 
     x1 = x1 - 1; 

    } else if (code == KeyEvent.VK_DOWN) { 
     x1 = x1 + 1; 
    } else if (code == KeyEvent.VK_LEFT) { 

     y1 = y1 - 1; 
    } else if (code == KeyEvent.VK_RIGHT) { 
     y1 = y1 + 1; 
    } 

    this.repaint(); 

} 

public void paintComponent(Graphics g) { 
    /* 
    * Add your code here 
    * 
    * 
    */ 
    // variables 
    int squareWidth = getWidth()/grid.length; 
    int squareHeight = getHeight()/grid[0].length; 

    // paints the rows 

    for (int y = 0; y < 20; y++) { 
     if (grid[y][x] == EMPTY) { 
      g.setColor(Color.GRAY); 
      g.fillRect(squareWidth * y, squareWidth * x, squareWidth, squareHeight); 
     } 
    } 
    x++; 
    for (int y = 0; y < 20; y++) { 

     if (grid[y][x] == EMPTY) { 
      g.setColor(Color.BLUE); 
      g.fillRect(squareWidth * y, squareWidth * x, squareWidth, squareHeight); 
     } 
    } 
    x++; 
    for (int y = 0; y < 20; y++) { 

     if (grid[y][x] == EMPTY) { 
      g.setColor(Color.BLACK); 
      g.fillRect(squareWidth * y, squareWidth * x, squareWidth, squareHeight); 
     } 
    } 
    x++; 
    for (int y = 0; y < 20; y++) { 

     if (grid[y][x] == EMPTY) { 
      g.setColor(Color.BLUE); 
      g.fillRect(squareWidth * y, squareWidth * x, squareWidth, squareHeight); 
     } 
    } 
    x++; 
    for (int y = 0; y < 20; y++) { 

     if (grid[y][x] == EMPTY) { 
      g.setColor(Color.BLUE); 
      g.fillRect(squareWidth * y, squareWidth * x, squareWidth, squareHeight); 
     } 
    } 
    x++; 
    for (int y = 0; y < 20; y++) { 

     if (grid[y][x] == EMPTY) { 
      g.setColor(Color.BLACK); 
      g.fillRect(squareWidth * y, squareWidth * x, squareWidth, squareHeight); 
     } 
    } 
    x++; 
    for (int y = 0; y < 20; y++) { 

     if (grid[y][x] == EMPTY) { 
      g.setColor(Color.GRAY); 
      g.fillRect(squareWidth * y, squareWidth * x, squareWidth, squareHeight); 
     } 
    } 
    // frog 

    g.drawImage(frog, squareWidth * y1, squareHeight * x1, squareWidth, squareHeight, this); 

    // lilypads 
    g.drawImage(lily, squareWidth, squareHeight, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 5, squareHeight, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 10, squareHeight, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 10, squareHeight * 3, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 8, squareHeight * 3, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 3, squareHeight * 3, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 2, squareHeight * 4, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 4, squareHeight * 4, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 12, squareHeight * 4, squareWidth, squareHeight, null); 
    g.drawImage(lily, squareWidth * 17, squareHeight * 4, squareWidth, squareHeight, null); 
    // cars 
    g.drawImage(car, squareWidth * 2, squareHeight * 5, squareWidth, squareHeight, null); 
    g.drawImage(car, squareWidth * 5, squareHeight * 5, squareWidth, squareHeight, null); 
    g.drawImage(car, squareWidth * 11, squareHeight * 5, squareWidth, squareHeight, null); 
    g.drawImage(car, squareWidth * 7, squareHeight * 2, squareWidth, squareHeight, null); 
    g.drawImage(car, squareWidth * 4, squareHeight * 2, squareWidth, squareHeight, null); 

} 

public void tick(int round) { 
    /* 
    * Add your code here 
    */ 
} 

}

+0

如何無效的方法鍵被調用?參數int代碼是如何確定的?代碼中缺少唯一的interessting部分。 – ArcticLord

+2

[如何使用鍵綁定](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – MadProgrammer

+0

@MadProgrammer你太快了^ _ ^很高興再次見到你! –

回答

2

必須添加一個KeyListener獲得KeyEvents(或使用鍵綁定)。

任何方式,一旦你獲得了KeyListener(或鍵綁定ActionEventsKeyEvents可以delegte這些事件對你的工作梅索德

我會告訴你如何使用按鍵綁定,這是更優雅的方法

public void addKeyBinding(){ 
    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT), 
         "left") 
    //map as many keys as you want 
} 

必須偵聽這些事件,使用的ActionListener

public class FroggerComponent extends JComponent implements ActionListener { 

    //... 

    @Override 
    public void actionPerformed(ActionEvent e){ 
     if ("left".equals(e.getActionCommand()){ 
      key(KeyEvent.VK_LEFT) 
     } 
    } 
} 

你只需要添加的ActionListener然後

public FroggerComponent(String filename) { 

    //... 
    addActionListener(this); 

} 

也看到Java: Use keystroke with arrow key