Java examples for Swing:Screen
Modifies the position of rect so that it is completely on screen if that is possible.
/*//from ww w.ja v a 2 s . c om * @(#)PortingUtils.java 4/12/2006 * * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved. */ //package com.java2s; import java.awt.*; import java.awt.geom.Area; import java.util.ArrayList; import java.util.List; public class Main { /** * Modifies the position of rect so that it is completely on screen if that is possible. By default, it will allow * the rect to cross two screens. You can call {@link #ensureOnScreen(java.awt.Rectangle, boolean)} and set the * second parameter to false if you don't want to allow that case. * * @param rect The rectangle to be moved to a single screen * @return rect after its position has been modified */ public static Rectangle ensureOnScreen(Rectangle rect) { return ensureOnScreen(rect, true); } /** * Modifies the position of rect so that it is completely on screen if that is possible. * * @param rect The rectangle to be moved to a single screen * @param allowCrossScreen a flag to allow or disallow when the rect is cross two screens. * @return rect after its position has been modified */ public static Rectangle ensureOnScreen(Rectangle rect, boolean allowCrossScreen) { // optimize it so that it is faster for most cases Rectangle localScreenBounds = getLocalScreenBounds(); if (localScreenBounds.contains(rect)) { return rect; } final Rectangle[] SCREENS = getScreens(); // check if rect is total on screen if (allowCrossScreen && getScreenArea().contains(rect)) return rect; // see if the top left is on any of the screens Rectangle containingScreen = null; Point rectPos = rect.getLocation(); for (Rectangle screenBounds : SCREENS) { if (screenBounds.contains(rectPos)) { containingScreen = screenBounds; break; } } // if not see if rect partial on any screen for (Rectangle screenBounds : SCREENS) { if (screenBounds.intersects(rect)) { containingScreen = screenBounds; break; } } // check if it was on any screen if (containingScreen == null) { // it was not on any of the screens so center it on the first screen rect.x = (SCREENS[0].width - rect.width) / 2; rect.y = (SCREENS[0].height - rect.height) / 2; return rect; } else { // move rect so it is completely on a single screen // check X int rectRight = rect.x + rect.width; int screenRight = containingScreen.x + containingScreen.width; if (rectRight > screenRight) { rect.x = screenRight - rect.width; } if (rect.x < containingScreen.x) rect.x = containingScreen.x; // check Y int rectBottom = rect.y + rect.height; int screenBottom = containingScreen.y + containingScreen.height; if (rectBottom > screenBottom) { rect.y = screenBottom - rect.height; } if (rect.y < containingScreen.y) rect.y = containingScreen.y; // return corrected rect return rect; } } /** * Gets the local monitor's screen bounds. * * @return the screen bounds. */ public static Rectangle getLocalScreenBounds() { GraphicsEnvironment e = GraphicsEnvironment .getLocalGraphicsEnvironment(); return e.getMaximumWindowBounds(); } private static Rectangle[] getScreens() { GraphicsEnvironment environment = GraphicsEnvironment .getLocalGraphicsEnvironment(); List<Rectangle> screensList = new ArrayList<Rectangle>(); GraphicsDevice[] screenDevices = environment.getScreenDevices(); for (GraphicsDevice device : screenDevices) { GraphicsConfiguration configuration = device .getDefaultConfiguration(); Rectangle screenBounds = configuration.getBounds(); screensList.add(screenBounds); } return screensList.toArray(new Rectangle[screensList.size()]); } /** * Get screen area of all monitors. * * @return Union of all screens */ public static Area getScreenArea() { Area SCREEN_AREA = new Area(); GraphicsEnvironment environment = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice[] screenDevices = environment.getScreenDevices(); for (GraphicsDevice device : screenDevices) { GraphicsConfiguration configuration = device .getDefaultConfiguration(); Rectangle screenBounds = configuration.getBounds(); SCREEN_AREA.add(new Area(screenBounds)); } return SCREEN_AREA; } }