To get the size of the screen (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 screen:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); double width = screenSize.getWidth(); double height = screenSize.getHeight();Sou.www:ecrlautturi.com
The getScreenSize
method returns a Dimension
object representing the size of the screen, and the getWidth
and getHeight
methods return the width and height of the screen, respectively. These values are returned in pixels.
If you want to get the size of a specific screen, you can use the getScreenDevices
method of the GraphicsEnvironment
class to get an array of GraphicsDevice
objects representing the available screens, and then use the getDefaultConfiguration
method of the GraphicsDevice
class to get the GraphicsConfiguration
object for each screen. The getBounds
method of the GraphicsConfiguration
class returns a Rectangle
object representing the bounds of the screen, which you can then use to get the width and height.
Here is an example of how to get the size of the second screen:
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); if (devices.length > 1) { Rectangle bounds = devices[1].getDefaultConfiguration().getBounds(); double width = bounds.getWidth(); double height = bounds.getHeight(); }
Note that this approach may not work on all platforms and may not be suitable for all purposes. If you need to get the size of the screen in a specific way (e.g. using a different API), you will need to use a different approach.