get Screen Dimension - Java Swing

Java examples for Swing:Screen

Description

get Screen Dimension

Demo Code


//package com.java2s;
import java.awt.*;

public class Main {
    public static Dimension getScreenDimension(Component container) {
        if (container != null) {
            Dimension d = new Dimension(0, 0);
            int scrID = getScreenID(container);
            if (scrID > 0) {
                GraphicsEnvironment ge = GraphicsEnvironment
                        .getLocalGraphicsEnvironment();
                DisplayMode mode = ge.getScreenDevices()[scrID - 1]
                        .getDisplayMode();
                d.setSize(mode.getWidth(), mode.getHeight());
            }/*w w w .  j  a v  a 2 s.c o m*/
            return d;
        } else {
            return Toolkit.getDefaultToolkit().getScreenSize();
        }
    }

    private static int getScreenID(Component container) {
        int scrID = 1;
        GraphicsEnvironment ge = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsDevice[] gd = ge.getScreenDevices();
        for (int i = 0; i < gd.length; i++) {
            GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
            Rectangle r = gc.getBounds();
            if (r.contains(container.getLocation())) {
                scrID = i + 1;
            }
        }
        return scrID;
    }
}

Related Tutorials