Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm practicing some simple techniques with Java swing games, to learn the basics. Right now I'm practicing how to move a sprite... but it's not working! whhyyyy

Here is what I've come up with so far (Main class):

public class GraphicsPracticeDrawing extends JPanel implements Runnable{

private CoinSprite coin2;
private Thread animate;

private final int DELAY = 25;

public GraphicsPracticeDrawing(){
    this.addKeyListener(new InputHandler());

    animate = new Thread(this);

public void Initialize(){
    coin2 = new CoinSprite("images/coin.gif", 25, 25);

public void paintComponent(Graphics g){

    Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(coin2.getImage(), coin2.getX(), coin2.getY(), null);

public void run() {
    long beforeTime, timeDiff, sleep;

    beforeTime = System.currentTimeMillis();

        coin2.move2(); //move the 2nd coin based on the inputHandler

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - timeDiff;

        if(sleep < 0)
            sleep = 2;

        } catch(InterruptedException e){
            System.out.println("Animation interrupted!");

        beforeTime = System.currentTimeMillis();

 * create a inner class to handle key inputs via the CoinSprite class
private class InputHandler extends KeyAdapter{
    public void keyPressed(KeyEvent e) {
    public void keyReleased(KeyEvent e) {


and for my CoinSprite class:

public class CoinSprite {

private BufferedImage img;
private int speedKeyX, speedKeyY;

public CoinSprite(){ }

public CoinSprite(String fileLoc, int x, int y){
        this.img = File(fileLoc));
    } catch (IOException e){
        System.out.println("Can't load file!");

    this.x = x;
    this.y = y;

 * move the sprite based on key inputs
public void move2() {
    this.x += this.speedKeyX;
    this.y += this.speedKeyY;

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_LEFT) {
        speedKeyX = -1; //when move is, called change the speed
    if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
        speedKeyX = 1;

public void keyReleased(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_LEFT){
        speedKeyX = 0;
    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
        speedKeyX = 0;

public BufferedImage getImage(){ return this.img; }
public int getX(){ return this.x; }
public int getY(){ return this.y; }

public void setX(int x){ this.x = x; }
public void setY(int y){ this.y = y; }


When I run the game and press left and right the sprite wont move! I don't know why...

share|improve this question
I would breakpoint the keyPressed procedure in the coin class and inspect the speedKeyX variable. Is it getting set properly? is your move2() getting called (another breakpoint) ... as for your checks e.getKeyCode() == KeyEnent.VK_LEFT) i would use an int and capture that once on entering the keyPressesd function vs. continuing to call getKeycode (i.e. int key = e.getKeyCode(); and then ask if (key == KeyEvent.VK_LEFT) // etc ... also is it possible to go both right and left at once? Your code thinks so, if (left condition) { ... } else if (right condition) { ... } makes more sense for – Robert French Nov 13 '11 at 20:55
You should ask programming questions like this on instead. – Jonas Feb 6 '12 at 11:15
up vote 2 down vote accepted

I found the issue, I needed to set my JPanel to focusable() in order to handle input.

In my constructor I added:


and everything works fine.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.