I have an image I’m drawing in java 2d but I want it to keep wrapping forever once the coordinates are outside the size of the image. The problem at the moment is that once the coordinates that I specify go outside the size of the image, it draws nothing so the image ends.
E.g an image that constantly moves horizontally and doesn’t end.
int posx = (int) ((Player.x / 20));
int posy = (int) ((Player.y / 20));
g.drawImage(SpaceBGLayer0, 0, 0, screenwidth, screenheight, posx, posy, posx+3000, posy+3000, null, null);
To wrap the image, you need to first test whether there’s any ‘overflow’ (ie any parts of the screen where wrapping is required). There are 4 cases: no overflow, x-overflow, y-overflow, x- and y-overflow.
Depending on the case, you may then need to draw additional copies of your image displaced by 3000px in either x, y or both.
I’ve assumed that your (posx,posy) point corresponds to the lower left corner of the screen, and that both screenWidth and screenHeight are less than 3000…
I haven’t tested this. You should replace all the
3000‘s with some variables, such as backgroundWidth and backgroundHeight…