Java Tutorial 45 (GUI) – Moving Object with Keyboard Inputs (KeyListener)

///Java Tutorial 45 (GUI) – Moving Object with Keyboard Inputs (KeyListener)

Java Tutorial 45 (GUI) – Moving Object with Keyboard Inputs (KeyListener)

FavoriteLoadingAdd to favorites

In this tutorial, you will learn how to create a user controlled object. KeyListener is a must in games.

source

Java468x60
By |2020-11-20T10:39:45+00:00November 20th, 2020|Java Video Tutorials|20 Comments

20 Comments

  1. 1 November 20, 2020 at 10:39 am - Reply

    My rectangle disappears after I move it 🙁

  2. Anna Obretencheva November 20, 2020 at 10:39 am - Reply

    Best Channel Ever

  3. Ali FX November 20, 2020 at 10:39 am - Reply

    You don’t need to create a Rectangle object??

  4. Larry Li November 20, 2020 at 10:39 am - Reply

    Very useful! I am doing my java final game project, and this helps a lot.

  5. artmasterpl November 20, 2020 at 10:39 am - Reply

    damm i waste like 3h to figure out why my object didnt move … I had to press tab key each time I lunch the app in other to move the object

  6. Dylan Z November 20, 2020 at 10:39 am - Reply

    I got the rectangle to move but it doesn’t stop the rectangle when you release a key.. i have that part of the code and all the brackets are the same

  7. Becca O'Brien November 20, 2020 at 10:39 am - Reply

    you talk so BLODDY SLOW !

  8. akash shukla November 20, 2020 at 10:39 am - Reply

    Thanks for the video

  9. Boys Passetti November 20, 2020 at 10:39 am - Reply

    Can you make a video on collision detection?

  10. Hamza Siddiqui November 20, 2020 at 10:39 am - Reply

    Got stuck in error .i checked many times it keep showing that your class is not abstract.anyone please?

  11. phanciar sompod November 20, 2020 at 10:39 am - Reply

    Thank you brother.

  12. FAJARDO, Hans Alexander November 20, 2020 at 10:39 am - Reply

    Guys this is my code…. sometimes it works but sometimes it don't…WHY?!?!?!? HELP PLEASEEEEE:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class Main extends JPanel implements KeyListener, ActionListener{
    Timer timer = new Timer(5, this);

    int y = 310, vely = 0;

    public static void main(String[] arge) {
    Main main = new Main();
    JFrame window = new JFrame("Kill It");
    window.setBounds(420, 150, 1080, 720);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    window.setResizable(false);
    window.add(main);
    }
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillRect(20, y, 50, 50);

    }

    public Main() {
    timer.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
    if(y < 0)
    {
    vely=0;
    y = 0;
    }

    if(y > 640)
    {
    vely=0;
    y = 640;
    }
    y += vely;
    repaint();
    }

    @Override
    public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if (code == KeyEvent.VK_W){
    vely = -1;
    }
    if (code == KeyEvent.VK_S) {
    vely = 1;
    }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
    vely = 0;
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
    }

    }

  13. MrTerryPup November 20, 2020 at 10:39 am - Reply

    so i got it working but for some reason, when i hold down the buttons to move and then press a different button, it would freeze. for example, if i hold the down button and then quickly press the right button, it would freeze

  14. Bangus Rice November 20, 2020 at 10:39 am - Reply

    HERE IS MY CODE FOR SOME REASON IT RUNS BUT DOES NOT LET ME MOVE MY BALL

  15. ori kosary November 20, 2020 at 10:39 am - Reply

    how can i add another object

  16. Street Gamer November 20, 2020 at 10:39 am - Reply

    10 "wanna be programmers" disliked lol

  17. Vivek sai kode November 20, 2020 at 10:39 am - Reply

    thanks man!:)

  18. Vivek sai kode November 20, 2020 at 10:39 am - Reply

    i got everything correct 🙂 here is my code just copy it!
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class Gamara extends JPanel implements ActionListener, KeyListener{
    Timer tm=new Timer(2,this);

    int x=0,y=0,velX=0,velY=0;
    public Gamara ()
    {
    tm.start(); //starts timer
    addKeyListener(this); //this referring to KeyListener
    setFocusable(true); //enable KeyListener
    setFocusTraversalKeysEnabled(false); //shift or tab is not use so F
    }
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);

    g.setColor(Color.red);
    g.fillRect(x,y,50,30);
    }
    public void actionPerformed(ActionEvent e)
    {

    if(x < 0)
    {
    velX=0;
    x = 0;
    }

    if(x > 500)
    {
    velX=0;
    x = 500;
    }

    if(y < 0)
    {
    velY=0;
    y = 0;
    }

    if(y > 300)
    {
    velY=0;
    y = 300;
    }
    x=x+velX;
    y=y+velY;
    repaint();

    }
    public void keyPressed(KeyEvent e)
    {
    int c = e.getKeyCode();
    if (c == KeyEvent.VK_LEFT) // VK_Left is left arrow
    {
    velX = -3;
    velY = 0;
    }
    if (c == KeyEvent.VK_UP) // VK_UP is up arrow
    {
    velX = 0;
    velY = -3; // means up
    }
    if (c == KeyEvent.VK_RIGHT)
    {
    velX = 3;
    velY = 0;
    }

    if (c == KeyEvent.VK_DOWN)
    {
    velX = 0;
    velY = 3;
    }
    }
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){
    velX=0;
    velY=0;

    }
    /*public static void main(String [] args)
    {

    JFrame f=new JFrame("vivek");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Gamara a =new Gamara ();
    f.add(a);
    f.setSize(600,400);
    f.setVisible(true);
    }
    }*/

    public static void main(String[] args)
    {
    Gamara g = new Gamara();
    JFrame jf = new JFrame();
    jf.setTitle("Tutorial");
    jf.setSize(600,400);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Gamara g1 = new Gamara();
    jf.add(g1);

    }

    }

  19. Anh Huynh Ngoc November 20, 2020 at 10:39 am - Reply

    why we need to setFocusable is true???

  20. Kepler November 20, 2020 at 10:39 am - Reply

    TLDR: This guy doesn't know what any of his lines of code are doing and probably copied this form somewhere.

    Adding a keyListener(this) in the constructor is actually referring to the current object.Which means the Tutorial Class.
    Same with the timer.. and setFocusable doesn't tell your timer anything. It allows the window (or Swing components) to receive focus or become "active"

Leave A Comment

*