Trying to center my game's display, need some advice!

Hello all, in my game’s core class I’m trying to center the game’s display, and it turns out looking like this.

I have a Dimension called fSize, it’s just the width/height of the user’s screen. I have another Dimension called size, which is at a minimum of 1280x800, the user can change this in the launcher options.

I am as well scaling down the game and stretching it, if you zoom into the image you’ll see that the pixels are uneven.

Here’s the current code I have for centering it. Does anyone have any help?

g.drawImage(display, (fSize.width / 2) - (size.width / 2), (fSize.height / 2) - (size.height / 2), size.width, size.height, 0, 0, size.width / 2, size.height / 2, null);

All advice is appreciated.

  • Jev.

jFrame.setPositionRelativeTo(null);

That should center the jFrame on the user’s screen.

I’m not trying to center the frame. It’s a full screen game, making it windowed would break the immersion.
I’m just trying to center the display, which is an Image.

  • Jev.

That doesn’t really make sense…
If you are drawing an image, figure out how your coordinate system is set up. If your origin is in the bottom left, draw with coords 0, 0. If you’re drawing from the top left, windowWidth, windowHeight. If your image is the same size as the window, it’ll draw it perfectly in the center. Other than that, I don’t know what you would want… If you want a image that isn’t the same size, get the width of the window and divide by 2, then do the same for the height. Could you show us some code?

Is this what you mean?

If fSide is the size of the screen and not of the panel or canvas you are drawing to then that would be the problem. Possibly you are confusing device and user coordinates.