Monkeybro10
Monkeybro10

Reputation: 257

Scaling a game with hard coded coordinates to fit any screen?

I've been working on a pretty basic java game for about a month now, and I've ran into a huge problem. It works fine on any monitor, as long as that monitor's resolution is above 1280 x 720, which is what I have the game set to run at. I didn't realize this was a problem until I ran it on my friend's computer and it didn't all fit on his screen. The real problem here is that I'm making sort of a 2D Top-Down shooter type of game, and the walls and powerups in each level all have hard-coded coordinates. I can't make everything relative to the screen size because not only would it be extremely hard, but it would also make the game vary in difficulty and even possibility with the screen resolution. I suppose one solution would be to make it run in 800 x 600 and just make all of the levels smaller, but that isn't as much space as I'd like to work with.

What I want to know is if there is a way for me to scale my program to fit the monitor. For example, if I ran is on a 4:3 monitor, such as my friend's 1024x768, I want the program to fit itself to the screen proportionally, (so it would have black bars at the top and bottom like a widescreen video on a 4:3 monitor) but I also want all of my walls and powerups to be placed where the should be in respect to the newly scaled screen.

Is there some kind of way to "stretch" or "shrink" pixels in a program so everything appears in the window nice and scaled? Even if it isn't literally. I appreciate any help.

Upvotes: 1

Views: 1377

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109547

You can scale on painting, assuming you use the standard Graphics/Graphics2D:

private static final double SCALE = 0.75;
private static final double INV_SCALE = 1.0 / SCALE;

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.scale(SCALE, SCALE);
    ...
    g2.scale(INV_SCALE, INV_SCALE);

}

The scale factor (here SCALE) is of course a bit more difficult to set.

The mouse coordinates one has to scale oneself.

Upvotes: 1

MrSmith42
MrSmith42

Reputation: 10151

Use a logic resolution in your code and scale it while you make the output.

Upvotes: 0

Related Questions