Java - Fullscreen and windowed mode

1.7k Views Asked by At

Is it possible that if i switch between the two modes and draw things to adjust them so that in windowed mode the things seem to be drawn as in the fullscreen mode but are actually smaller...

So that's my code:

Constructor: GraphicsEnvironment gfxEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();

GraphicsDevice gfxDevice = gfxEnvironment.getDefaultScreenDevice();

private boolean fullscreen = true; // not really like that

if(answer == JOptionPane.YES_OPTION) {

        setUndecorated(true);
        setAlwaysOnTop(true);
        DisplayMode getMode = gfxDevice.getDisplayMode();
        DisplayMode displayMode = new DisplayMode(getMode.getWidth(), getMode.getHeight(), getMode.getBitDepth(), getMode.getRefreshRate());
        gfxDevice.setFullScreenWindow(Frame.this);
        gfxDevice.setDisplayMode(displayMode);
        fullscreen = true;
    } else {
        setUndecorated(false);
        setVisible(true);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setResizable(false);
        fullscreen = false;
    }

Switch the screens: if(fullscreen) {

        dispose();
        setUndecorated(false);
        gfxDevice.setFullScreenWindow(null);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setVisible(true);
        fullscreen = false;
    } else {
        setVisible(false);
        dispose();
        setUndecorated(true);
        gfxDevice.setFullScreenWindow(Frame.this);
        fullscreen = true;
    }

draw: private void draw(Graphics g) {

    g.setColor(Color.RED);
    g.fillRect(0, 0, 400, 800);
}

So how can I adjust the Rect? Thanks:)

1

There are 1 best solutions below

3
On

You can base the drawing of the rect on the screen size. So base it off of a fraction of the window width and height with java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width; and java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height; instead of fixed pixel values.