1

Im trying to create a mini game using java swing and i cant find any solution for a problem that using the same code, the app displays differently on different pcs. It displays like it should on 1366x768 resolution screens but stretches on full hd screens. ive tried changing resolution on the high res screens, tried changing icons/font sizes but with no luck. im adding 2 screenshots, 1 with how it suppose to look like and the other on high res screen. notice that for some reason, in addition to stretching the map, the high screen enlarges the toolbar on top of the map too.

how it suppose to look like: how it suppose to look like

how it looks on high res screens: how it looks on high res screens

Anton R
  • 21
  • 4

2 Answers2

1

To solve these problems you need to find the ratio of width to height for the screen. The images can then be scaled accordingly.

If you always want the images to be square then you would want to find whether the width or height available is the smallest and then scale it to fit.

What you're currently doing is taking the whole screen and stretching it to fit instead of taking the section you would like and fitting it all within it. Also image sizes should adjust if the screen size available is larger/smaller.

Michael
  • 3,411
  • 4
  • 25
  • 56
  • i think understand what you mean. if i set frame to be Resizable and just minimize the window, i can get it to be in the right shape however i have no idea how to make the window get the right shape from the start. The way im writing the code, i dont have the possibility of doing repack() every time i change a map. – Anton R Jun 23 '17 at 11:28
1

I'm guessing that your tiles occupy a GridLayout in BorderLayout.CENTER, the default layout and location for a JFrame. Resize this related example to see the effect with tiles having a fixed size, illustrated below. This might happen, for example, if you've setExtendedState() to MAXIMIZED_BOTH. You could try setResizable(false) on the enclosing JFrame to preclude the separation.

I have no idea how to make the window get the right shape from the start.

When you pack() the enclosing JFrame, it "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents." The example cited gets it's initial size from the SIZE of the icons comprising the tiles, but you can override getPreferredSize() to specify a different result.

Before:

image not stretched

After:

image stretched

trashgod
  • 203,806
  • 29
  • 246
  • 1,045