Adding KeyListener in Java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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)





Please format your code properly next time.
– hellow
Aug 8 at 11:44





if you are new to Java, you would do better to start by learning the basics. for now, look for a UI component in your application that can listen to a KeyListener. in your code, try this.addKeyListener(new keyListener());
– Stultuske
Aug 8 at 11:46





Since you already found the interface KeyListener doesn't the documentation also tell you how to use it? Btw, "I am new to Java" (which was removed by the edit) - I'd suggest you start with less complex tasks to learn the language. You're basically trying to take 10 steps at a time which is bound to lead you into even more headaches.
– Thomas
Aug 8 at 11:46





You need to add the KeyListener to a focusable GUI part which can then react to it
– XtremeBaumer
Aug 8 at 11:46


KeyListener




1 Answer
1



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.





You should maybe also explain why it ahs to be added to a GUI part
– XtremeBaumer
Aug 8 at 11:46





@XtremeBaumer better?
– kutschkem
Aug 8 at 11:51






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered