Original Post
The setup: I am trying to render an updating tilemap to the screen as part of an animation loop. The tilemap is a 2D array of Tile class objects ( Tile[][] tilemap = ...). The animation loop takes places in the run() method of a main class that implements Runnable and extends JPanel. Inside the run() method there are 3 methods, in the following order: update(), render(), paintScreen() The update() method updates the tilemap 2D array by shifting all tiles in the array left, right, up or down and adding new tiles, depending on the characters direction of movement. The render() method draws all game elements to an offscreen Graphics object, including the drawing of the complete tilemap object. The paintScreen() method actively renders the buffered image to the screen. It does this by getting a fresh reference to the panel's graphics context each time (this.getGraphics()) and also ensures that the display is properly sync'ed (Toolkit.getDefaultToolkit().sync()), which is necessary because I am developing on Linux, which doesn't automatically flush it's display buffer. The problem: As the player character (locked in the centre of the screen) walks around, the tilemap moves under them and correctly updates itself as the player crosses a tile boundary, as you would expect. The problem being that every-so-often (at seemingly irregular intervals) when the player crosses a tile boundary the tilemap seems to "jump" back by one tile and the forward again very rapidly. I have thoroughly and repeatedly examined and debugged both the update and the render code for the tilemap and am convinced it is correct. It is almost as if the render code is accessing an old version of the tilemap data structure - but how can this be if the update and the render are two discrete steps, one that follows another, both coming from the same thread and there are no other methods that access the tilemap array??? I can only think that maybe the JVM "schedules" the update of the 2D array and this sometimes get queued behind the next render call. If anyone has any insight into this problem at all then I'd be most grateful to hear it...