Get the real screen size, including multiple screens - Java 2D Graphics

Java examples for 2D Graphics:Screen

Description

Get the real screen size, including multiple screens

Demo Code


//package com.java2s;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

public class Main {
    /**//from  www .  java2  s. c o m
     * Get the real screen size, including multiple screens
     *
     * @return The screen size
     */
    public static Rectangle getScreenSize() {
        GraphicsEnvironment ge = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsDevice[] screens = ge.getScreenDevices();

        Rectangle allScreenBounds = new Rectangle();
        for (GraphicsDevice screen : screens) {
            Rectangle screenBounds = screen.getDefaultConfiguration()
                    .getBounds();

            allScreenBounds.width += screenBounds.width;
            allScreenBounds.height = Math.max(allScreenBounds.height,
                    screenBounds.height);

            if (screenBounds.x < allScreenBounds.y
                    || screenBounds.y < allScreenBounds.y) {
                allScreenBounds.x = Math.min(allScreenBounds.x,
                        screenBounds.x);
                allScreenBounds.y = Math.min(allScreenBounds.y,
                        screenBounds.y);
            }
        }
        return allScreenBounds;
    }
}

Related Tutorials