1

I have to make a frogger game for my assignment and my key method is not working. Im working on public void key and have no idea why my arrow keys are not registering? Sorry I'm new to this site. my code:

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

}

Ankit
  • 3,083
  • 7
  • 35
  • 59

1 Answers1

2

you have to add an KeyListener to obtain KeyEvents (or use KeyBindings).

any way once you obtain KeyEvents from the KeyListener (or ActionEvents from the KeyBinding) you can delegte these events to your working methode

i'll show you how you use keyBinding, it's the more elegant method

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

after that you must listen for these events, using a ActionListener

public class FroggerComponent extends JComponent  implements ActionListener { 

    //...

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

you simply have to add the ActionListener then

public FroggerComponent(String filename) {

    //...
    addActionListener(this);

}

see also Java: Use keystroke with arrow key

Community
  • 1
  • 1
Martin Frank
  • 3,445
  • 1
  • 27
  • 47