SwingUtil.java Source code

Java tutorial

Introduction

Here is the source code for SwingUtil.java

Source

//package com.javadocking.util;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;

/**
 * This class contains a collection of static utility methods for Swing.
 * 
 * @author Heidi Rakels.
 */
public class SwingUtil {
    /**
     * Verifies if the given point is visible on the screen.
     * 
     * @param    location       The given location on the screen.
     * @return                True if the location is on the screen, false otherwise.
     */
    public static boolean isLocationInScreenBounds(Point location) {

        // Check if the location is in the bounds of one of the graphics devices.
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
        Rectangle graphicsConfigurationBounds = new Rectangle();

        // Iterate over the graphics devices.
        for (int j = 0; j < graphicsDevices.length; j++) {

            // Get the bounds of the device.
            GraphicsDevice graphicsDevice = graphicsDevices[j];
            graphicsConfigurationBounds.setRect(graphicsDevice.getDefaultConfiguration().getBounds());

            // Is the location in this bounds?
            graphicsConfigurationBounds.setRect(graphicsConfigurationBounds.x, graphicsConfigurationBounds.y,
                    graphicsConfigurationBounds.width, graphicsConfigurationBounds.height);
            if (graphicsConfigurationBounds.contains(location.x, location.y)) {

                // The location is in this screengraphics.
                return true;

            }

        }

        // We could not find a device that contains the given point.
        return false;

    }
}