Detect Which Screen Monitor Your Coordinates Belong To Using Java


Getting a screen’s size is simple using Toolkit class’ getScreenSize(). But what if you are working using more than 1 screen monitor, say a dual monitor? The ToolKit class won’t apply in this case. There are a few set of classes in Java that can do the trick namely GraphicsEnvironment and GraphicsDevice.

Let us say you have a coordinate and you want to know which screen monitor is belongs to (monitor 1 or monitor 2). This method will do just that.

1
2
3
4
5
public static int getDisplayLocation(int x, int y) {
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     GraphicsDevices[] gDevs = ge.getScreenDevices();
     Point p = new Point(x, y);
     for (int i=0; i

Found this post useful? Buy me a cup of coffee or subscribe to my RSS feeds and Google Friend Connect

Related Posts with Thumbnails

2 Responses to “Detect Which Screen Monitor Your Coordinates Belong To Using Java”

  1. 1
    Jason Says:

    Your declaration for GraphicsDevice is missing a variable and GraphicsDevice needs to be set to an array b/c ge.getScreenDevices() returns an array of screen device objects.

    Cheers!

  2. 2
    tech Says:

    you’re right. lolz. thanks for the typo correction

Leave a Reply

Spam protection by WP Captcha-Free