Swing is a single threaded environment, it's also, reasonably optimised for what it does.
It's possible that the repaint manager is condensing the repaint
requests down into a single request, so you only see the last request.
See Concurrency in Swing for more details.
What you "can" do, is postpone the second update, using something like a Swing Timer
...
Something like...
if(key == KeyEvent.VK_UP)
{
comp.setY(y++);
// You may need to force a repaint here
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
comp.setY(y--);
// You may need to force a repaint here
}
});
timer.setRepeats(false);
timer.start();
}
...for example.
See How to use Swing Timers for more details.
Personally, I would setup a Timer
which was capable of performing both the up and down movement based on a state variable it carries, but that's me
I would also discourage the use KeyListener
s as they are just more trouble than they are worth, and instead would encourage the use of the Key Bindings API, see How to Use Key Bindings for more details