Imagine playing Super Mario Bros on a PC emulator in windowed mode, and the size of the window was equal to the size of the stage. That would make the window extremely wide and it would totally suck, wouldnt it? That is a problem I am having. The size of the JFrame is equal to the size of the stage.
What I am trying to do is to make the size of the JFrame smaller than the size of the stage and make the stage move when the main character moves, so he always is in the middle of the window, except if "he" is at the right-most or left-most at the map.
The entire stage is a hardware accelerated BufferedImage(meaning it is not modified after its creation). The stage is painted on the JFrame with java.awt.Graphics drawImage(img, x, y, w, h, null) method. Putting negative values on x and y should nudge the image to the left, making it looks like the main character is in the middle. Here is my code, works but very buggy:
public void draw(Graphics2D g)
int cord1 = getXCorrdinate();
int cord2 = getYCorrdinate();
g.drawImage(stage.map, cord1, cord2, stage.width, stage.height, null);
private int getXCorrdinate()
int testcord = (ourChar.posX - (stage.visibleWidth / 2)) + ourChar.width / 2;
int testcord2 = (ourChar.posX + (stage.visibleWidth / 2)) + ourChar.width / 2;
if (testcord < 0)
else if (testcord2 > stage.width)
return ~(testcord - testcord2 % stage.width);
stage.width and stage.height is the full dimension of the stage, we do not have to worry about these.
Lets look at cord1(and ignore cord2 for now, one problem at the time).
ourChar is the main character, and stage.visibleWidth is the width of the JFrame(and not the stage).
So whats wrong with these codes: the main character is not in the middle.
I hope my text was easy to understand. The code is a different matter thought