Search code examples
javaframefullscreenborderlesswindowed

How do I set my frames to Borderless Fullscreen, Windowed Fullscreen, and Fullscreen in Java?


I use GraphicsDevice to fullscreen my Java Window, but I don't know how to change these settings while the application is running.

public void act(java.awt.Frame window) {
        if (state == SCREEN.FULLSCREEN) {
            if (ev.isFullScreenSupported()) {
                isFullScreen = true;
                window.dispose();
                ev.setFullScreenWindow(window);
                window.setVisible(true);
            }
        }
        if (state == SCREEN.WINDOWED) {
            isFullScreen = false;
            window.dispose();
            ev.setFullScreenWindow(null);
            window.setUndecorated(false);
            window.setVisible(true);
        }
        if (state == SCREEN.BORDERLESS) {
            isFullScreen = true;
            window.dispose();
            ev.setFullScreenWindow(null);
            window.setUndecorated(true);
            window.setVisible(true);
        }
    }

When I run this infinitely, the code just creates a new window over and over, but I want to make it so that the screen changes depending on the value of state.


Solution

  • Although I had to change the parameters around, it was rather simple. For the fields, you need:

        public enum ScreenState {
            fullscreen, borderless, windowed, none
        };
        public static ScreenState current;
        private static GraphicsEnvironment env = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        private static GraphicsDevice ev = env.getDefaultScreenDevice();
    

    And for the method you can do something like:

    public void changeWindow(ScreenState applied, JFrame frame) {
    
            if (applied == ScreenState.fullscreen && current != ScreenState.fullscreen) {
                if (ev.isFullScreenSupported()) {
                    ev.setFullScreenWindow(frame);
                }
                current =  ScreenState.fullscreen;
            }
            if (applied == ScreenState.borderless && current != ScreenState.borderless) {
                frame.setUndecorated(true);
                frame.setExtendedState(Frame.MAXIMIZED_BOTH);
                current =  ScreenState.borderless;
            }
            if (applied == ScreenState.windowed && current != ScreenState.windowed) {
                frame.setUndecorated(false);
                // you can choose to make the screen fit or not
                frame.setExtendedState(Frame.MAXIMIZED_BOTH);
                current =  ScreenState.windowed;
            }
        }
    

    Thank you for the ones spending some time to help me out!