Arvind Kalra
Arvind Kalra

Reputation: 99

Key action listener in Java Swing

I was making a Nokia snake game. I have already made the Frame and everything and already written my algo. Currently I was doing something so that I can perform actions using my keys (such as left, right, up, down).

I made a class named Frame that extends JFrame and implements action listener, somewhere I saw on Stack Overflow that I need to write my code in key released function so as to make my code run while I press the key.

But when I press the keys nothing happened.

Here is my Java code:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane; 

public class Frame extends JFrame implements KeyListener {

private static final int BOARD_SIZE = 51;
private static final int FRAME_SIZE = 700;
private static final Color SNAKE = Color.GREEN;
private static final Color BOARD = Color.WHITE;
private static final Color FOOD = Color.BLUE;

private static enum DIRECTION {
    right, left, up, down;
}

private static DIRECTION curr = DIRECTION.right;

public Frame() {
    super.setTitle("<<SNAKE GAME>>");
    super.setSize(this.FRAME_SIZE, this.FRAME_SIZE);
    GridLayout layout = new GridLayout(this.BOARD_SIZE, this.BOARD_SIZE);
    super.setLayout(layout);
    int val = this.randomNum(this.BOARD_SIZE * this.BOARD_SIZE - 7);
    int count = 0;
    for (int i = 0; i < this.BOARD_SIZE; i++) {
        for (int j = 0; j < this.BOARD_SIZE; j++) {
            JButton btn = new JButton();
            super.add(btn);
            this.buttons[i][j] = btn;
            if (i == this.BOARD_SIZE / 2 + 1 && j > this.BOARD_SIZE / 2 - 5 
                 && j <= this.BOARD_SIZE / 2 + 6) {
                this.snakeLL.snake.AddFirst(btn);
                btn.setBackground(this.SNAKE);
            } else if (count == val) {
                btn.setBackground(FOOD);
                count++;
            } else {
                btn.setBackground(this.BOARD);
                count++;
            }
        }
    }
    super.addKeyListener(this);
    super.setResizable(false);
    super.setVisible(true);
}

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
    int key = e.getKeyLocation();
    if (key == KeyEvent.VK_UP) {
        this.curr = DIRECTION.up;
    }
}

Upvotes: 2

Views: 2631

Answers (1)

camickr
camickr

Reputation: 324197

, somewhere i saw on stack overflow that i need to write my code in key released function so as to.. make my code run while i press the key...

I don't think you got that advice here.

We always advise that you should be using Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings for basic information.

You can also check out Motion Using the Keyboard which contains a working example of using Key Bindings to animate a component.

Upvotes: 3

Related Questions