[英]How do I get the dimensions of each individual screen in Java
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.我确实知道
Toolkit.getDefaultToolkit().getScreenSize()
为我提供了所有显示器的累积分辨率,但为了限制我绘制的某些帧的大小,我想知道各个屏幕的分辨率。
I tried using GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
which returns an array of all screens, but I could not find any resolution information in there.我尝试使用返回所有屏幕数组的
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
,但我在其中找不到任何分辨率信息。
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() {
[ . . . ]
}
You need to use GraphicsConfiguration .您需要使用GraphicsConfiguration 。 The documentation shows how to get the screen bounds.
该文档显示了如何获取屏幕边界。
I just found out the same, while Rob Spoor answered, so I'm gonna give a full answer to my question:我刚刚发现了同样的问题,而 Rob Spoor 回答了,所以我将对我的问题给出一个完整的答案:
GraphicsDevice
has a function getDefaultConfiguration()
which returns a GraphicsConfiguration
which has a getBounds()
method which again returns the values as a Rectangle
. GraphicsDevice
有一个函数getDefaultConfiguration()
,它返回一个GraphicsConfiguration
,它有一个getBounds()
方法,它再次将值作为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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.