Java:获取一个/所有可用显示器(而不是整个桌面)的分辨率?

发布于 2024-07-19 09:07:31 字数 371 浏览 9 评论 0原文

我有两个不同尺寸的显示器,使用(我相信)TwinView 连接在一起。

我尝试

System.out.println(Toolkit.getDefaultToolkit().getScreenSize());

得出

java.awt.Dimension[width=2960,height=1050]

如果将两个显示器放在一起

以下结论是正确的。 相反,我希望能够实现以下之一

  • 获取当前显示器的分辨率
  • 获取主显示器的分辨率

I have two different-sized monitors, connected together using (I believe) TwinView.

I tried

System.out.println(Toolkit.getDefaultToolkit().getScreenSize());

and get

java.awt.Dimension[width=2960,height=1050]

which is true if you count both monitors together.

Instead of this, I would like to be able achieving one of the following:

  • getting resolution of the current monitor
  • getting resolution of the main monitor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

垂暮老矣 2024-07-26 09:07:31

您需要使用GraphicsEnvironment

特别是, getScreenDevices() 返回 GraphicsDevice 可以从中读取显示模式的宽度/高度的对象。

例子:

GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = g.getScreenDevices();

for (int i = 0; i < devices.length; i++) {
    System.out.println("Width:" + devices[i].getDisplayMode().getWidth());
    System.out.println("Height:" + devices[i].getDisplayMode().getHeight());
} 

you'll want to use the GraphicsEnvironment.

In particular, getScreenDevices() returns an array of GraphicsDevice objects from which you can read the width/height of the display mode.

Example:

GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = g.getScreenDevices();

for (int i = 0; i < devices.length; i++) {
    System.out.println("Width:" + devices[i].getDisplayMode().getWidth());
    System.out.println("Height:" + devices[i].getDisplayMode().getHeight());
} 
云裳 2024-07-26 09:07:31

当我试图实现一种在单个屏幕截图中捕获任何给定计算机上的所有屏幕的方法时,我遇到了这个线程,并且无法找到如何在其他地方完成此操作的答案。

就我而言,Toolkit.getDefaultToolkit().getScreenSize() 只会返回主显示器的分辨率。 我使用以下方法来创建一个矩形,该矩形的原点位于所有监视器之间的最左上角,并且足够大以捕获 GraphicsEnvironment 提供的所有监视器上的所有内容。 getLocalGraphicsEnvironment().getScreenDevices() 当传递给 Robot().createScreenCapture 时。

private Rectangle allScreensRectangle()
{
    // make array of all screens
    GraphicsDevice[] screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    
    // start out with the rectangle of the primary monitor
    Rectangle allScreens = screens[0].getDefaultConfiguration().getBounds();
    for (int i = 1; i < screens.length; i++)
    {
        Rectangle screenRect = screens[i].getDefaultConfiguration().getBounds();

        // expand the size of the capture region to include any area in the x/y dimension that is outside the already-defined rectangle
        allScreens.width += (screenRect.width - (screenRect.width - Math.abs(screenRect.x)));
        allScreens.height += (screenRect.height - (screenRect.height - Math.abs(screenRect.y)));

        // ensure the origin point is always at the top-leftmost point of all monitors.
        allScreens.x = Math.min(allScreens.x, screenRect.x);
        allScreens.y = Math.min(allScreens.y, screenRect.y);
    }
    return allScreens;
}

I came accross this thread when trying to implement a way to capture all screens on any given computer in a single screenshot, and could not find an answer as to how to accomplish this anywhere else.

In my case, Toolkit.getDefaultToolkit().getScreenSize() would only return the resolution of the primary monitor. I made the following method to create a Rectangle that has an origin at the top-leftmost point between all monitors, and is large enough to capture all the content on all the monitors supplied by GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() when passed to Robot().createScreenCapture.

private Rectangle allScreensRectangle()
{
    // make array of all screens
    GraphicsDevice[] screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    
    // start out with the rectangle of the primary monitor
    Rectangle allScreens = screens[0].getDefaultConfiguration().getBounds();
    for (int i = 1; i < screens.length; i++)
    {
        Rectangle screenRect = screens[i].getDefaultConfiguration().getBounds();

        // expand the size of the capture region to include any area in the x/y dimension that is outside the already-defined rectangle
        allScreens.width += (screenRect.width - (screenRect.width - Math.abs(screenRect.x)));
        allScreens.height += (screenRect.height - (screenRect.height - Math.abs(screenRect.y)));

        // ensure the origin point is always at the top-leftmost point of all monitors.
        allScreens.x = Math.min(allScreens.x, screenRect.x);
        allScreens.y = Math.min(allScreens.y, screenRect.y);
    }
    return allScreens;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文