Acquiring full screen in Java-applications

Inn a recent project I’ve been toying around with, the need to acquire the entire screen came up as a need. Being rather new to Java as I am, this took some time figuring out. I use the javax.swing-library to handle my GUI.

My first try was to ┬áset the JFrame undecorated, “always on top”, start at (x,y)=(0,0) and size=screen size, like this:

public class Window extends JFrame {
    Window() {
        // Not too important in this example
        this.setLayout(new GridLayout(1,1));

        // Start at top-left
        this.setLocation(0, 0);

        // Get screensize, set as window-size
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

       // I don't want it to look like a window

        // Woop-de-do

So when instantiating a new Window(), it will cover the entire screen. Almost. This will behave as expected in Windows (XP at least), but not in OS X. In OS X the dock will overlap, and the coordinates (0,0) will mean “first pixel _underneath_ the menubar” – obviously not what I want – and not ideal for this application.

The solution is to take over the entire screen, the same way as ie DVD Players, games and other presentation-apps. One way to do this in Java is like this:

import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;

public class Main {
    public static void main(String[] args) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices(); // There may be more than one screen-device
        // Can also get the default-screen directly with ge.getDefaultScreenDevice()
        window = new Window(); // Instantiating the class from above, to create a dummy-window
        // Setting device to full-screen
        gs[0].setFullScreenWindow(window); // In my case, only interested in the first screen

I suppose this is the correct way to do it always for Windows XP etc as this will actually take over the entire screen, not just overlapping the existing one.

Thanks goes to Jan-Petter for giving me a push in the correct direction.

Be Sociable, Share!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>