Make the stage move when the main character move

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)//Make sure its not overlapping at the left-most
			return 0;
		else if (testcord2 > stage.width)//Make sure its not overlapping at the right-most
			return ~(testcord - testcord2 % stage.width);
		else//It is not overlapping.
			return ~testcord;
	}

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 :slight_smile:

you shouldn’t create a image as big as your hole game world.

Just draw to your screen as normal(without an bufferedimage), but use a affine transformation(google how to do it with java2d) to move your view.

for example if you want to have your main char always in the middle of the display:
just do this:
affine.transform(-(player.x - halfScreenWidth), 0);

to get the desired effect at the start and end of the “stage” add a min and max

affine.transform(-(min(stageWidth-screenWidth, max(0, player.x - halfScreenWidth))), 0);

Thanks, I will read about affine transformation.
So, using your approach, I will be using this method to render?
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#drawRenderedImage(java.awt.image.RenderedImage, java.awt.geom.AffineTransform)

there are probably people here with more experience in java2d then me, but this should be fine
I think there should also be something like Graphics2d.setTransformation(x)

just give it a try, it’s so much faster just to google a bit and try everything until it works then waiting for someones answer on a forum :wink:

I am googling too. What transform method are you referring at? There is no transform method can make your suggestion work(ie no one takes two ints/floats etc).

you have to master your google skills then, young padawan

found after aprox. 0.032ns

public void paint(Graphics g) {
	AffineTransform transformer = new AffineTransform();
	transformer.translate(5,5);
	Graphics2D g2d = (Graphics2D)g;
	g2d.setTransform(transformer);
	// draw to g2d.
}

Oh I get how it works now. And I also got it to work :slight_smile:
Final question, “(-(min(stageWidth-screenWidth, max(0, player.x - halfScreenWidth))), 0);”
By “min” and “max”, of what? Sorry for lacking common sense.
I would google this, but I am pretty sure I cant find an interpretation of your snippset on google :stuck_out_tongue:

Edit: errrr I am pretty sure they are from java.lang.Math?
Well then, again, thanks :slight_smile:

Edit2: Got it 100% working :smiley: Btw, nice calculations. I would never be able to come up with such math solutions.