Alan
Alan

Reputation: 23

Java: Why doesn't my KeyEvent work?

Hey so I'm fairly new to java, but based on doing a lot of research and analyzing various code, I came up with some code to move a basic circle on a screen by using arrow keys.

However, for some reason, the code does not run the whole key event. If I press any key on the keyboard, even different arrow keys, it will cause the circle to only move one direction.

Based on my code, I feel like everything should work, but it just won't run the KeyEvent function properly. Any ways to fix it?

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

public class second extends JPanel implements ActionListener, KeyListener
{
  Timer t = new Timer(5, this);
  int x = 0, y = 0, velx = 0, vely = 0;

  public second()
  {
    t.start();
    addKeyListener(this);
    setFocusable(true);
  }

  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.fill(new Ellipse2D.Double(x, y, 40, 40));
  }

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

  public void up()
  {
    velx = 0;
    vely = 2;
  }

  public void down()
  {
    velx = 0;
    vely = -2;
  }
  public void left()
  {
    velx = -2;
    vely = 0;
  }
  public void right()
  {
    velx = 2;
    vely = 0;
  }

  public void keyPressed(KeyEvent e)
  {
    int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_UP);
    {
      up();
    }
    if (keyCode == KeyEvent.VK_DOWN);
    {
      down();
    }
    if (keyCode == KeyEvent.VK_LEFT);
    {
      left();
    }
    if (keyCode == KeyEvent.VK_RIGHT);
    {
      right();
    }
  }

  public void keyTyped(KeyEvent e)
  {
  }

  public void keyReleased(KeyEvent e)
  {
    velx = 0;
    vely = 0;
  }

  public static void main(String args[])
  {
    second s = new second();
    f.add(s);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(800, 600);
  }
}

Upvotes: 1

Views: 2666

Answers (2)

Oyzuu
Oyzuu

Reputation: 371

First, you have a semi-colon following your first if statement in keyPressed(), which impeaches considering following instructions. cf :

public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();

    if (keyCode == KeyEvent.VK_UP)
        up();
    if (keyCode == KeyEvent.VK_DOWN)
        down();
    if (keyCode == KeyEvent.VK_LEFT)
        left();
    if (keyCode == KeyEvent.VK_RIGHT)
        right();
}

You also have a negative value for vely in down() where it should be positive (and vice-versa). And since you already reset velx and vely to 0 on keyReleased(), it is unnecessary to do it in your directions functions.

public void up() {
    vely = -2;
}

public void down() {
    vely = 2;
}

Class names should be CamelCase (not lowercase as presented).

Have fun.

Upvotes: 1

Sunil Dabburi
Sunil Dabburi

Reputation: 1472

First of all, ActionListener is not required in this case. KeyListener is sufficient. Also there is a semicolon beside if statements, this is causing all the functions up, down, left and right to be invoked negating the effect.

Also for up, y should be reduced and for down, y should be increased.

Please find the below code which works fine. You can limit the right and down movement with MAX_LIMIT based on the window size. I limited it for 0,0 for left and up movement.

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

public class Second extends JPanel implements KeyListener
{
    int x = 0, y = 0;
  public Second()
  {
    addKeyListener(this);
    setFocusable(true);
  }

  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.fill(new Ellipse2D.Double(x, y, 40, 40));
  }

  public void up()
  {
    if(y >= 5) {
        y -= 5;
        repaint();  
    }    
  }

  public void down()
  { 
    y += 5;
    repaint();
  }

  public void left()
  {   
    if(x >= 5) {
        x -= 5;
        repaint();  
    }
  }

  public void right()
  {   
    x += 5;
    repaint();  
  }

  public void keyPressed(KeyEvent e)
  {
    int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_UP)
    {
      up();
    }
    if (keyCode == KeyEvent.VK_DOWN)
    {
      down();
    }
    if (keyCode == KeyEvent.VK_LEFT)
    {
      left();
    }
    if (keyCode == KeyEvent.VK_RIGHT)
    {
      right();
    }
  }

  public void keyTyped(KeyEvent e)
  {
  }

  public void keyReleased(KeyEvent e)
  {
  }

  public static void main(String args[])
  {
    Second s = new Second();
    JFrame f = new JFrame();
    f.add(s);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(800, 600);
  }
}

Upvotes: 1

Related Questions