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 |









December 18th, 2009 at 10:38 am
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!
December 18th, 2009 at 10:40 am
you’re right. lolz. thanks for the typo correction