I have a for loop that subtracts 1 from the coordinates of the map
Would suggest that you blocking the event dispatching thread, which is responsible for processing paint requests AND key event (amongst other things)
This means that while the loo in keyPressed
is been executed, nothing else will be processed, including the repaint request.
Before you comment that you are painting the component from a separate thread, you're not, actually. What happens is you make a paint request to the RepaintManager
, which places a repaint request into the event queue, which is then processed by the Event Dispatching Thread
What I suggest you do, is raise some kind of flag in keyPressed event method. You would, then monitor for this flag in the game thread and update the state of the map on each iteration of the thread loop until the end state is meet and/or some kind of event changes the state of the flag, for example...
You might like to take. Look at Painting in AWT and Swing and Concurrency in Swing for more details
And while I'm here, I would encourage you to use key bindings over KeyListener
as it has better control over the focus requirements