Somehow my Java animation isn't moving

1

I'm trying to make a movable object in a Java frame by pressing the arrow keys. Somehow my animation isn't doing anything, neither is it showing the keyCode (if I would be pressing the wrong keys) through a System.out.println().

Here's my code, I'm hoping someone can look through it, maybe test it out himself and figure out where the problem's located for me? I would be very grateful, cause I'm stuck with this for hours now :(

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 GUI extends JPanel implements ActionListener, KeyListener{

Timer tm = new Timer(5, this);
int x = 0, y = 0, velX = 0, velY = 0;

public GUI(){
    tm.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
}

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

public void actionPerformed(ActionEvent e){ 
    x = x + velX;
    y = y + velY;
    repaint();
}

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

    if(c == KeyEvent.VK_LEFT){
        velX = -1;
        velY = 0;
    }
    else if(c == KeyEvent.VK_UP){
        velX = 0;
        velY = -1;
    }
    else if(c == KeyEvent.VK_RIGHT){
        velX = 1;
        velY = 0;
    }
    else if(c == KeyEvent.VK_DOWN){
        velX = 0;
        velY = 1;
    }
    else System.out.println(e.getKeyChar() + " = " + c);
}


public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

public static void main(String[] args){
    GUI t = new GUI();  
    JFrame jf = new JFrame();

    jf.setTitle("Animation");
    jf.setSize(600,400);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.add(t);
}
}

Krikke93

Posted 2014-01-19T13:04:08.460

Reputation: 84

I can't see where the problem is... I tried the code myself a few seconds ago and it works just fine. The red box is moving in the direction of the arrow keys, I press. - Tested with Eclipse Juno. – XWaveX – 2014-01-19T13:08:20.257

Answers

1

In your main method, add the JPanel to the JFrame before calling setVisible:

public static void main(String[] args){
    GUI t = new GUI();  
    JFrame jf = new JFrame();
    jf.add(t);

    jf.setTitle("Animation");
    jf.setSize(600,400);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);
}

patterned

Posted 2014-01-19T13:04:08.460

Reputation: 294

Thank you so much, this seemed to be the problem indeed. Works perfectly now. Kinda weird though, since it worked without this yesterday.

Thanks a bunch!! – Krikke93 – 2014-01-19T13:15:25.477

@user3209916 You sure you didn't edit the code between now and yesterday? – patterned – 2014-01-19T13:17:19.357

Apparently I did, cause it would be impossible to not work now and work yesterday with the same code. Anyways, I'm glad it works now :P – Krikke93 – 2014-01-19T13:23:14.760