简体   繁体   中英

How do I switch a JFrame from being decorated to being not decorated?

I want a game I am making to go fullscreen. I have the game being drawn in a JPanel and added to a JFrame.

I am currently using graphicsDevice.setFullScreenWindow(jframe) to set the JFrame as fullscreen.

The JFrame is decorated. When I set it fullscreen, there is space left for the decoration, but I want it to take up the whole screen. When I have the JFrame as undecorated, it takes up the whole screen when fullscreen.

I want the JFrame to be decorated when in windowed mode, but when in fullscreen mode, I want the screen to be completely used.

I have tried setting the JFrame to be not visible, then changing the decoration and setting it back to visible. I have also tried setting the original to have decoration, making a new JFrame with no decoration and fullscreen with the JPanel added.

I'm not sure but I think you cannot change the setUndecorated() property after a JFrame has become visible. As a result, one solution would be to have two frames. One windowed, one fullscreen. Swap between the frames when you want to toggle fullscreen.

Since you're drawing things onto a JPanel , the solution is to create a draw() method where the drawing will happen. Things do get a little more complicated if you wish to add Swing components though. Anyway, try this out.

Code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FullScreenExample {

    public static void main(String[] args) throws InterruptedException {
        new FullScreenExample();
    }

    public FullScreenExample() throws InterruptedException {
        // Full screen frame set-up.
        JFrame frameFullScr = new JFrame();
        frameFullScr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frameFullScr.setUndecorated(true);
        frameFullScr.add(new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                if(frameFullScr.isVisible()) draw(this, g);
            }
        });
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frameFullScr);

        // Windowed frame set-up.
        JFrame frameWin = new JFrame();
        frameWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frameWin.setSize(512, 512);
        frameWin.add(new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                if(frameWin.isVisible()) draw(this, g);
            }
        });

        Thread.sleep(5000);

        // Now let's go into a window...
        frameFullScr.setVisible(false);
        frameWin.setVisible(true);
    }

    private void draw(JComponent component, Graphics g) {
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, component.getWidth(), component.getHeight());
        g.setColor(Color.RED);
        g.drawOval(32, 32, 256, 256);
    }

}

Just some unrelated advice: In case you run into performance problems, try looking into AWT's BufferStrategy . It is more optimized better accelerated by the graphics card. You can expect a +25% to 75% improvement in framerates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM