0

We are currently doing a game project using Eclipse. We started the project on my friend's Mac and the code worked how it should. If we start the program on any other windows device it doesn't work properly anymore. The idea behind the code is, that the Window always has the same resolution so the pictures and everything don't get distorted.

frmwidth and frmheight are the starting values ​​for the height and the width of the window.

Don't get distracted by the 27, it's because the aspect ratio isn't perfectly 16:9.

Does someone have an idea why it doesn't work on windows?

public void componentResized(ComponentEvent e) {
        int height = ((JFrame)e.getSource()).getHeight();
        int width = ((JFrame)e.getSource()).getWidth();

        if(height != frmheight && width != frmwidth){
            ((JFrame)e.getSource()).setSize(frmwidth, frmheight);
            
        }else if(height != frmheight) {
            frmwidth = ((height - 27) / 9) * 16;
            frmheight = height;
            ((JFrame)e.getSource()).setSize(frmwidth, frmheight);
             Hintergrund.anpassen(frmheight, frmwidth);
        }else {
            frmwidth = width;
            frmheight = ((width / 16) * 9) + 27;
            ((JFrame)e.getSource()).setSize(frmwidth, frmheight);
            Hintergrund.anpassen(frmheight, frmwidth);
        }
        ((JFrame)e.getSource()).setSize(frmwidth, frmheight);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Earny
  • 1
  • 1
  • `27` and `16` are magic numbers. I expect they relate to the size of the title bar of the frame. Don't use magic numbers. The problem here can be solved by setting the preferred size of the component that is displaying the images, then packing the frame to fit that. See also this [chess board](https://stackoverflow.com/a/21142687/418556). It ensures the chess board remains exactly 1:1 in height & width, within a larger GUI that can be resized. – Andrew Thompson May 26 '22 at 06:52
  • 1
    Generally, it’s a bad idea to modify the source component from within a listener while an event is delivered, especially regarding the very property you’re listening to. Besides that, you should rethink your logic. Regardless of which condition is fulfilled, you’re calling `setSize` even twice. Another useful thing would be to provide more problem details than “doesn't work properly”. – Holger Jun 17 '22 at 10:49

1 Answers1

0

The look and feel of the swing components is respecting the operating system's conditions. I've had issues with JTabbedPane. In Windows it worked as intended but on macos it was rendered differently. The properties of the swing components can be overridden by inheriting from the corresponding ComponentUI class.

Dan Chirita
  • 119
  • 9