0

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.

Tgwizman
  • 1,469
  • 2
  • 19
  • 34
  • use [these codes](http://stackoverflow.com/a/11570414/4101906) – Rahmat Waisi Jul 24 '16 at 05:34
  • there is a method named `setUndecorated(false/true)` for frames, but it must be used once and before your frame goes visible, after it you should add the main jpanel of game to new frame [check this answer](http://stackoverflow.com/a/876954/4101906) for [this question](http://stackoverflow.com/q/875132/4101906) – Rahmat Waisi Jul 24 '16 at 05:37
  • When I do, java throws `Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.` – Tgwizman Jul 24 '16 at 05:40
  • [read this](http://stackoverflow.com/a/18068923/4101906) – Rahmat Waisi Jul 24 '16 at 05:47
  • as i said `setUndecorated` can not be used after frame displayed, but there is a trick, here is the [solution](http://stackoverflow.com/a/876954/4101906) – Rahmat Waisi Jul 24 '16 at 05:49

1 Answers1

1

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.

Roland
  • 612
  • 5
  • 17