1

I've built a simple animation program that moves a box to a direction depending on the arrow key pressed (similar to snake), in JFrame using KeyListener and ActionListener. but I've noticed that if I start the application and I move the mouse the application wont continue to check what arrow key was pressed and which direction to move to.

Can some one explain this to me, and is it because I need to disable something involving mouse events?

heres the code:

public class gui extends JPanel implements ActionListener, KeyListener{
    Timer tm = new Timer(5, this);
    int x = 300, y = 178, velx = 0, vely = 0;


    public gui() {
        tm.start();
        addKeyListener(this);
        setFocusable(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(x, y, 50, 30);

    }

    @Override
    public void keyPressed(KeyEvent e) {
        keys(e);
    }

    public void keys(KeyEvent e) {
        int c = e.getKeyCode();
        if (c == KeyEvent.VK_LEFT)

        {
            velx = -1;
            vely = 0;
        }
        if (c == KeyEvent.VK_UP)

        {
            velx = 0;
            vely = -1;
        }
        if (c == KeyEvent.VK_RIGHT)

        {
            velx = 1;
            vely = 0;
        }
        if (c == KeyEvent.VK_DOWN)

        {
            velx = 0;
            vely = 1;

        }
    }

    public void borders(ActionEvent e) {
        if (x < 0) {
            velx = 0;
            x = 0;
            JOptionPane
                    .showMessageDialog(null, "you hit the borders you lost!");
            System.exit(0);
        }
        if (x > 530) {
            velx = 0;
            x = 530;
            JOptionPane
                    .showMessageDialog(null, "you hit the borders you lost!");
            System.exit(0);
        }
        if (y < 0) {
            velx = 0;
            y = 0;
            JOptionPane
                    .showMessageDialog(null, "you hit the borders you lost!");
            System.exit(0);
        }
        if (y > 330) {
            velx = 0;
            y = 330;
            JOptionPane
                    .showMessageDialog(null, "you hit the borders you lost!");
            System.exit(0);

        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }


    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }


    public void actionPerformed(ActionEvent e) {
        x += velx;
        y += vely;
        repaint();
        borders(e);

    }

}

Coupon22
  • 395
  • 8
  • 24
david1597
  • 39
  • 1
  • 4

1 Answers1

0

I don't know what your problem is. I used your code and had no problem.

Here is what I used:

        public gui() 
        {
            addKeyListener(this);
            setFocusable(true);
        }

        public void keyPressed(KeyEvent e)
        {
            int c = e.getKeyCode();
            if (c == KeyEvent.VK_LEFT)
            {
                velx = -1;
                vely = 0;
            }
            if (c == KeyEvent.VK_UP)
            {
                velx = 0;
                vely = -1;
            }
            if (c == KeyEvent.VK_RIGHT)
            {
                velx = 1;
                vely = 0;
            }
            if (c == KeyEvent.VK_DOWN)
            {
                velx = 0;
                vely = 1;
            }
        }

        public void actionPerformed(ActionEvent e)
        {
            x += velx;
            y += vely;
            repaint();
            borders(e);
        }

        public static void main(String[] args)
        {
            JFrame frame = new JFrame("gui");
            frame.add(new gui());
            frame.setVisible(true);
            frame.setSize(600, 400);
        }
    }

I clicked the mouse and moved it before I started the program, then I used the keys; it works fine. I would switch to KeyBindings though.

Coupon22
  • 395
  • 8
  • 24