0

I set the size of my window as 640 x 1136, and I'm running the program on a 13-inch MBP, which has a resolution of 2560 x 1600. I'm assuming that the window should be able to fully displayed on my screen, but it only shows the top half.

Here's my code.

public static final int WIDTH = 640;  // width of window
public static final int HEIGHT = 1136; // height of window

public static void main(String[] args) {
    JFrame frame = new JFrame();
    World world = new World();
    frame.add(world);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(WIDTH, HEIGHT);
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); // 1) set window visible 2) paint() asap
    world.action(); // start the program
}

Here's how it looks like on my screen:
enter image description here

I tried to play around with the parameters and I found that when the HEIGHT is around 800, the window would exceed the screen. How can the max window height be only 1/2 of the resolution of the screen?

And is there a way to increase or decrease the size of the window according to the screen size automatically while keeping everything in scale?

Thanks!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
GlenXoseph
  • 113
  • 1
  • 11
  • 1
    Maybe this _Stack Overflow_ question will help: [Get effective screen size from java](https://stackoverflow.com/questions/10123735/get-effective-screen-size-from-java) – Abra Jun 16 '19 at 18:14
  • 1
    Possible duplicate of [How can I get screen resolution in java?](https://stackoverflow.com/questions/3680221/how-can-i-get-screen-resolution-in-java) – Yevgen Jun 16 '19 at 18:14
  • @Eugene Thanks, but the images in my program have fixed sizes, according to the answers, wouldn't that still require a different set of height and width for different screens? Then everything wouldn't be in scale anymore. Is there a way to set the window size while keeping everything in scale? – GlenXoseph Jun 16 '19 at 18:29
  • @Abra Thanks, but the images in my program have fixed sizes, according to the answers, wouldn't that still require a different set of height and width for different screens? Then everything wouldn't be in scale anymore. Is there a way to set the window size while keeping everything in scale? – GlenXoseph Jun 16 '19 at 18:30

1 Answers1

0

You have to get screen size at runtime and adjust frame size accordingly. This will allow your program to look identically on different screens.

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(dim.width / 2, dim.height / 2);
Yevgen
  • 1,576
  • 1
  • 15
  • 17