Verifies if the given point is visible on the screen. - Java 2D Graphics

Java examples for 2D Graphics:Screen

Description

Verifies if the given point is visible on the screen.

Demo Code


//package com.java2s;

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

public class Main {
    /**/*from  ww  w  . jav  a 2s.c  o m*/
     * 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;

    }
}

Related Tutorials