Q:

Continuously pressed key

I have the following code snippet from my SWING game:
public void keyPressed(KeyEvent e) {

        if(e.getKeyCode() == KeyEvent.VK_RIGHT)
        {         
             player.setX(player.getX() + speed);
             canvas.repaint();

        }

The code is working correctly, when I press the right arrow(VK_RIGHT),
the player moves RIGHT, if I press the left arrow it moves left.
The problem is when I hold the LEFT or RIGHT arrow, the player moves once and then it stops for some time and then it starts continuously moving the pressed direction. 
I think it takes time for java to understand that the pressed button is HOLD. Any idea how I can make it move continuously instantly?

A:

Any idea how I can make it move continuously instantly?

Animate the custom painted component by using a Swing timer to invoke repaint.
Add a KeyListener to the component.
Start moving on keyPressed(KeyEvent)
Stop moving on keyReleased(KeyEvent)

