The Java2D engine I’m building uses a render loop that iterates over a Set of Renderable objects. It calls render(RenderControl) on each Renderable object. It’s then up to each object to call render(int,int,int,int,int[]) so that the image data is added into the main BufferedImage’s raster.
In a test object that just renders an image on screen and increments the position based on how much time has passed (to animate it), I have this code:
@Override
public void render(RenderControl rc) {
if (System.currentTimeMillis() - last >= 20) {
x+=4;
y+=4;
last = System.currentTimeMillis();
}
rc.render(x, y, img.getWidth(), img.getHeight(), data);
}
Where img is a BufferedImage representing the image loaded previously using ImageIO.read.
When this runs, however, the moving image kind of chugs in it’s movement. It freezes or relapses in its path around every second, even though the frame rate is consistently 55-60fps (capped using Thread.sleep).
Is there something wrong with the way I’m doing this? Should the position changing code be executed in a separate thread?
Let me know if you need any more information. Many thanks in advance!
Let me know if you need any more information,