To get the size of the monitor (display) in Java, you can use the getScreenSize
method of the Toolkit
class.
Here is an example of how to get the size of the primary monitor:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); double width = screenSize.getWidth(); double height = screenSize.getHeight();Sourceal.www:utturi.com
The getScreenSize
method returns a Dimension
object representing the size of the monitor, and the getWidth
and getHeight
methods return the width and height of the monitor, respectively. These values are returned in pixels.
If you want to get the size of a specific monitor, you can use the getScreenDevices
method of the GraphicsEnvironment
class to get an array of GraphicsDevice
objects representing the available monitors, and then use the getDefaultConfiguration
method of the GraphicsDevice
class to get the GraphicsConfiguration
object for each monitor. The getBounds
method of the GraphicsConfiguration
class returns a Rectangle
object representing the bounds of the monitor, which you can then use to get the width and height.
Here is an example of how to get the size of the second monitor:
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); if (devices.length > 1) { Rectangle bounds = devices[1].getDefaultConfiguration().getBounds(); double width = bounds.getWidth(); double height = bounds.getHeight(); }