java - Windows 下的 Java 多显示处理 - 缩放显示的错误?
问题描述
tl;博士
在 Windows 10 下,如果我将辅助显示器放在主显示器的右侧,并对辅助显示器应用缩放(例如 150%),则显示坐标(由 Java API 返回)重叠而不是让显示边界并排坐着。换句话说,如果我将鼠标从主屏幕的左边缘缓慢移动到次屏幕的右边缘,Java 的 APIMouseInfo.getPointerInfo().getLocation()
返回一个从 0 到 1920 递增的 X 位置,然后一旦光标进入第二个屏幕,值就会跳跃回落至 1280,然后再次上涨至 2560。因此,针对不同区域,两次返回 1280-1920 范围。
在帖子的末尾,我包含了一个(更新的)演示,使问题显而易见。不要犹豫,尝试并报告。
长版:
这篇文章提供了(太多)上下文,但也旨在分享我在搜索该主题时学到的东西。
首先,为什么要打扰?因为我正在用 Java 构建一个屏幕捕获应用程序,它需要正确处理多显示器配置,包括应用 Windows 缩放功能的显示器。
使用 Java API GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
( .
以下图片是使用帖子末尾的代码制作的。
例如,如果我们有 2 个全高清显示器,主显示器的左上角位于 (0,0),而...
- 如果辅助位于其右侧,在同一级别,则其左上角为 (1920,0):
- 如果辅助节点位于其左侧,在同一级别,则其左上角为 (-1920,0):
- 如果辅助位于下方,水平对齐,则其左上角为 (0,1080):
- 如果辅助位于上方,水平对齐,则其左上角为 (0,-1080):
- 如果显示未对齐,依此类推:
- 或不同的分辨率:
但是,如果缩放辅助显示器,事情就会出错:似乎缩放因子不仅适用于它的尺寸,而且还适用于它的origin,它更接近 (0,0)。
如果二级在左边,这是有道理的。例如,当次要 1920x1080 缩放为 150% 时,它会使逻辑 1280x720 定位在 (-1280,0):
但是如果第二个在右边,原点也被缩放到(1280,0),越来越接近原点并导致它“重叠”主要的:
换句话说,如果鼠标位于 (1800,0) - 请参见上面的红点 - 我无法知道它实际上是位于第一个显示器的右侧(距右边缘 120 像素)还是左侧次要的(在左边缘的 520 像素处)。在这种情况下,当将鼠标从主显示器移动到辅助显示器时,鼠标的 X 位置在到达主显示器的边界时会“跳回”。
在屏幕上定位窗口也是如此。如果我将对话框的 X 位置设置为 1800,我无法知道它将在哪里打开。
经过大量浏览后,像这样的一些答案表明查询 Windows 缩放的唯一方法是使用本机调用。实际上,使用 JNA,可以获得显示器的物理尺寸(尽管答案似乎表明调用应该返回逻辑尺寸)。即 JNA 调用忽略缩放因子,并且在缩放为 100% 时的行为与 Java API 完全相同:
那么我错过了什么吗?
不知道缩放因子是一个小问题,但无法判断鼠标在哪个显示器上,或者无法在我想要的显示器上定位窗口对我来说似乎是一个真正的问题。它是 Java 错误吗?
注意:这是上面使用的应用程序的代码,在 Windows 10 64b 上使用 OpenJDK14 运行。它显示了 Java 感知的显示设置和鼠标位置的缩小版本。如果您在小矩形内单击并拖动,它还可以在真实屏幕上放置和移动一个小对话框。信用: UI 的灵感来自于此处发布的 WheresMyMouse 代码。
照原样,代码仅使用 Java API。如果要与 JNA 进行比较,请搜索标记为“JNA_ONLY”的 4 个块,取消注释,然后添加 jna 库。然后,该演示将在 JNA 和 Java API 之间切换,以在每次右键单击时显示屏幕边界和鼠标光标。此版本中对话框定位从不使用 JNA。
// JNA_ONLY
//import com.sun.jna.platform.win32.User32;
//import com.sun.jna.platform.win32.WinDef;
//import com.sun.jna.platform.win32.WinUser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
/**
* Java multi-display detection and analysis.
* UI idea based on WheresMyMouse - https://stackoverflow.com/a/21592711/13551878
*/
public class ShowDisplays {
private static boolean useJna = false;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new JFrame("Display Configuration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
public static class TestPane extends JPanel {
private List<Rectangle> screenBounds;
JDialog dlg;
public TestPane() {
screenBounds = getScreenBounds();
// refresh screen details every second to reflect changes in Windows Preferences in "real time"
new Timer(1000, e -> screenBounds = getScreenBounds()).start();
// Refresh mouse position at 25fps
new Timer(40, e -> repaint()).start();
MouseAdapter mouseAdapter = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() != MouseEvent.BUTTON1) {
useJna = !useJna;
repaint();
}
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println(e.getButton());
if (e.getButton() == MouseEvent.BUTTON1) {
if (!dlg.isVisible()) {
dlg.setVisible(true);
}
moveDialogTo(e.getPoint());
}
}
@Override
public void mouseDragged(MouseEvent e) {
moveDialogTo(e.getPoint());
}
private void moveDialogTo(Point mouseLocation) {
final Rectangle surroundingRectangle = getSurroundingRectangle(screenBounds);
double scaleFactor = Math.min((double) getWidth() / surroundingRectangle.width, (double) getHeight() / surroundingRectangle.height);
int xOffset = (getWidth() - (int) (surroundingRectangle.width * scaleFactor)) / 2;
int yOffset = (getHeight() - (int) (surroundingRectangle.height * scaleFactor)) / 2;
int screenX = surroundingRectangle.x + (int) ((mouseLocation.x - xOffset) / scaleFactor);
int screenY = surroundingRectangle.y + (int) ((mouseLocation.y - yOffset) / scaleFactor);
dlg.setLocation(screenX - dlg.getWidth() / 2, screenY - dlg.getHeight() / 2);
}
};
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
// Prepare the test dialog
dlg = new JDialog();
dlg.setTitle("Here");
dlg.setSize(50, 50);
dlg.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
// Mouse position
Point mousePoint = getMouseLocation();
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
final Rectangle surroundingRectangle = getSurroundingRectangle(screenBounds);
double scaleFactor = Math.min((double) getWidth() / surroundingRectangle.width, (double) getHeight() / surroundingRectangle.height);
int xOffset = (getWidth() - (int) (surroundingRectangle.width * scaleFactor)) / 2;
int yOffset = (getHeight() - (int) (surroundingRectangle.height * scaleFactor)) / 2;
g2d.setColor(Color.BLUE);
g2d.fillRect(xOffset, yOffset, (int) (surroundingRectangle.width * scaleFactor), (int) (surroundingRectangle.height * scaleFactor));
Font defaultFont = g2d.getFont();
for (int screenIndex = 0; screenIndex < screenBounds.size(); screenIndex++) {
Rectangle screen = screenBounds.get(screenIndex);
Rectangle scaledRectangle = new Rectangle(
xOffset + (int) ((screen.x - surroundingRectangle.x) * scaleFactor),
yOffset + (int) ((screen.y - surroundingRectangle.y) * scaleFactor),
(int) (screen.width * scaleFactor),
(int) (screen.height * scaleFactor));
// System.out.println(screen + " x " + scaleFactor + " -> " + scaledRectangle);
g2d.setColor(Color.DARK_GRAY);
g2d.fill(scaledRectangle);
g2d.setColor(Color.GRAY);
g2d.draw(scaledRectangle);
// Screen text details
g2d.setColor(Color.WHITE);
// Display number
final Font largeFont = new Font(defaultFont.getName(), defaultFont.getStyle(), (int) (screen.height * scaleFactor) / 2);
g2d.setFont(largeFont);
String label = String.valueOf(screenIndex + 1);
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout layout = new TextLayout(label, largeFont, frc);
Rectangle2D bounds = layout.getBounds();
g2d.setColor(Color.WHITE);
g2d.drawString(
label,
(int) (scaledRectangle.x + (scaledRectangle.width - bounds.getWidth()) / 2),
(int) (scaledRectangle.y + (scaledRectangle.height + bounds.getHeight()) / 2)
);
// Resolution + corner
final Font smallFont = new Font(defaultFont.getName(), defaultFont.getStyle(), (int) (screen.height * scaleFactor) / 10);
g2d.setFont(smallFont);
// Resolution
String resolution = screen.width + "x" + screen.height;
layout = new TextLayout(resolution, smallFont, frc);
bounds = layout.getBounds();
g2d.drawString(
resolution,
(int) (scaledRectangle.x + (scaledRectangle.width - bounds.getWidth()) / 2),
(int) (scaledRectangle.y + scaledRectangle.height - bounds.getHeight())
);
// Corner
String corner = "(" + screen.x + "," + screen.y + ")";
g2d.drawString(
corner,
scaledRectangle.x,
(int) (scaledRectangle.y + bounds.getHeight() * 1.5)
);
}
g2d.setFont(defaultFont);
FontMetrics fm = g2d.getFontMetrics();
if (mousePoint != null) {
g2d.fillOval(xOffset + (int) ((mousePoint.x - surroundingRectangle.x) * scaleFactor) - 2,
yOffset + (int) ((mousePoint.y - surroundingRectangle.y) * scaleFactor) - 2,
4,
4
);
g2d.drawString("Mouse pointer is at (" + mousePoint.x + "," + mousePoint.y + ")", 4, fm.getHeight());
}
g2d.drawString("Click and drag in this area to move a dialog on the actual screens", 4, fm.getHeight() * 2);
// JNA_ONLY
// g2d.drawString("Now using " + (useJna ? "JNA" : "Java API") + ". Right-click to toggle", 4, fm.getHeight() * 3);
g2d.dispose();
}
}
public static Rectangle getSurroundingRectangle(List<Rectangle> screenRectangles) {
Rectangle surroundingBounds = null;
for (Rectangle screenBound : screenRectangles) {
if (surroundingBounds == null) {
surroundingBounds = new Rectangle(screenRectangles.get(0));
}
else {
surroundingBounds.add(screenBound);
}
}
return surroundingBounds;
}
private static Point getMouseLocation() {
// JNA_ONLY
// if (useJna) {
// final WinDef.POINT point = new WinDef.POINT();
// if (User32.INSTANCE.GetCursorPos(point)) {
// return new Point(point.x, point.y);
// }
// else {
// return null;
// }
// }
return MouseInfo.getPointerInfo().getLocation();
}
public static List<Rectangle> getScreenBounds() {
List<Rectangle> screenBounds;
// JNA_ONLY
// if (useJna) {
// screenBounds = new ArrayList<>();
// // Enumerate all monitors, and call a code block for each of them
// // See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaymonitors
// // See http://www.pinvoke.net/default.aspx/user32/EnumDisplayMonitors.html
// User32.INSTANCE.EnumDisplayMonitors(
// null, // => the virtual screen that encompasses all the displays on the desktop.
// null, // => don't clip the region
// (hmonitor, hdc, rect, lparam) -> {
// // For each found monitor, get more information
// // See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmonitorinfoa
// // See http://www.pinvoke.net/default.aspx/user32/GetMonitorInfo.html
// WinUser.MONITORINFOEX monitorInfoEx = new WinUser.MONITORINFOEX();
// User32.INSTANCE.GetMonitorInfo(hmonitor, monitorInfoEx);
// // Retrieve its coordinates
// final WinDef.RECT rcMonitor = monitorInfoEx.rcMonitor;
// // And convert them to a Java rectangle, to be added to the list of monitors
// screenBounds.add(new Rectangle(rcMonitor.left, rcMonitor.top, rcMonitor.right - rcMonitor.left, rcMonitor.bottom - rcMonitor.top));
// // Then return "true" to continue enumeration
// return 1;
// },
// null // => No additional info to pass as lparam to the callback
// );
// return screenBounds;
// }
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices();
screenBounds = new ArrayList<>(screenDevices.length);
for (GraphicsDevice screenDevice : screenDevices) {
GraphicsConfiguration configuration = screenDevice.getDefaultConfiguration();
screenBounds.add(configuration.getBounds());
}
return screenBounds;
}
}
解决方案
这看起来像您遇到了错误JDK-8211999的表现:
在涉及一个 HiDPI 屏幕的多显示器设置中,一个 HiDPI 屏幕放置在一台常规显示器的右侧,在 Windows 10 上,返回的边界
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[x].getDefaultConfiguration().getBounds()
是重叠的。这会导致各种次要错误......
评论指出:
Linux 上也存在相同的错误,macOS 不受影响。
似乎没有简单的纯 Java 解决方法。
已经提出了一个适用于 Windows的修复程序,甚至不尝试在 Java 中进行坐标数学运算,而是将解决方案委托给本机代码。
由于使用 JNA(本机)实现似乎可行,这似乎是 JDK 版本 9 到 15 的最佳方法。该错误已在 JDK16 中修复。
根据错误报告,它会影响 JDK 9+,因此恢复到 JDK 8 可能会解决问题,尽管我看到了冲突的帐户。
推荐阅读
- flutter - 如何在 SyncFusion Maps Flutter 中设置标记的 z-index?
- c++ - 如何在 C++ 中创建指向函数的指针并为该函数定义一些参数
- oauth-2.0 - Prometheus 使用 oauth2 抓取会导致“未解析的变量:request.header.Authorization”
- maven - 调试缓存:45 - 无法在缓存中找到用于 log4j 使用的日志中的范围报告的模板
- opencv - Orbbec 和 opencv 统一。地板上的物体检测
- .htaccess - .htaccess 和 $_GET 问题
- python - 使用python在mysql中加载数据时出错
- sql-server - 如何在容器启动时运行命令,而不覆盖原始图像的命令?
- stream - 视频通话和互动直播的区别?
- text - 反转希伯来字符