Renamed getMonitor methods (so the name makes more sense). Added
wrappers for getting monitor at mouse location.
This commit is contained in:
parent
99bd330730
commit
9d177a779b
@ -30,7 +30,7 @@ public final
|
|||||||
class ScreenUtil {
|
class ScreenUtil {
|
||||||
public static
|
public static
|
||||||
Rectangle getScreenBoundsAt(Point pos) {
|
Rectangle getScreenBoundsAt(Point pos) {
|
||||||
GraphicsDevice gd = getGraphicsDeviceAt(pos);
|
GraphicsDevice gd = getMonitorAtLocation(pos);
|
||||||
Rectangle bounds = null;
|
Rectangle bounds = null;
|
||||||
|
|
||||||
if (gd != null) {
|
if (gd != null) {
|
||||||
@ -42,7 +42,15 @@ class ScreenUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static
|
public static
|
||||||
GraphicsDevice getGraphicsDeviceAt(Point pos) {
|
GraphicsDevice getMonitorAtMouseLocation() {
|
||||||
|
Point mouseLocation = MouseInfo.getPointerInfo()
|
||||||
|
.getLocation();
|
||||||
|
|
||||||
|
return getMonitorAtLocation(mouseLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static
|
||||||
|
GraphicsDevice getMonitorAtLocation(Point pos) {
|
||||||
|
|
||||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
GraphicsDevice screenDevices[] = ge.getScreenDevices();
|
GraphicsDevice screenDevices[] = ge.getScreenDevices();
|
||||||
@ -65,13 +73,41 @@ class ScreenUtil {
|
|||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static
|
||||||
|
int getMonitorNumberAtMouseLocation() {
|
||||||
|
Point mouseLocation = MouseInfo.getPointerInfo()
|
||||||
|
.getLocation();
|
||||||
|
|
||||||
|
return getMonitorNumberAtLocation(mouseLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static
|
||||||
|
int getMonitorNumberAtLocation(Point pos) {
|
||||||
|
|
||||||
|
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
GraphicsDevice screenDevices[] = ge.getScreenDevices();
|
||||||
|
|
||||||
|
for (int i = 0; i < screenDevices.length; i++) {
|
||||||
|
final GraphicsDevice device1 = screenDevices[i];
|
||||||
|
GraphicsConfiguration gc = device1.getDefaultConfiguration();
|
||||||
|
Rectangle screenBounds = gc.getBounds();
|
||||||
|
|
||||||
|
if (screenBounds.contains(pos)) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// we are the primary monitor, so return 0.
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
public static
|
public static
|
||||||
void showOnSameScreenAsMouse_Center(final Container frame) {
|
void showOnSameScreenAsMouse_Center(final Container frame) {
|
||||||
Point mouseLocation = MouseInfo.getPointerInfo()
|
Point mouseLocation = MouseInfo.getPointerInfo()
|
||||||
.getLocation();
|
.getLocation();
|
||||||
|
|
||||||
GraphicsDevice deviceAtMouse = ScreenUtil.getGraphicsDeviceAt(mouseLocation);
|
GraphicsDevice monitorAtMouse = ScreenUtil.getMonitorAtLocation(mouseLocation);
|
||||||
Rectangle bounds = deviceAtMouse.getDefaultConfiguration()
|
Rectangle bounds = monitorAtMouse.getDefaultConfiguration()
|
||||||
.getBounds();
|
.getBounds();
|
||||||
frame.setLocation(bounds.x + bounds.width / 2 - frame.getWidth() / 2, bounds.y + bounds.height / 2 - frame.getHeight() / 2);
|
frame.setLocation(bounds.x + bounds.width / 2 - frame.getWidth() / 2, bounds.y + bounds.height / 2 - frame.getHeight() / 2);
|
||||||
}
|
}
|
||||||
@ -81,9 +117,10 @@ class ScreenUtil {
|
|||||||
Point mouseLocation = MouseInfo.getPointerInfo()
|
Point mouseLocation = MouseInfo.getPointerInfo()
|
||||||
.getLocation();
|
.getLocation();
|
||||||
|
|
||||||
GraphicsDevice deviceAtMouse = ScreenUtil.getGraphicsDeviceAt(mouseLocation);
|
GraphicsDevice monitorAtMouse = ScreenUtil.getMonitorAtLocation(mouseLocation);
|
||||||
frame.setLocation(deviceAtMouse.getDefaultConfiguration()
|
Rectangle bounds = monitorAtMouse.getDefaultConfiguration()
|
||||||
.getBounds().x, frame.getY());
|
.getBounds();
|
||||||
|
frame.setLocation(bounds.x, bounds.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
private
|
private
|
||||||
|
Loading…
Reference in New Issue
Block a user