0

I am making a brick breaker assignment for school and i do not understand what I am doing wrong with the keyListener

import java.awt.*;

import java.awt.event.; import javax.swing.;

public class Board extends JPanel {

private Brick bricks[];
private Ball ball;
private Timer timer;
private Paddle paddle;

public Board() {
    bricks = new Brick[Settings.TOTAL_BRICKS];
    //create a bunch of bricks
    for (int i =0;i<Settings.TOTAL_BRICKS;i++) {
        int row = (int) Math.floor(i / Settings.BRICK_COLUMNS);
        int col = i % Settings.BRICK_COLUMNS;

        int x = 10 + 70 * col;
        int y = 10 + 70 * row;
        bricks[i] = new Brick(x,y);
    }

    ball = new Ball(100,100);
    ball.setSpeed(2,2);
    paddle = new Paddle(100,315);



    timer = new Timer(20, new GameTick());
   timer.start();

   addKeyListener(new KeyboardManager());
   setFocusable(true);
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    for (int i = 0; i < Settings.TOTAL_BRICKS;i++) {
        g2d.drawImage(bricks[i].image,bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height,this);


    }
    g2d.drawImage(ball.image,ball.x,ball.y,ball.width,ball.height,this);
    g2d.drawImage(paddle.image,paddle.x,paddle.y,paddle.width,paddle.height,this);

}
public void padcolis()
{

}


private class KeyboardManager extends KeyAdapter {


    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode(); 
        if (key == KeyEvent.VK_LEFT) {
            paddle.setMovement(-1);
        }
        if (key == KeyEvent.VK_RIGHT) {
            paddle.setMovement(1);


    }
    }
    public void keyRelease(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            paddle.setMovement(0);
        }
    }

    }


private class GameTick implements ActionListener
{
   public void actionPerformed(ActionEvent event)
   {
       ball.move();
       ball.coli();
       paddle.move();

      repaint();
   }

}

}

hears the paddle

mport java.awt.Image;

import javax.swing.ImageIcon;

public class Paddle extends Sprite{

int moveXP, moveY, sloco=100;

public Paddle(int _x, int _y) {
    x = _x;
    y = _y;
    ImageIcon icn = new ImageIcon("paddle.png");
    image = icn.getImage();
    width = image.getWidth(null);
    height = image.getHeight(null);



}

public void setMovement(int i) {
    moveXP = i; 
}
public void move()
{
    if (moveXP == 0)
    {
        x = x + moveXP;
    }

    if (moveXP == 1)
    {
        x=x+moveXP;
        sloco ++;
    }
    if (moveXP ==-1)
    {
        x = x + moveXP;
        sloco --;
    }

}
public int colip(int i)
{
    int full = sloco + 75;
    return (full);
}

}

  • 2
    What happens incorrectly? What errors are you getting? – APerson May 18 '17 at 04:01
  • Questions seeking debugging help ("why isn't this code working?") **must include the desired behavior, a specific problem or error** and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. – Erwin Bolwidt May 18 '17 at 04:01
  • Use the [Key Bindings API](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) and solve all your related issues – MadProgrammer May 18 '17 at 04:02
  • Sorry Erwin. I am new to the site. My **specific problem or error** is that i want to move a paddle in java and nothing happens when I press the keys. I hope thats _useful to other readers_ – Limocrasher May 18 '17 at 04:13
  • @APerson it does not do anything i am not getting an error just no number being passed to the paddle class. I am pretty sure that the Keylisteners are not working – Limocrasher May 18 '17 at 04:14
  • 1
    @Limocrasher `KeyListener` is well known for it's focus related issues. If you do any research into the problem, you will find that the only reliable solution is to make use of the Key Bindings API which was introduced, in part, to solve this issue – MadProgrammer May 18 '17 at 04:20
  • If this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach. – trashgod May 18 '17 at 04:31

0 Answers0