Aditya Arora
Aditya Arora

Reputation: 15

Adding KeyListener in Java

This a small game. Here I made 2 rectangles of same size. I want to move them whenever a person presses a key on keyboard. But I dont know how to add KeyListener. I had looked through previous answers here but I couldn't catch my mistake. I even searched the google but no clue. This is my code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class PongGame extends JComponent implements ActionListener {
    int ballX = 200;
    int ballY = 340;
    short ballX_Direction = 1;
    short ballY_Direction = 1;
    static int Direction1 = 300;
    static int Direction2 = 300;
    private static keyListener move;
    public static void main(String[] args) {
        JFrame frame = new JFrame("Pong Game");        
        PongGame game = new PongGame();
        frame.add(game);
        frame.pack();
        frame.setSize(900,700);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
        Timer t = new Timer(2,game);
        t.start();
    }            
    public void paintComponent(Graphics g) {
        g.setColor(new Color(0,242,237));
        g.fillRect(0,0,900,700);
        g.setColor(Color.black);int a = 60;
        for(int i = 1;i<=5;i++) {
            g.fillRect(444,0+a,12,65);
            a = a+125;
        }
        g.setColor(Color.blue);
        g.fillRect(100,Direction2,15,100);
        g.fillRect(770,Direction1,15,100);
        g.setColor(Color.red);
        g.fillOval(ballX,ballY,20,20);
        repaint();
    }
    public void actionPerformed(ActionEvent e) {
        ballX = ballX + ballX_Direction;
        ballY = ballY + ballY_Direction;      
        if(ballY >= 660) ballY_Direction = -1;
        if(ballY <= 0) ballY_Direction = 1;

        if(ballX >= 444-20 && ballY >= 60-20 && ballX <= 444-20 && ballY <= 185-20) {
            ballX_Direction = -1;
        }
        if(ballX >= 444-20 && ballY >= 185-20 && ballX <= 444-20 && ballY <= 250-20) {
            ballX_Direction = -1;
        }
        if(ballX >= 444-20 && ballY >= 250-20 && ballX <= 444-20 && ballY <= 375-20) {
            ballX_Direction = -1;
        }
        if(ballX >= 444-20 && ballY >= 375-20 && ballX <= 444-20 && ballY <= 500-20) {
            ballX_Direction = -1;
        }
        if(ballX >= 444-20 && ballY >= 500-20 && ballX <= 444-20 && ballY <= 625-20) {
            ballX_Direction = -1;
        }
        repaint();
    }        
}

class keyListener implements KeyListener {
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if(key == KeyEvent.VK_UP) 
        {
            PongGame.Direction1 = PongGame.Direction1 - 25
        }
        if(key == KeyEvent.VK_DOWN) 
        {
            PongGame.Direction1 = PongGame.Direction1 - 25;
        }
        if(key == '1') 
        {
            PongGame.Direction2 -= 25;
        }
        if(key == '4') 
        {
            PongGame.Direction2 += 25;
        }
    }
    public void keyReleased(KeyEvent e) {}  
    public void keyTyped(KeyEvent e) {}  
}    

Upvotes: 0

Views: 74

Answers (1)

kutschkem
kutschkem

Reputation: 8163

JFrame has a method for that:

frame.addKeyListener(new keyListener());

It needs to be added to a GUI part, in particular a part that has focus, since that is the thing the OS tells about input.

Upvotes: 3

Related Questions