Java AWT/Swing: Fullscreen
Today I had faced a problem with creating fullscreen AWT/Swing windows or frames. Some Googling solved my problem, but it was not trivial as it looks first.
Trival part: setting Frame/JFrame fullscreen with GraphicsDevice’s setFullScreenWindow(Frame f) function.
So first one would extend a Frame, and adding this small snippet:
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gd.setFullScreenWindow(this);
It will work, however, the border will be still there. Also note that setFullScreenWindow will move to 0,0 and resize to fit screen in case of no full screen suppport present (you can check for this with isFullScreenSupport()). Removing decorations can be achieved by setResizable(false) and setUndecorated(true).
However, setUndecorated() works only when Frame is not displayable (check with isDisplayable()), thus if you extend one, it will fail. You need to instantiate a new frame and calling this method before setVisible().
Now we have:
Frame fr = new Frame();
fr.setResizable(false);
if (!fr.isDisplayable()) fr.setUndecorated(true);
// GraphicsDevice… (the same as above)
gd.setFullScreenWindow(fr);
fr.setVisible(true);
In theory this should be working, however, on MacOSX Tiger, the primary platform I do Java coding (on Linux I do C) this code still leaves the decorations on and displays a black block instead the Mac menubar. This was solved with adding implicit size definitions, such as fr.setSize(1024, 768) and fr.setLocation(0, 0).
I also tried the same codes on Windows, and the same states happened. Defining the size makes it working.
Note: on Mac the apple.awt.fakefullscreen property can be turned true for development purposes, it will mimic fullscreen functionality to the code, but still display a frame thus the application can be killed with a mouse.
Commenting is closed for this article.