如何获得Java中每个单独屏幕的尺寸

发布于 2025-02-13 00:02:30 字数 599 浏览 3 评论 0 原文

我需要一个使我个人监视器的分辨率的功能。

我确实知道 toolkit.getDefaultToolKit()。getScreensize()为我提供了所有监视器的累积分辨率,但是为了限制我绘制的某些框架的大小,我想知道单个屏幕的分辨率。

我尝试使用 graphicSenvironment.getLocalGraphicsEnvironment()。getScreendevices(),它返回了所有屏幕的数组,但是我找不到其中的任何分辨率信息。

功能的标题应该是

/**
 * Returns the Dimension of all available screen devices in order of appearance in  
 * GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
 * 
 * @return the Dimensions of all available screen devices.
 */
public Dimension[] getScreenResolutions() {
    [ . . . ]
}

I need a function that gives me the resolutions of my individual monitors.

I do know that Toolkit.getDefaultToolkit().getScreenSize() gives me the cumulative resolution of all monitors, but to limit the size of some frames I draw I want to know the resolutions of the individual screens.

I tried using GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() which returns an array of all screens, but I could not find any resolution information in there.

The header of the function should be something like

/**
 * Returns the Dimension of all available screen devices in order of appearance in  
 * GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
 * 
 * @return the Dimensions of all available screen devices.
 */
public Dimension[] getScreenResolutions() {
    [ . . . ]
}

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

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

发布评论

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

评论(2

山川志 2025-02-20 00:02:30

You need to use GraphicsConfiguration. The documentation shows how to get the screen bounds.

痴情换悲伤 2025-02-20 00:02:30

当Rob Spoor回答时,我刚刚发现了同样的内容,所以我要对我的问题给出一个完整的答案:

GraphicsDevice 具有函数 getDefaultConfiguration()返回a <代码> GraphicsConfiguration 具有 getBounds()方法,该方法再次将值返回为矩形

因此,我的功能标题的实现将是:

/**
 * Returns the Dimension of all available screen devices in order of appearance in  
 * GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
 * 
 * @return the Dimensions of all available screen devices.
 */
public Dimension[] getScreenResolutions() {
    // step 1: get amount of screens and allocate space
    int deviceAmount = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length;
    Dimension[] dimensions = new Dimension[deviceAmount];
    Rectangle[] rectangles = new Rectangle[deviceAmount];
    // step 2: get values as Rectangle[]
    for (int i = 0; i < deviceAmount; i++) { 
        rectangles[i] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[i].getDefaultConfiguration().getBounds();
    }
    // step 3: convert to Dimension[]
    for (int i = 0; i < rectangles.length; i++) {
        dimensions[i] = new Dimension(r.getWidth(), r.getHeight());
    }
    // step 4: return
    return dimensions;
}

I just found out the same, while Rob Spoor answered, so I'm gonna give a full answer to my question:

GraphicsDevice has a function getDefaultConfiguration() which returns a GraphicsConfiguration which has a getBounds() method which again returns the values as a Rectangle.

An implementation of my function header would therefore be:

/**
 * Returns the Dimension of all available screen devices in order of appearance in  
 * GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
 * 
 * @return the Dimensions of all available screen devices.
 */
public Dimension[] getScreenResolutions() {
    // step 1: get amount of screens and allocate space
    int deviceAmount = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length;
    Dimension[] dimensions = new Dimension[deviceAmount];
    Rectangle[] rectangles = new Rectangle[deviceAmount];
    // step 2: get values as Rectangle[]
    for (int i = 0; i < deviceAmount; i++) { 
        rectangles[i] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[i].getDefaultConfiguration().getBounds();
    }
    // step 3: convert to Dimension[]
    for (int i = 0; i < rectangles.length; i++) {
        dimensions[i] = new Dimension(r.getWidth(), r.getHeight());
    }
    // step 4: return
    return dimensions;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文